Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / linkit / src / Form / Matcher / OverviewForm.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\linkit\Form\Matcher\OverviewForm.
6  */
7
8 namespace Drupal\linkit\Form\Matcher;
9
10 use Drupal\Core\Form\FormBase;
11 use Drupal\Core\Form\FormStateInterface;
12 use Drupal\Core\Url;
13 use Drupal\linkit\ConfigurableMatcherInterface;
14 use Drupal\linkit\MatcherManager;
15 use Drupal\linkit\ProfileInterface;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
17
18 /**
19  * Provides an overview form for matchers on a profile.
20  */
21 class OverviewForm extends FormBase {
22
23   /**
24    * The profiles to which the matchers are applied to.
25    *
26    * @var \Drupal\linkit\ProfileInterface
27    */
28   private $linkitProfile;
29
30   /**
31    * The matcher manager.
32    *
33    * @var \Drupal\linkit\MatcherManager
34    */
35   protected $manager;
36
37   /**
38    * Constructs a new OverviewForm.
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_overview_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     $form['#attached']['library'][] = 'linkit/linkit.admin';
69     $form['plugins'] = [
70       '#type' => 'table',
71       '#header' => [
72         [
73           'data' => $this->t('Matcher'),
74           'colspan' => 2
75         ],
76         $this->t('Weight'),
77         $this->t('Operations'),
78       ],
79       '#empty' => $this->t('No matchers added.'),
80       '#tabledrag' => [
81         [
82           'action' => 'order',
83           'relationship' => 'sibling',
84           'group' => 'plugin-order-weight',
85         ],
86       ],
87     ];
88
89     foreach ($this->linkitProfile->getMatchers() as $plugin) {
90       $key = $plugin->getUuid();
91
92       $form['plugins'][$key]['#attributes']['class'][] = 'draggable';
93       $form['plugins'][$key]['#weight'] = $plugin->getWeight();
94
95       $form['plugins'][$key]['label'] = [
96         '#plain_text' => (string) $plugin->getLabel(),
97       ];
98
99       $form['plugins'][$key]['summary'] = [];
100
101       $summary = $plugin->getSummary();
102       if (!empty($summary)) {
103         $form['plugins'][$key]['summary'] = [
104           '#type' => 'inline_template',
105           '#template' => '<div class="linkit-plugin-summary">{{ summary|safe_join("<br />") }}</div>',
106           '#context' => ['summary' => $summary],
107         ];
108       }
109
110       $form['plugins'][$key]['weight'] = [
111         '#type' => 'weight',
112         '#title' => t('Weight for @title', ['@title' => (string) $plugin->getLabel()]),
113         '#title_display' => 'invisible',
114         '#default_value' => $plugin->getWeight(),
115         '#attributes' => ['class' => ['plugin-order-weight']],
116       ];
117
118       $form['plugins'][$key]['operations'] = [
119         '#type' => 'operations',
120         '#links' => [],
121       ];
122
123       $is_configurable = $plugin instanceof ConfigurableMatcherInterface;
124       if ($is_configurable) {
125         $form['plugins'][$key]['operations']['#links']['edit'] = [
126           'title' => t('Edit'),
127           'url' => Url::fromRoute('linkit.matcher.edit', [
128             'linkit_profile' =>  $this->linkitProfile->id(),
129             'plugin_instance_id' => $key,
130           ]),
131         ];
132       }
133
134       $form['plugins'][$key]['operations']['#links']['delete'] = [
135         'title' => t('Delete'),
136         'url' => Url::fromRoute('linkit.matcher.delete', [
137           'linkit_profile' =>  $this->linkitProfile->id(),
138           'plugin_instance_id' => $key,
139         ]),
140       ];
141     }
142
143     $form['actions'] = ['#type' => 'actions'];
144     $form['actions']['submit'] = [
145       '#type' => 'submit',
146       '#value' => $this->t('Save'),
147       '#button_type' => 'primary',
148     ];
149
150     return $form;
151   }
152
153   /**
154    * {@inheritdoc}
155    */
156   public function submitForm(array &$form, FormStateInterface $form_state) {
157     foreach ($form_state->getValue('plugins') as $id => $plugin_data) {
158       if ($this->linkitProfile->getMatchers()->has($id)) {
159         $this->linkitProfile->getMatcher($id)->setWeight($plugin_data['weight']);
160       }
161     }
162     $this->linkitProfile->save();
163   }
164
165 }