You are here

domain_menu_access.module in Domain Menu Access 8

Same filename and directory in other branches
  1. 6 domain_menu_access.module
  2. 7 domain_menu_access.module

Domain-based access control for menu link.

File

domain_menu_access.module
View source
<?php

/**
 * @file
 * Domain-based access control for menu link.
 */
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_ENTITY_TYPE_presave().
 *
 * Fires only if Devel Generate module is present, to assign test menu
 * link content to domains.
 */
function domain_menu_access_menu_link_content_presave(EntityInterface $node) {
  domain_access_presave_generate($node);
}

/**
 * Implements hook_form_BASE_FORM_ID_alter() for
 * \Drupal\menu_link_content\MenuLinkContentForm.
 *
 * Move Domain Access fields to an advanced tab like other node settings.
 */
function domain_menu_access_form_menu_link_content_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  /** @var \Drupal\menu_link_content\Form\MenuLinkContentForm $form_object */
  $form_object = $form_state
    ->getFormObject();

  /** @var \Drupal\menu_link_content\Entity\MenuLinkContent $entity */
  $entity = $form_object
    ->getEntity();
  $config = \Drupal::config('domain_menu_access.settings')
    ->get('menu_enabled');
  if (!empty($config) && in_array($entity
    ->getMenuName(), $config)) {
    $form['domain'] = [
      '#type' => 'details',
      '#title' => t('Domain'),
      '#open' => TRUE,
      '#weight' => 25,
    ];
    $form[DOMAIN_ACCESS_FIELD]['#group'] = 'domain';
    $form[DOMAIN_ACCESS_ALL_FIELD]['#group'] = 'domain';

    // Add the options hidden from the user silently to the form.
    $manager = \Drupal::service('domain.element_manager');
    $form = $manager
      ->setFormOptions($form, $form_state, DOMAIN_ACCESS_FIELD);
  }
  else {
    $form[DOMAIN_ACCESS_FIELD]['#access'] = FALSE;
    $form[DOMAIN_ACCESS_ALL_FIELD]['#access'] = FALSE;
  }
}

Functions