4

I'm creating a view that is used to review migrated content that was flagged for review during the migration process. I want to provide a bulk action on this view that allows the user to select a bunch and mark them as "resolved".

I created a custom action to do this following some guidance from https://www.drupal.org/node/2330631:

config/install/pu_news_migrate.action.resolve_news_migration_flag.yml

langcode: en
status: true
dependencies:
  module:
    - node
id: resolve_news_migration_flag
label: 'Resolve news migration flag'
type: node
plugin: resolve_news_migration_flag
configuration: {  }

And created the action plugin implementation:

`src/Plugin/Action/ResolveNewsMigrationFlag.php

<?php

namespace Drupal\pu_news_migrate\Plugin\Action;

use Drupal\Core\Action\ActionBase;
use Drupal\Core\Session\AccountInterface;

/**
 * Removes the "needs review" flag on a migrated news article.
 *
 * @Action(
 *   id = "resolve_news_migration_flag",
 *   label = @Translation("Resolve news migration flag"),
 *   type = "node"
 * )
 */
class ResolveNewsMigrationFlag extends ActionBase {

  /**
   * {@inheritdoc}
   */
  public function execute($node = NULL) {
    if ($node) {
      /** @var \Drupal\node\NodeInterface $node */
      if (!$node->hasField('field_news_migration_review')) {
        throw new \RuntimeException("News migration field not found on node.");
      }
      $node->set('field_news_migration_review', FALSE);
      $node->save();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
    /** @var \Drupal\node\NodeInterface $object */
    $result = $object->field_news_migration_review->access('edit', $account, TRUE)
      ->andIf($object->access('update', $account, TRUE));

    return $return_as_object ? $result : $result->isAllowed();
  }

}

I also added a schema file: config/schema/pu_news_migrate.schema.yml:

action.configuration.resolve_news_migration_flag:
  type: resolve_news_migration_flag
  label: 'Remove the "needs review" flag from a migrated article.'

I uninstalled the module and re-installed it so that the action definition is loaded into Drupal's config.

However, when I view the list of actions I can select in views, it doesn't appear there. I enabled the "Actions" module which displays a list of all actions, and it does not appear there either.

What did I do wrong here?

  • Did you also provide a schema.yml file? Check the node module for an example. I also think the name of the file needs to be system.action.... not pu_news_migrate – Kevin Mar 15 '17 at 13:20
  • Yes, just updated question with that snippet – Brian Mar 15 '17 at 15:06
  • Did you try renaming the plugin yaml file name to system.action.., uninstalling and reinstalling? The actions in node and user are named this way, not node.action.blahblah. – Kevin Mar 15 '17 at 15:27
  • 1
    Kevin, that was it! If you post that as an answer I can can accept your answer and bounty. – Brian Mar 16 '17 at 17:00
  • I thought so, just wanted to double check. Answer added. – Kevin Mar 16 '17 at 17:01
9
+50

Action plugin yaml definitions need to be prefixed with the name system.action.action_name.yml, and not mymodule.action_name.yml.

For reference, check the provided action plugins for node and user module in core in their config/install and config/schema folders.

Uninstall the module, rename the files, and install the module again so the changes are picked up. You should then see your custom action.

So:

config/install/system.action.resolve_news_migration_flag.yml

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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