Version 1
[yaffs-website] / web / core / modules / action / src / Form / ActionAdminManageForm.php
1 <?php
2
3 namespace Drupal\action\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Component\Utility\Crypt;
7 use Drupal\Core\Action\ActionManager;
8 use Drupal\Core\Form\FormStateInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Provides a configuration form for configurable actions.
13  */
14 class ActionAdminManageForm extends FormBase {
15
16   /**
17    * The action plugin manager.
18    *
19    * @var \Drupal\Core\Action\ActionManager
20    */
21   protected $manager;
22
23   /**
24    * Constructs a new ActionAdminManageForm.
25    *
26    * @param \Drupal\Core\Action\ActionManager $manager
27    *   The action plugin manager.
28    */
29   public function __construct(ActionManager $manager) {
30     $this->manager = $manager;
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public static function create(ContainerInterface $container) {
37     return new static(
38       $container->get('plugin.manager.action')
39     );
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function getFormId() {
46     return 'action_admin_manage';
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public function buildForm(array $form, FormStateInterface $form_state) {
53     $actions = [];
54     foreach ($this->manager->getDefinitions() as $id => $definition) {
55       if (is_subclass_of($definition['class'], '\Drupal\Core\Plugin\PluginFormInterface')) {
56         $key = Crypt::hashBase64($id);
57         $actions[$key] = $definition['label'] . '...';
58       }
59     }
60     $form['parent'] = [
61       '#type' => 'details',
62       '#title' => $this->t('Create an advanced action'),
63       '#attributes' => ['class' => ['container-inline']],
64       '#open' => TRUE,
65     ];
66     $form['parent']['action'] = [
67       '#type' => 'select',
68       '#title' => $this->t('Action'),
69       '#title_display' => 'invisible',
70       '#options' => $actions,
71       '#empty_option' => $this->t('Choose an advanced action'),
72     ];
73     $form['parent']['actions'] = [
74       '#type' => 'actions'
75     ];
76     $form['parent']['actions']['submit'] = [
77       '#type' => 'submit',
78       '#value' => $this->t('Create'),
79     ];
80     return $form;
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function submitForm(array &$form, FormStateInterface $form_state) {
87     if ($form_state->getValue('action')) {
88       $form_state->setRedirect(
89         'action.admin_add',
90         ['action_id' => $form_state->getValue('action')]
91       );
92     }
93   }
94
95 }