editor_advanced_link-8.x-1.8/editor_advanced_link.module

editor_advanced_link.module
<?php

/**
 * @file
 * Advanced Link module.
 */

use Drupal\Core\Form\FormStateInterface;
use Drupal\editor\Entity\Editor;

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Add a title and a target fields on EditorLinkDialog if the filter allows it.
 * Note: the editor_file module declares that its EditorFileDialog form uses
 *   'editor_link_dialog' as base_form_id. In this case, the function above is
 *   going to be called as an implementation of hook_form_BASE_FORM_ID_alter().
 */
function editor_advanced_link_form_editor_link_dialog_alter(&$form, FormStateInterface $form_state, $form_id) {
  $argument = $form_state->getBuildInfo()['args'][0];
  // In case the only argument we get is the Editor instead of the FilterFormat.
  if ($argument instanceof Editor) {
    $argument = $argument->getFilterFormat();
  }
  $restrictions = $argument->getHtmlRestrictions();

  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') ?: [];
  }

  /*
   * Helper to retrieve form fields' default values.
   *
   * @param string $attribute_name
   * @param string $fallback
   *
   * @return mixed
   *   The existing value or the fallback.
   */
  $get_default_value = function ($attribute_name, $fallback = '') use ($input) {
    return !empty($input[$attribute_name]) ? $input[$attribute_name] : $fallback;
  };

  /*
   * Helper to set the status of a form field according to the status of the
   * filter about the attribute it is defining.
   *
   * @param string $attribute_name
   *
   * @return bool
   *   TRUE if the filter is disabled or if the entire "a" tag is allowed or if
   *   the given attribute is allowed for the "a" tag.
   */
  $is_accessible = function ($attribute_name) use ($restrictions) {
    return $restrictions === FALSE || $restrictions['allowed']['a'] === TRUE || !empty($restrictions['allowed']['a'][$attribute_name]);
  };

  // Load the library.
  $form['#attached']['library'][] = 'editor_advanced_link/editor_advanced_link';

  $form['attributes']['href']['#weight'] = 0;

  $form['attributes']['title'] = [
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#description' => t('Populates the title attribute of the link, usually shown as a small tooltip on hover.'),
    '#default_value' => $get_default_value('title'),
    '#maxlength' => 512,
    '#weight' => 1,
    '#access' => $is_accessible('title'),
  ];

  $form['advanced'] = [
    '#type' => 'details',
    '#title' => t('Advanced'),
    '#access' => FALSE,
    '#weight' => 2,
  ];

  $form['attributes']['aria-label'] = [
    '#type' => 'textfield',
    '#title' => t('ARIA label'),
    '#description' => t('Provide an alternative text value/label for assistive technologies.'),
    '#default_value' => $get_default_value('aria-label'),
    '#maxlength' => 512,
    '#access' => $is_accessible('aria-label'),
    '#group' => 'advanced',
  ];

  $form['attributes']['class'] = [
    '#type' => 'textfield',
    '#title' => t('CSS classes'),
    '#description' => t('List of CSS classes to add to the link, separated by spaces.'),
    '#default_value' => $get_default_value('class'),
    '#maxlength' => 512,
    '#access' => $is_accessible('class'),
    '#group' => 'advanced',
  ];

  $form['attributes']['id'] = [
    '#type' => 'textfield',
    '#title' => t('ID'),
    '#description' => t('Allows linking to this content using a <a href="https://en.wikipedia.org/wiki/Fragment_identifier" target="_blank">URL fragment</a>. Must be unique.'),
    '#default_value' => $get_default_value('id'),
    '#maxlength' => 512,
    '#access' => $is_accessible('id'),
    '#group' => 'advanced',
  ];

  $form['attributes']['target'] = [
    '#title' => t('Open in new window/tab'),
    '#type' => 'checkbox',
    '#description' => t('See WCAG guidance on <a href="https://www.w3.org/WAI/WCAG21/Techniques/general/G200" target="_blank">opening links in new windows/tabs</a>.'),
    '#default_value' => $get_default_value('target', FALSE),
    '#return_value' => '_blank',
    '#access' => $is_accessible('target'),
    '#group' => 'advanced',
  ];

  $form['attributes']['rel'] = [
    '#type' => 'textfield',
    '#title' => t('Relation (rel)'),
    '#description' => t('Often used by JS galleries. When opening links in new windows/tabs, it is advised to add the noopener attribute due to security concerns.'),
    '#default_value' => $get_default_value('rel'),
    '#maxlength' => 512,
    '#access' => $is_accessible('rel'),
    '#group' => 'advanced',
  ];

  // Show the advanced group if at least one of its fields is accessible.
  foreach ($form['attributes'] as $element) {
    if (!empty($element['#group']) && $element['#group'] === 'advanced' && (!isset($element['#access']) || $element['#access'] === TRUE)) {
      $form['advanced']['#access'] = TRUE;
      break;
    }
  }

  // Add #validate callback that handles empty attributes.
  array_unshift($form['#validate'], '_editor_advanced_link_attributes_validate');
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * LinkIt 5.x module compatibility.
 */
function editor_advanced_link_form_linkit_editor_dialog_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  editor_advanced_link_form_editor_link_dialog_alter($form, $form_state, $form_id);
}

/**
 * Filter empty attributes to avoid empty HTML output.
 */
function _editor_advanced_link_attributes_validate(array &$form, FormStateInterface $form_state) {
  $values = $form_state->getValue('attributes');
  $link_element = $form_state->get('link_element');
  foreach ($values as $key => $value) {
    if (empty($value)) {
      $form_state->setValue(['attributes', $key], '');
      // Special case on content creation.
      if (empty($link_element)) {
        $form_state->unsetValue(['attributes', $key]);
      }
    }
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc