3

I have a View that lists some nodes. Each node instance has a boolean - "Spam" - that defaults to FALSE.

In the view there is an "Operations" column where there shall be a link with the anchor text "Flag as spam". When the user clicks on that link, the value of the boolean Spam field for that node becomes "TRUE".

I found this tutorial: https://www.drupal.org/docs/7/creating-custom-modules/howtos/how-to-add-a-column-with-dynamically-generated-action-buttons - but it is for Drupal 7.

However, what do I need to do to list a custom operation (with clickable link or button) in an "Operations" column produced by Views?

3
  • 1
    Maybe you can try like this drupal.stackexchange.com/questions/295329/… May 3, 2022 at 16:31
  • 1
    Those are not "actions" they are "operations". Actions would be used for Views Bulk Operations.
    – sonfd
    May 6, 2022 at 16:00
  • @sonfd Thanks a lot for your correction. I've edited the question to use the correct terminology. May 7, 2022 at 8:16

1 Answer 1

4

Those are "operations". That view field is controlled by the EntityOperations field.

It's pretty easy to add a new operation for an entity by implementing hook_entity_operation.

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;

/**
 * Implements hook_entity_operation().
 */
function mymodule_entity_operation(\Drupal\Core\Entity\EntityInterface $entity) {
  $operations = [];
  if ($entity->getEntityTypeId() == 'node' && someOtherCondition($entity)) {
    $operations['flag_as_spam'] = [
      'title' => t('Flag as Spam'),
      // Replace with the route you've created to set the node value.
      'url' => Url::fromRoute('entity.node.canonical', ['node' => $entity->id()]),
      'weight' => 50,
    ];
  }
  return $operations;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.