You are here

domain.module in Domain Access 8

Defines a Domain concept for use with Drupal.

File

domain/domain.module
View source
<?php

/**
 * @file
 * Defines a Domain concept for use with Drupal.
 */
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Url;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Component\Utility\Html;
use Drupal\domain\DomainInterface;

/**
 * The name of the admin access control field.
 *
 * @deprecated This constant will be replaced in the final release by
 * Drupal\domain\DomainInterface::DOMAIN_ADMIN_FIELD.
 */
const DOMAIN_ADMIN_FIELD = 'field_domain_admin';

/**
 * Entity URI callback.
 *
 * @param \Drupal\domain\DomainInterface $domain
 *   The Domain object.
 *
 * @return \Drupal\Core\Url
 *   The Domain URL.
 */
function domain_uri(DomainInterface $domain) {
  return Url::fromUri($domain
    ->getPath(), [
    'absolute' => TRUE,
  ]);
}

/**
 * Implements hook_entity_load().
 *
 * The $domain->path and $domain->uri properties are derived from data in the
 * {domain} table. We use the hook system to load that data to indicate that
 * the data is not native to the object.
 *
 * This action is performed in hook_entity_load(), which precedes the running
 * of hook_domain_load() and ensures that our data is present for other modules.
 */
function domain_entity_load(array $entities, $entity_type) {
  if ($entity_type == 'domain') {
    foreach ($entities as $domain) {
      $domain
        ->setPath();
      $domain
        ->setUrl();
    }
  }
}

/**
 * Implements hook_help().
 */
function domain_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'domain.admin':
      $output = t('<p>The following domains have been created for your site.  The currently active domain
                     <strong>is shown in boldface</strong>. You may click on a domain to change the currently active domain.
                     </p>');
      return $output;
  }
}

/**
 * Implements hook_token_info().
 */
function domain_token_info() {
  return \Drupal::service('domain.token')
    ->getTokenInfo();
}

/**
 * Implements hook_tokens().
 */
function domain_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  return \Drupal::service('domain.token')
    ->getTokens($type, $tokens, $data, $options, $bubbleable_metadata);
}

/**
 * Implements hook_preprocess_HOOK() for html.html.twig.
 */
function domain_preprocess_html(array &$variables) {

  // Add class to body tag, if set.
  $config = \Drupal::config('domain.settings');
  if ($string = $config
    ->get('css_classes')) {
    $token = \Drupal::token();

    // Prepare the classes proparly, with one class per string.
    $classes = explode(' ', trim($string));
    foreach ($classes as $class) {

      // Ensure no leading or trailing space.
      $class = trim($class);
      if (!empty($class)) {
        $variables['attributes']['class'][] = Html::getClass($token
          ->replace($class));
      }
    }
  }
}

/**
 * Implements hook_form_BASE_FORM_ID_alter() for \Drupal\user\UserForm.
 *
 * Handle settings that the user cannot access.
 */
function domain_form_user_form_alter(&$form, &$form_state, $form_id) {

  // Add the options hidden from the user silently to the form.
  $manager = \Drupal::service('domain.element_manager');
  $form = $manager
    ->setFormOptions($form, $form_state, DomainInterface::DOMAIN_ADMIN_FIELD);
}

/**
 * Implements hook_domain_references_alter().
 */
function domain_domain_references_alter($query, $account, $context) {

  // Restrict domains by assignment, being sure only to act on the admin field.
  if ($context['field_type'] == 'admin' && $context['entity_type'] == 'user') {
    if ($account
      ->hasPermission('administer domains')) {

      // Do nothing.
    }
    elseif ($account
      ->hasPermission('assign domain administrators')) {
      $allowed = \Drupal::service('domain.element_manager')
        ->getFieldValues($account, DomainInterface::DOMAIN_ADMIN_FIELD);
      $query
        ->condition('id', array_keys($allowed), 'IN');
    }
    else {

      // Remove all options.
      $query
        ->condition('id', '-no-possible-match-');
    }
  }
}

/**
 * Implements hook_views_data_alter().
 */
function domain_views_data_alter(array &$data) {
  $table = 'user__' . DomainInterface::DOMAIN_ADMIN_FIELD;

  // Since domains are not stored in the database, relationships cannot be used.
  unset($data[$table][DomainInterface::DOMAIN_ADMIN_FIELD]['relationship']);
}

/**
 * Implements hook_theme().
 */
function domain_theme() {
  return [
    'domain_nav_block' => [
      'render element' => 'items',
    ],
  ];
}

/**
 * Prepares variables for block templates.
 *
 * Default template: domain-nav-block.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - items: An array of labels and urls for use in the list.
 *     Properties used: 'label', 'url', 'active'.
 */
function template_preprocess_domain_nav_block(array &$variables) {
  $variables['items'] = $variables['items']['#items'];
}

/**
 * Implements hook_hook_info().
 */
function domain_hook_info() {
  $hooks['domain_request_alter'] = [
    'group' => 'domain',
  ];
  $hooks['domain_validate_alter'] = [
    'group' => 'domain',
  ];
  $hooks['domain_references_alter'] = [
    'group' => 'domain',
  ];
  return $hooks;
}

Functions

Constants

Namesort descending Description
DOMAIN_ADMIN_FIELD Deprecated The name of the admin access control field.