Score:0

View Bulk Operation with custom action

cn flag

I have a custom action to update some table values base on node content selection. My custom actions are listed correctly on bulk update select box. But I can perform operation some random node content only. Any thoughts!! why It's allow actions to perform on some contents only.

View Bulk Operation: https://www.drupal.org/project/views_bulk_operations

Tried Version: 8.x-3.13 , 4.0.0-rc1

<?php

namespace Drupal\custom_user\Plugin\Action;

use Drupal\node\Entity\Node;
use Drupal\views_bulk_operations\Action\ViewsBulkOperationsActionBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Entity\ContentEntityInterface;

/**
 * Content moderation publish node.
 *
 * @Action(
 *   id = "notification_read",
 *   label = @Translation("Update Notification As Read"),
 *   type = "node",
 *   confirm = TRUE
 * )
 */

class NotificationRead extends ViewsBulkOperationsActionBase {

  use StringTranslationTrait;

  /**
   * {@inheritdoc}
   */
  public function execute(ContentEntityInterface $entity = NULL) {
    $nid = $entity->id();
    $uid = \Drupal::currentUser()->id();
    if ($nid) {
      $connection = \Drupal::database();
      $connection->delete('notifications')
      ->condition('entity_id', $nid)
      ->condition('entity_uid', $uid)
      ->execute();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
   \Drupal::logger('test')->warning(print_r("test", TRUE));
   if ($object instanceof Node) {
    return True;
  }

  return FALSE;
}
}[![enter image description here][1]][1]

Working

Not Working

Score:0
de flag

This line means that you can only use your action on Nodes, and not any other entity type:

*  type = "node",

Remove this line, clear your registry, and you will be able to apply your action to other entity types as well.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.