actionStorage = $action_storage; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $configuration, $plugin_id, $plugin_definition, $container->get('entity_type.manager')->getStorage('action') ); } /** * {@inheritdoc} */ public function summary() { return $this->t('Perform a pre-configured action.'); } /** * {@inheritdoc} */ public function execute(EntityInterface $entity = NULL) { $config = $this->getConfiguration(); $action_ids = $config['actions']; foreach ($action_ids as $action_id) { $action = $this->actionStorage->load($action_id); // Make sure that the action is appropriate for the entity. if ($entity->getEntityTypeId() === $action->getType()) { $action->execute([$entity]); } } } /** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $actions = $this->actionStorage->loadMultiple(); foreach ($actions as $action) { $options[ucfirst($action->getType())][$action->id()] = $action->label(); } $config = $this->getConfiguration(); $form['actions'] = [ '#title' => $this->t('Actions'), '#description' => $this->t('Pre-configured actions to execute. Multiple actions may be selected by shift or ctrl clicking.'), '#type' => 'select', '#multiple' => TRUE, '#options' => $options, '#default_value' => isset($config['actions']) ? $config['actions'] : '', '#size' => 15, ]; return $form; } /** * {@inheritdoc} */ public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { $this->setConfiguration(['actions' => $form_state->getValue('actions')]); } }