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