linkit.module 5.47 KB
Newer Older
anon's avatar
anon committed
1 2 3 4
<?php

/**
 * @file
5
 * Linkit hook implementations.
anon's avatar
anon committed
6 7
 */

8
use Drupal\Core\Form\FormStateInterface;
anon's avatar
anon committed
9
use Drupal\Core\Routing\RouteMatchInterface;
10 11
use Drupal\Core\Url;

anon's avatar
anon committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/**
 * Implements hook_help().
 */
function linkit_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.linkit':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Linkit module provides an easy interface for internal and external linking with wysiwyg editors by using an autocomplete field.') . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Managing Linkit profiles') . '</dt>';
      $output .= '<dd>' . t('You can create and edit Linkit profiles on the <a href=":profiles">Linkit profile page</a>. You can create a Linkit profile by clicking "<a href=":add_profile">Add profile</a>".', [':profiles' => Url::fromRoute('entity.linkit_profile.collection')->toString(), ':add_profile' => Url::fromRoute('entity.linkit_profile.add_form')->toString()]) . '</dd>';
      $output .= '</dl>';
      return $output;

    case 'entity.linkit_profile.collection':
      $output = '<p>' . t('Linkit profiles define how Linkit will operate on fields that have Linkit attached.') . '</p>';
      $output .= '<p>' . t('The most common way to use Linkit is to enable Linkit on the Drupal Link plugin and associate a Linkit profile to it on a Text format.') . '</p>';
      return $output;

    case 'linkit.matchers':
      $output = '<p>' . t('Matchers defines how different data can be queried and displayed in the autocomplete suggestion list. Multiple matchers of the same type can be used at the same time to granulate the suggestions. The order of the added matchers defines in which order the suggestions will be presented.') . '</p>';
      return $output;
  }
}

anon's avatar
anon committed
39 40 41 42 43 44 45 46 47
/**
 * Implements hook_ckeditor_plugin_info_alter().
 */
function linkit_ckeditor_plugin_info_alter(array &$plugins) {
  if (isset($plugins['drupallink'])) {
    $plugins['drupallink']['class'] = "Drupal\\linkit\\Plugin\\CKEditorPlugin\\LinkitDrupalLink";
  }
}

anon's avatar
anon committed
48
/**
49
 * Implements hook_form_FORM_ID_alter().
anon's avatar
anon committed
50 51
 */
function linkit_form_editor_link_dialog_alter(&$form, FormStateInterface $form_state, $form_id) {
52 53 54 55 56
  // Alter only the form with ID 'editor_link_dialog'.
  if ($form_id !== 'editor_link_dialog') {
    return;
  }

57
  /** @var Drupal\filter\Entity\FilterFormat $filter_format */
anon's avatar
anon committed
58 59 60 61 62 63 64
  $filter_format = $form_state->getBuildInfo()['args'][0];

  /** @var \Drupal\Core\Entity\EntityStorageInterface $editorStorage */
  $editorStorage = Drupal::service('entity.manager')->getStorage('editor');

  /** @var \Drupal\editor\EditorInterface $editor */
  $editor = $editorStorage->load($filter_format->id());
65 66 67 68 69 70 71
  $plugin_settings = $editor->getSettings()['plugins']['drupallink'];

  // Do not alter the form if Linkit is not enabled for this text format.
  if (!isset($plugin_settings['linkit_enabled']) || (isset($plugin_settings['linkit_enabled']) && !$plugin_settings['linkit_enabled'])) {
    return;
  }

anon's avatar
anon committed
72
  $linkit_profile_id = $editor->getSettings()['plugins']['drupallink']['linkit_profile'];
73

74 75 76 77 78 79 80 81 82
  if (isset($form_state->getUserInput()['editor_object'])) {
    $input = $form_state->getUserInput()['editor_object'];
    $form_state->set('link_element', $input);
    $form_state->setCached(TRUE);
  }
  else {
    // Retrieve the link element's attributes from form state.
    $input = $form_state->get('link_element') ?: [];
  }
83

84 85 86 87 88 89
  $form['href_dirty_check'] = [
    '#type' => 'hidden',
    '#default_value' => isset($input['href']) ? $input['href'] : '',
  ];

  $form['attributes']['href'] = array_merge($form['attributes']['href'], [
90
    '#type' => 'linkit',
91
    '#description' => t('Start typing to find content.'),
92 93 94 95 96
    '#autocomplete_route_name' => 'linkit.autocomplete',
    '#autocomplete_route_parameters' => [
      'linkit_profile_id' => $linkit_profile_id,
    ],
    "#weight" => -10,
97
    '#default_value' => isset($input['href']) ? $input['href'] : '',
98
  ]);
99

100 101 102 103 104 105 106 107 108 109
  $fields = [
    'data-entity-type',
    'data-entity-uuid',
    'data-entity-substitution',
  ];

  $form['attributes']["#weight"] = -100;

  foreach ($fields as $field_name) {
    $form['attributes'][$field_name] = [
110
      '#title' => $field_name,
111 112 113 114 115 116 117
      '#type' => 'hidden',
      '#default_value' => isset($input[$field_name]) ? $input[$field_name] : '',
    ];
  }

  // Add #submit callback that handles the data-* attributes.
  array_unshift($form['#submit'], 'linkit_form_editor_link_dialog_submit');
anon's avatar
anon committed
118 119 120
}

/**
121
 * Handles the data-* attributes and href replacement when appropriate.
anon's avatar
anon committed
122
 */
123
function linkit_form_editor_link_dialog_submit(array &$form, FormStateInterface $form_state) {
124 125
  $link_element = $form_state->get('link_element');

126
  $href = $form_state->getValue(['attributes', 'href']);
127
  $href_dirty_check = $form_state->getValue(['href_dirty_check']);
128

129
  if ($href !== $href_dirty_check) {
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    $form_state->unsetValue(['attributes', 'data-entity-type']);
    $form_state->unsetValue(['attributes', 'data-entity-uuid']);
    $form_state->unsetValue(['attributes', 'data-entity-substitution']);
  }

  $fields = [
    'href',
    'data-entity-type',
    'data-entity-uuid',
    'data-entity-substitution',
  ];

  foreach ($fields as $field_name) {
    $value = $form_state->getValue(['attributes', $field_name]);
    if (empty($value)) {
      if (!empty($link_element)) {
        $form_state->setValue(['attributes', $field_name], '');
      }
      else {
        $form_state->unsetValue(['attributes', $field_name]);
      }
151
    }
152
  }
anon's avatar
anon committed
153
}