3 namespace Drupal\action;
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;
12 * Provides a form for action add forms.
14 class ActionAddForm extends ActionFormBase {
19 * @var \Drupal\Core\Action\ActionManager
21 protected $actionManager;
24 * Constructs a new ActionAddForm.
26 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
28 * @param \Drupal\Core\Action\ActionManager $action_manager
29 * The action plugin manager.
31 public function __construct(EntityStorageInterface $storage, ActionManager $action_manager) {
32 parent::__construct($storage);
34 $this->actionManager = $action_manager;
40 public static function create(ContainerInterface $container) {
42 $container->get('entity.manager')->getStorage('action'),
43 $container->get('plugin.manager.action')
50 * @param string $action_id
51 * The hashed version of the action ID.
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']);
67 return parent::buildForm($form, $form_state);