annotate core/modules/action/src/ActionAddForm.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\action;
Chris@0 4
Chris@0 5 use Drupal\Component\Utility\Crypt;
Chris@0 6 use Drupal\Core\Action\ActionManager;
Chris@0 7 use Drupal\Core\Entity\EntityStorageInterface;
Chris@0 8 use Drupal\Core\Form\FormStateInterface;
Chris@0 9 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Provides a form for action add forms.
Chris@0 13 */
Chris@0 14 class ActionAddForm extends ActionFormBase {
Chris@0 15
Chris@0 16 /**
Chris@0 17 * The action manager.
Chris@0 18 *
Chris@0 19 * @var \Drupal\Core\Action\ActionManager
Chris@0 20 */
Chris@0 21 protected $actionManager;
Chris@0 22
Chris@0 23 /**
Chris@0 24 * Constructs a new ActionAddForm.
Chris@0 25 *
Chris@0 26 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
Chris@0 27 * The action storage.
Chris@0 28 * @param \Drupal\Core\Action\ActionManager $action_manager
Chris@0 29 * The action plugin manager.
Chris@0 30 */
Chris@0 31 public function __construct(EntityStorageInterface $storage, ActionManager $action_manager) {
Chris@0 32 parent::__construct($storage);
Chris@0 33
Chris@0 34 $this->actionManager = $action_manager;
Chris@0 35 }
Chris@0 36
Chris@0 37 /**
Chris@0 38 * {@inheritdoc}
Chris@0 39 */
Chris@0 40 public static function create(ContainerInterface $container) {
Chris@0 41 return new static(
Chris@0 42 $container->get('entity.manager')->getStorage('action'),
Chris@0 43 $container->get('plugin.manager.action')
Chris@0 44 );
Chris@0 45 }
Chris@0 46
Chris@0 47 /**
Chris@0 48 * {@inheritdoc}
Chris@0 49 *
Chris@0 50 * @param string $action_id
Chris@0 51 * The hashed version of the action ID.
Chris@0 52 */
Chris@0 53 public function buildForm(array $form, FormStateInterface $form_state, $action_id = NULL) {
Chris@0 54 // In \Drupal\action\Form\ActionAdminManageForm::buildForm() the action
Chris@0 55 // are hashed. Here we have to decrypt it to find the desired action ID.
Chris@0 56 foreach ($this->actionManager->getDefinitions() as $id => $definition) {
Chris@0 57 $key = Crypt::hashBase64($id);
Chris@0 58 if ($key === $action_id) {
Chris@0 59 $this->entity->setPlugin($id);
Chris@0 60 // Derive the label and type from the action definition.
Chris@0 61 $this->entity->set('label', $definition['label']);
Chris@0 62 $this->entity->set('type', $definition['type']);
Chris@0 63 break;
Chris@0 64 }
Chris@0 65 }
Chris@0 66
Chris@0 67 return parent::buildForm($form, $form_state);
Chris@0 68 }
Chris@0 69
Chris@0 70 }