Version 1
[yaffs-website] / web / modules / contrib / linkit / src / Form / LinkitEditorDialog.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\linkit\Form\LinkitEditorDialog.
6  */
7
8 namespace Drupal\linkit\Form;
9
10 use Drupal\Core\Entity\EntityStorageInterface;
11 use Drupal\Core\Form\FormBase;
12 use Drupal\Core\Form\FormStateInterface;
13 use Drupal\filter\Entity\FilterFormat;
14 use Drupal\Core\Ajax\AjaxResponse;
15 use Drupal\Core\Ajax\HtmlCommand;
16 use Drupal\editor\Ajax\EditorDialogSave;
17 use Drupal\Core\Ajax\CloseModalDialogCommand;
18 use Drupal\linkit\AttributeCollection;
19 use Symfony\Component\DependencyInjection\ContainerInterface;
20
21 /**
22  * Provides a linkit dialog for text editors.
23  */
24 class LinkitEditorDialog extends FormBase {
25
26   /**
27    * The editor storage service.
28    *
29    * @var \Drupal\Core\Entity\EntityStorageInterface
30    */
31   protected $editorStorage;
32
33   /**
34    * The linkit profile storage service.
35    *
36    * @var \Drupal\Core\Entity\EntityStorageInterface
37    */
38   protected $linkitProfileStorage;
39
40   /**
41    * The linkit profile.
42    *
43    * @var \Drupal\linkit\ProfileInterface
44    */
45   protected $linkitProfile;
46
47   /**
48    * Constructs a form object for linkit dialog.
49    *
50    * @param \Drupal\Core\Entity\EntityStorageInterface $editor_storage
51    *   The editor storage service.
52    * @param \Drupal\Core\Entity\EntityStorageInterface $linkit_profile_storage
53    *   The linkit profile storage service.
54    */
55   public function __construct(EntityStorageInterface $editor_storage, EntityStorageInterface $linkit_profile_storage) {
56     $this->editorStorage = $editor_storage;
57     $this->linkitProfileStorage = $linkit_profile_storage;
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public static function create(ContainerInterface $container) {
64     return new static(
65       $container->get('entity.manager')->getStorage('editor'),
66       $container->get('entity.manager')->getStorage('linkit_profile')
67     );
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function getFormId() {
74     return 'linkit_editor_dialog_form';
75   }
76
77   /**
78    * {@inheritdoc}
79    *
80    * @param \Drupal\filter\Entity\FilterFormat $filter_format
81    *   The filter format for which this dialog corresponds.
82    */
83   public function buildForm(array $form, FormStateInterface $form_state, FilterFormat $filter_format = NULL) {
84     // The default values are set directly from \Drupal::request()->request,
85     // provided by the editor plugin opening the dialog.
86     $user_input = $form_state->getUserInput();
87     $input = isset($user_input['editor_object']) ? $user_input['editor_object'] : [];
88
89     /** @var \Drupal\editor\EditorInterface $editor */
90     $editor = $this->editorStorage->load($filter_format->id());
91     $linkit_profile_id = $editor->getSettings()['plugins']['linkit']['linkit_profile'];
92     $this->linkitProfile = $this->linkitProfileStorage->load($linkit_profile_id);
93
94     $form['#tree'] = TRUE;
95     $form['#attached']['library'][] = 'editor/drupal.editor.dialog';
96     $form['#prefix'] = '<div id="linkit-editor-dialog-form">';
97     $form['#suffix'] = '</div>';
98
99     // Everything under the "attributes" key is merged directly into the
100     // generated link tag's attributes.
101     $form['attributes']['href'] = [
102       '#title' => $this->t('Link'),
103       '#type' => 'linkit',
104       '#default_value' => isset($input['href']) ? $input['href'] : '',
105       '#description' => $this->t('Start typing to find content or paste a URL.'),
106       '#autocomplete_route_name' => 'linkit.autocomplete',
107       '#autocomplete_route_parameters' => [
108         'linkit_profile_id' => $linkit_profile_id
109       ],
110       '#weight' => 0,
111     ];
112
113     $this->addAttributes($form, $form_state, $this->linkitProfile->getAttributes(), $input);
114
115     $form['actions'] = [
116       '#type' => 'actions',
117     ];
118
119     $form['actions']['save_modal'] = [
120       '#type' => 'submit',
121       '#value' => $this->t('Save'),
122       '#submit' => [],
123       '#ajax' => [
124         'callback' => '::submitForm',
125         'event' => 'click',
126       ],
127     ];
128
129     return $form;
130   }
131
132   /**
133    * {@inheritdoc}
134    */
135   public function validateForm(array &$form, FormStateInterface $form_state) {
136     $attributes = array_filter($form_state->getValue('attributes'));
137     $form_state->setValue('attributes', $attributes);
138   }
139
140   /**
141    * {@inheritdoc}
142    */
143   public function submitForm(array &$form, FormStateInterface $form_state) {
144     $response = new AjaxResponse();
145
146     if ($form_state->getErrors()) {
147       unset($form['#prefix'], $form['#suffix']);
148       $form['status_messages'] = [
149         '#type' => 'status_messages',
150         '#weight' => -10,
151       ];
152       $response->addCommand(new HtmlCommand('#linkit-editor-dialog-form', $form));
153     }
154     else {
155       $response->addCommand(new EditorDialogSave($form_state->getValues()));
156       $response->addCommand(new CloseModalDialogCommand());
157     }
158
159     return $response;
160   }
161
162   /**
163    * Adds the attributes enabled on the current profile.
164    *
165    * @param array $form
166    *   An associative array containing the structure of the form.
167    * @param \Drupal\Core\Form\FormStateInterface $form_state
168    *   The current state of the form.
169    * @param AttributeCollection $attributes
170    *   A collection of attributes for the current profile.
171    * @param array $input
172    *   An array with the attribute values from the editor.
173    */
174   private function addAttributes(array &$form, FormStateInterface &$form_state, AttributeCollection $attributes, array $input) {
175     if ($attributes->count()) {
176       $form['linkit_attributes'] = [
177         '#type' => 'container',
178         '#title' => $this->t('Attributes'),
179         '#weight' => '10',
180       ];
181
182       /** @var \Drupal\linkit\AttributeInterface $plugin */
183       foreach ($attributes as $plugin) {
184         $plugin_name = $plugin->getHtmlName();
185
186         $default_value = isset($input[$plugin_name]) ? $input[$plugin_name] : '';
187         $form['linkit_attributes'][$plugin_name] = $plugin->buildFormElement($default_value);
188         $form['linkit_attributes'][$plugin_name] += [
189           '#parents' => [
190             'attributes', $plugin_name,
191           ],
192         ];
193       }
194     }
195   }
196
197   /**
198    * Gets the linkit profile entity.
199    *
200    * @return \Drupal\linkit\ProfileInterface
201    *   The current linkit profile used by this form.
202    */
203   public function getLinkitProfile() {
204     return $this->linkitProfile;
205   }
206
207 }