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