Added missing modules, including some as submodules.
[yaffs-website] / web / modules / contrib / linkit / src / Form / Matcher / AddForm.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\linkit\Form\Matcher\AddForm.
6  */
7
8 namespace Drupal\linkit\Form\Matcher;
9
10 use Drupal\Core\Form\FormBase;
11 use Drupal\Core\Form\FormStateInterface;
12 use Drupal\linkit\ConfigurableMatcherInterface;
13 use Drupal\linkit\MatcherManager;
14 use Drupal\linkit\ProfileInterface;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
16
17 /**
18  * Provides a form to apply matchers to a profile.
19  */
20 class AddForm extends FormBase {
21
22   /**
23    * The profiles to which the matchers will be applied.
24    *
25    * @var \Drupal\linkit\ProfileInterface
26    */
27   protected $linkitProfile;
28
29
30   /**
31    * The matcher manager.
32    *
33    * @var \Drupal\linkit\MatcherManager
34    */
35   protected $manager;
36
37   /**
38    * Constructs a new AddForm.
39    *
40    * @param \Drupal\linkit\MatcherManager $manager
41    *   The matcher manager.
42    */
43   public function __construct(MatcherManager $manager) {
44     $this->manager = $manager;
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public static function create(ContainerInterface $container) {
51     return new static(
52       $container->get('plugin.manager.linkit.matcher')
53     );
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function getFormId() {
60     return "linkit_matcher_add_form";
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public function buildForm(array $form, FormStateInterface $form_state, ProfileInterface $linkit_profile = NULL) {
67     $this->linkitProfile = $linkit_profile;
68
69     $form['#attached']['library'][] = 'linkit/linkit.admin';
70     $header = [
71       'label' => $this->t('Matchers'),
72     ];
73
74     $form['plugin'] = [
75       '#type' => 'tableselect',
76       '#header' => $header,
77       '#options' => $this->buildRows(),
78       '#empty' => $this->t('No matchers available.'),
79       '#multiple' => FALSE,
80     ];
81
82     $form['actions'] = ['#type' => 'actions'];
83     $form['actions']['submit'] = [
84       '#type' => 'submit',
85       '#value' => $this->t('Save and continue'),
86       '#submit' => ['::submitForm'],
87       '#tableselect' => TRUE,
88       '#button_type' => 'primary',
89     ];
90
91     $options = [];
92     foreach ($this->manager->getDefinitions() as $id => $plugin) {
93       $options[$id] = $plugin['label'];
94     }
95
96     return $form;
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   public function validateForm(array &$form, FormStateInterface $form_state) {
103     if (empty($form_state->getValue('plugin'))) {
104       $form_state->setErrorByName('plugin', $this->t('No matcher selected.'));
105     }
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public function submitForm(array &$form, FormStateInterface $form_state) {
112     $form_state->cleanValues();
113
114     /** @var \Drupal\linkit\MatcherInterface $plugin */
115     $plugin = $this->manager->createInstance($form_state->getValue('plugin'));
116
117     $plugin_uuid = $this->linkitProfile->addMatcher($plugin->getConfiguration());
118     $this->linkitProfile->save();
119
120     $this->logger('linkit')->notice('Added %label matcher to the @profile profile.', [
121       '%label' => $this->linkitProfile->getMatcher($plugin_uuid)->getLabel(),
122       '@profile' => $this->linkitProfile->label(),
123     ]);
124
125     $is_configurable = $plugin instanceof ConfigurableMatcherInterface;
126     if ($is_configurable) {
127       $form_state->setRedirect('linkit.matcher.edit', [
128         'linkit_profile' => $this->linkitProfile->id(),
129         'plugin_instance_id' => $plugin_uuid,
130       ]);
131     }
132     else {
133       drupal_set_message($this->t('Added %label matcher.', ['%label' => $plugin->getLabel()]));
134
135       $form_state->setRedirect('linkit.matchers', [
136         'linkit_profile' => $this->linkitProfile->id(),
137       ]);
138     }
139   }
140
141   /**
142    * Builds the table rows.
143    *
144    * @return array
145    *   An array of table rows.
146    */
147   private function buildRows() {
148     $rows = [];
149     $all_plugins = $this->manager->getDefinitions();
150     uasort($all_plugins, function ($a, $b) {
151       return strnatcasecmp($a['label'], $b['label']);
152     });
153     foreach ($all_plugins as $definition) {
154       /** @var \Drupal\linkit\MatcherInterface $plugin */
155       $plugin = $this->manager->createInstance($definition['id']);
156       $row = [
157         'label' => $plugin->getLabel(),
158       ];
159       $rows[$plugin->getPluginId()] = $row;
160     }
161
162     return $rows;
163   }
164
165 }