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