In this article, I'll show you how to create or update entity operations links in Drupal 8 by using hook_entity_operation.
Suppose I want to add custom operation and link it to my own page, but only for article content type. I can do it by implementing hook_entity_operation
link following.
/**
* Implements hook_entity_operation().
*/
function MODULENAME_entity_operation_alter(array &$operations, EntityInterface $entity) {
$entityTypeId = $entity->getEntityTypeId();
if ($entityTypeId !== 'node') {
return;
}
$nodeType = $entity->getType();
$nodeId = $entity->id();
if ($nodeType === 'article') {
// add custom operation
$editUrl = Url::fromRoute('entity.node.edit_form', ['node' => $nodeId]);
$operations['custom_operation'] = array(
'title' => t('Custom'),
'weight' => 20,
'url' => $editUrl,
);
}
}
also using hook_entity_operation
, you can modify any entity operations links, for example modify the article edit operation and link it to your own page.