betasite-1.0.4/betasite.module

betasite.module
<?php

/**
 * @file
 * The main module file for the Beta Site module.
 */

use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_alter().
 *
 * Adds a beta_path field to all forms with a path field and
 * adds a submit callback to save or delete the beta path from the db.
 */
function betasite_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form_has_path_field = isset($form['path']);
  $user_is_admin = in_array('administrator', \Drupal::currentUser()->getRoles());
  $is_content_form = $form_state->getFormObject() instanceof ContentEntityForm;

  if (!$is_content_form || !$form_has_path_field || !$user_is_admin) {
    return;
  }

  // Load existing beta alias.
  $form_object = $form_state->getformObject();

  if (!($form_object instanceof EntityFormInterface)) {
    // Not an entity form, nothin to alter.
    return;
  }

  $default_alias = '';
  $nid = $form_object->getEntity()->id();
  if ($nid) {
    $beta_alias = \Drupal::service('betasite.beta_alias_storage')->getAlias($nid);
    $default_alias = $beta_alias;
  }

  // Add the field.
  $form['beta_path'] = _cbl_build_render_array($default_alias, $form['path']);

  // Add the submit handler to save the beta alias.
  $form['actions']['submit']['#submit'][] = '_cbl_save_beta_alias';
}

/**
 * Returns the render array for the beta alias field.
 *
 * @param string $default_alias
 *   If there is a beta alias for this node, populate the form with it.
 * @param array $path_field
 *   The $form['path'] from the form array.
 *   Used for matching weights and sources.
 */
function _cbl_build_render_array($default_alias, array $path_field) {
  $title = t('Beta URL alias');
  $descrip = t('Specify the path this node will use for the beta site. Leave it blank to use the main site path.');

  return [
    '#type' => 'container',
    '#access' => TRUE,
    '#weight' => $path_field['#weight'],
    '#parents' => ['beta_path_wrapper'],
    '#attributes' => [],
    '#cache' => [
      'contexts' => [],
      'tags' => [],
      'max-age' => -1,
    ],
    'widget' => [
      '#field_name' => 'beta_path',
      '#required' => FALSE,
      '#title' => $title,
      '#description' => '',
      '#theme' => 'field_multiple_value_form',
      '#cardinality' => 1,
      '#cardinality_multiple' => FALSE,
      '#max_delta' => 0,
      '#tree' => TRUE,
      '#parents' => ['beta_path'],
      '#after_build' => [],
      0 => [
        '#title' => $title,
        '#title_display' => 'before',
        '#description' => '',
        '#required' => FALSE,
        '#delta' => 0,
        '#element_validate' => ['_cbl_validate_beta_alias'],
        'source' => [
          '#type' => 'value',
          '#value' => $path_field['widget'][0]['source']['#value'],
        ],
        'langcode' => [
          '#type' => 'value',
          '#value' => 'en',
        ],
        '#type' => 'details',
        '#open' => FALSE,
        '#group' => 'advanced',
        '#access' => TRUE,
        '#weight' => $path_field['widget'][0]['#weight'],
        'alias' => [
          '#type' => 'textfield',
          '#title' => $title,
          '#default_value' => $default_alias,
          '#required' => FALSE,
          '#maxlength' => 255,
          '#description' => $descrip,
          '#states' => [],
          '#attached' => [],
        ],
      ],
    ],
  ];
}

/**
 * Validates the beta path for alias format and checks for duplicates.
 */
function _cbl_validate_beta_alias($element, FormStateInterface $form_state) {
  $alias = $form_state->getValue('beta_path')[0]['alias'];
  $old_alias = $element['alias']['#default_value'];

  // Alias has to exist and not match the old alias.
  if ($alias && $alias !== $old_alias) {
    if (substr($alias, 0, 1) !== '/' || strpos($alias, ' ') !== FALSE) {
      $form_state->setError($element, t('Beta alias must start with a / and have no spaces.'));
    }

    if (\Drupal::service('betasite.beta_alias_storage')->aliasExists($alias)) {
      $form_state->setError($element, t('This beta alias already exists.'));
    }
  }
}

/**
 * Save the beta_path.
 */
function _cbl_save_beta_alias(array $form, FormStateInterface $form_state) {
  $alias = $form_state->getValue('beta_path')[0]['alias'];
  $old_alias = $form['beta_path']['widget'][0]['alias']['#default_value'];
  // Don't bother if the alias hasn't changed.
  if ($alias === $old_alias) {
    return;
  }

  $form_object = $form_state->getformObject();

  if (!($form_object instanceof EntityFormInterface)) {
    // Not an entity form, nothin to alter.
    return;
  }

  $nid = $form_object->getEntity()->id();
  if ($alias) {
    \Drupal::service('betasite.beta_alias_storage')->saveAlias($nid, $alias);
  }
  else {
    \Drupal::service('betasite.beta_alias_storage')->deleteAlias($nid);
  }

  // Clear the routing cache so the new/updated beta alias can be added.
  \Drupal::service('router.builder')->rebuild();

  // Clear the menu caches so any links to this node are updated to
  // reflect the new/updated beta alias.
  \Drupal::cache('menu')->invalidateAll();
}

/**
 * Implements hook_page_attachments_alter().
 */
function betasite_page_attachments_alter(array &$page) {
  if (\Drupal::service('betasite.manager')->isBetaSite()) {
    $robots = [
      '#type' => 'html_tag',
      '#tag' => 'meta',
      '#attributes' => [
        'name' => 'robots',
        'content' => 'noindex, nofollow',
      ],
    ];
    $page['#attached']['html_head'][] = [$robots, 'robots'];
  }
}

/**
 * Implements hook_condition_info_alter().
 */
function betasite_condition_info_alter(array &$definitions) {
  $definitions['request_path']['class'] = '\Drupal\betasite\Plugin\Condition\BetaSiteRequestPath';
}

/**
 * Implements hook_robotstxt().
 */
function betasite_robotstxt() {
  if (\Drupal::service('betasite.manager')->isBetaSite()) {
    return [
      'Disallow: /',
    ];
  }
}

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

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