5 * Contains \Drupal\linkit\Form\Matcher\AddForm.
8 namespace Drupal\linkit\Form\Matcher;
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;
18 * Provides a form to apply matchers to a profile.
20 class AddForm extends FormBase {
23 * The profiles to which the matchers will be applied.
25 * @var \Drupal\linkit\ProfileInterface
27 protected $linkitProfile;
31 * The matcher manager.
33 * @var \Drupal\linkit\MatcherManager
38 * Constructs a new AddForm.
40 * @param \Drupal\linkit\MatcherManager $manager
41 * The matcher manager.
43 public function __construct(MatcherManager $manager) {
44 $this->manager = $manager;
50 public static function create(ContainerInterface $container) {
52 $container->get('plugin.manager.linkit.matcher')
59 public function getFormId() {
60 return "linkit_matcher_add_form";
66 public function buildForm(array $form, FormStateInterface $form_state, ProfileInterface $linkit_profile = NULL) {
67 $this->linkitProfile = $linkit_profile;
69 $form['#attached']['library'][] = 'linkit/linkit.admin';
71 'label' => $this->t('Matchers'),
75 '#type' => 'tableselect',
77 '#options' => $this->buildRows(),
78 '#empty' => $this->t('No matchers available.'),
82 $form['actions'] = ['#type' => 'actions'];
83 $form['actions']['submit'] = [
85 '#value' => $this->t('Save and continue'),
86 '#submit' => ['::submitForm'],
87 '#tableselect' => TRUE,
88 '#button_type' => 'primary',
92 foreach ($this->manager->getDefinitions() as $id => $plugin) {
93 $options[$id] = $plugin['label'];
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.'));
111 public function submitForm(array &$form, FormStateInterface $form_state) {
112 $form_state->cleanValues();
114 /** @var \Drupal\linkit\MatcherInterface $plugin */
115 $plugin = $this->manager->createInstance($form_state->getValue('plugin'));
117 $plugin_uuid = $this->linkitProfile->addMatcher($plugin->getConfiguration());
118 $this->linkitProfile->save();
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(),
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,
133 drupal_set_message($this->t('Added %label matcher.', ['%label' => $plugin->getLabel()]));
135 $form_state->setRedirect('linkit.matchers', [
136 'linkit_profile' => $this->linkitProfile->id(),
142 * Builds the table rows.
145 * An array of table rows.
147 private function buildRows() {
149 $all_plugins = $this->manager->getDefinitions();
150 uasort($all_plugins, function ($a, $b) {
151 return strnatcasecmp($a['label'], $b['label']);
153 foreach ($all_plugins as $definition) {
154 /** @var \Drupal\linkit\MatcherInterface $plugin */
155 $plugin = $this->manager->createInstance($definition['id']);
157 'label' => $plugin->getLabel(),
159 $rows[$plugin->getPluginId()] = $row;