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