menu_disallow_external_links-1.0.0-rc1/menu_disallow_external_links.module

menu_disallow_external_links.module
<?php

/**
 * @file
 * Hook implementations for module menu_disallow_external_links.
 *
 * @codeCoverageIgnore covered by functional tests
 * @see \Drupal\Tests\menu_disallow_external_links\Functional\MenuDisallowExternalLinks
 */

use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\system\Entity\Menu;
use Drupal\system\MenuInterface;

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Add checkbox to disallow external links to the menu form.
 */
function menu_disallow_external_links_form_menu_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (\Drupal::currentUser()->hasPermission('administer menu')) {
    // Get menu object.
    /** @var Drupal\menu_ui\MenuForm */
    $form_object = $form_state->getFormObject();
    /** @var Drupal\system\Entity\Menu */
    $menu = $form_object->getEntity();

    // Add checkbox to the form.
    $form['disallow_external_links'] = [
      '#type' => 'checkbox',
      '#title' => t('Disallow external links'),
      '#description' => t('Check this box if this menu should not contain external links.'),
      '#access' => \Drupal::currentUser()->hasPermission('administer menu'),
      '#default_value' => $menu->getThirdPartySetting('menu_disallow_external_links', 'menu_disallow_external_links', FALSE),
    ];

    // Add custom entity builder to the form.
    $form['#entity_builders'][] = '_menu_disallow_external_links_form_menu_edit_form_builder';
  }
}

/**
 * Store settings in config when saving the menu.
 *
 * @see menu_disallow_external_links_form_menu_edit_form_alter()
 */
function _menu_disallow_external_links_form_menu_edit_form_builder($entity_type, MenuInterface $type, &$form, FormStateInterface $form_state) {
  $disallow = $form_state->getValue('disallow_external_links');
  $type->setThirdPartySetting('menu_disallow_external_links', 'menu_disallow_external_links', $disallow);
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * If external links are disallowed for this menu, then add custom validator.
 */
function menu_disallow_external_links_form_menu_link_content_menu_link_content_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  // Get menu link object.
  /** @var Drupal\menu_link_content\Form\MenuLinkContentForm */
  $form_object = $form_state->getFormObject();
  /** @var Drupal\menu_link_content\Entity\MenuLinkContent */
  $menu_link = $form_object->getEntity();

  // Determine whether the link belongs to a menu that disallows external links.
  $menu_name = $menu_link->getMenuName();
  $menu = Menu::load($menu_name);
  $disallow = $menu->getThirdPartySetting('menu_disallow_external_links', 'menu_disallow_external_links', FALSE);

  // If it does, then add our validator and update the link field's help text.
  // @see Drupal\link\Plugin\Field\FieldWidget\LinkWidget::formElement()
  if ($disallow) {
    $form['link']['widget'][0]['uri']['#description']['#items'][1] = t('This must be an internal path such as %add-node. You can also start typing the title of a piece of content to select it. Enter %front to link to the front page. Enter %nolink to display link text only. Enter %button to display keyboard-accessible link text only.', [
      '%add-node' => '/node/add',
      '%front' => '<front>',
      '%nolink' => '<nolink>',
      '%button' => '<button>',
    ]);
    $form['#validate'][] = '_menu_disallow_external_links_form_menu_link_content_menu_link_content_form_validate';
  }
}

/**
 * Validate that the link provided by the user is not external.
 *
 * @see menu_disallow_external_links_form_menu_link_content_menu_link_content_form_alter()
 */
function _menu_disallow_external_links_form_menu_link_content_menu_link_content_form_validate(&$form, FormStateInterface $form_state) {
  // Get submitted link.
  $link = $form_state->getValue('link');
  if (UrlHelper::isExternal($link[0]['uri'])) {
    $form_state->setErrorByName('link', t('External links are not allowed in this menu. If you are indeed entering an internal link, make sure to use the internal Drupal path (ex: /node/2, /events, &lt;front&gt;, etc.) instead of the full URL. URL aliases are acceptable.'));
  }
}

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

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