n Create custom entity operations links in Drupal 8 | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

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,
        );
    }
}
custom operation link

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.

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook