5 * Contains \Drupal\linkit\Form\LinkitEditorDialog.
8 namespace Drupal\linkit\Form;
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;
22 * Provides a linkit dialog for text editors.
24 class LinkitEditorDialog extends FormBase {
27 * The editor storage service.
29 * @var \Drupal\Core\Entity\EntityStorageInterface
31 protected $editorStorage;
34 * The linkit profile storage service.
36 * @var \Drupal\Core\Entity\EntityStorageInterface
38 protected $linkitProfileStorage;
43 * @var \Drupal\linkit\ProfileInterface
45 protected $linkitProfile;
48 * Constructs a form object for linkit dialog.
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.
55 public function __construct(EntityStorageInterface $editor_storage, EntityStorageInterface $linkit_profile_storage) {
56 $this->editorStorage = $editor_storage;
57 $this->linkitProfileStorage = $linkit_profile_storage;
63 public static function create(ContainerInterface $container) {
65 $container->get('entity.manager')->getStorage('editor'),
66 $container->get('entity.manager')->getStorage('linkit_profile')
73 public function getFormId() {
74 return 'linkit_editor_dialog_form';
80 * @param \Drupal\filter\Entity\FilterFormat $filter_format
81 * The filter format for which this dialog corresponds.
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'] : [];
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);
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>';
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'),
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
113 $this->addAttributes($form, $form_state, $this->linkitProfile->getAttributes(), $input);
116 '#type' => 'actions',
119 $form['actions']['save_modal'] = [
121 '#value' => $this->t('Save'),
124 'callback' => '::submitForm',
135 public function validateForm(array &$form, FormStateInterface $form_state) {
136 $attributes = array_filter($form_state->getValue('attributes'));
137 $form_state->setValue('attributes', $attributes);
143 public function submitForm(array &$form, FormStateInterface $form_state) {
144 $response = new AjaxResponse();
146 if ($form_state->getErrors()) {
147 unset($form['#prefix'], $form['#suffix']);
148 $form['status_messages'] = [
149 '#type' => 'status_messages',
152 $response->addCommand(new HtmlCommand('#linkit-editor-dialog-form', $form));
155 $response->addCommand(new EditorDialogSave($form_state->getValues()));
156 $response->addCommand(new CloseModalDialogCommand());
163 * Adds the attributes enabled on the current profile.
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.
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'),
182 /** @var \Drupal\linkit\AttributeInterface $plugin */
183 foreach ($attributes as $plugin) {
184 $plugin_name = $plugin->getHtmlName();
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] += [
190 'attributes', $plugin_name,
198 * Gets the linkit profile entity.
200 * @return \Drupal\linkit\ProfileInterface
201 * The current linkit profile used by this form.
203 public function getLinkitProfile() {
204 return $this->linkitProfile;