fixed_path_alias-8.x-1.0-beta2/fixed_path_alias.module

fixed_path_alias.module
<?php

/**
 * @file
 * Fixed Path Alias module.
 *
 * Exports path aliases to config to make them available in any environment.
 */

use Drupal\Core\Form\FormStateInterface;
use Drupal\path_alias\PathAliasInterface;

/**
 * Implements hook_form_FORM_ID_alter().
 */
function fixed_path_alias_form_path_alias_form_alter(&$form, FormStateInterface $form_state) {
  /** @var \Drupal\Core\Entity\ContentEntityFormInterface $form_object */
  $form_object = $form_state->getFormObject();
  /** @var \Drupal\path_alias\PathAliasInterface $path_alias_entity */
  $path_alias_entity = $form_object->getEntity();

  // Check if the edited alias is already fixed.
  $is_fixed = FALSE;
  if (!$path_alias_entity->isNew()) {
    $alias_value = $path_alias_entity->getAlias();
    $fixed_aliases = \Drupal::configFactory()
      ->get('fixed_path_alias.aliases')
      ->get('aliases') ?: [];
    foreach ($fixed_aliases as $alias) {
      if ($is_fixed = $alias['alias'] == $alias_value) {
        break;
      }
    }
  }

  // Fixed aliases are stored in config, can be only updated by administrator.
  if (\Drupal::currentUser()->hasPermission('administer site configuration')) {
    _fixed_path_alias_alias_form_add_fixed_option($form, $is_fixed);
  }
  elseif ($is_fixed) {
    _fixed_path_alias_disable_path_alias_form($form);
  }
}

/**
 * Add the fixed option to the alias form.
 *
 * @param array $form
 *   The form.
 * @param bool $is_fixed
 *   (optional) Fixed field default value.
 */
function _fixed_path_alias_alias_form_add_fixed_option(array &$form, $is_fixed = FALSE) {
  $form['fixed_path_alias'] = [
    '#type' => 'checkbox',
    '#title' => t('Fixed alias'),
    '#description' => t('When checked, the alias is exported to config and will be present in any application environment.'),
    '#default_value' => $is_fixed,
    '#weight' => 15,
  ];

  // Add form submit handler to process the fixed path option.
  $form['actions']['submit']['#submit'][] = '_fixed_path_alias_update_on_save';
}

/**
 * Disables all path alias form fields.
 *
 * @param array $form
 *   The form.
 */
function _fixed_path_alias_disable_path_alias_form(array &$form) {
  $form['alias']['widget'][0]['#disabled'] = TRUE;
  $form['path']['widget'][0]['#disabled'] = TRUE;
  if (isset($form['langcode'])) {
    $form['langcode']['widget'][0]['#disabled'] = TRUE;
  }
  \Drupal::messenger()->addWarning(t('This is a fixed alias and can only be updated by a system administrator.'));
}

/**
 * Implements hook_ENTITY_TYPE_delete().
 */
function fixed_path_alias_path_alias_delete(PathAliasInterface $path) {
  // Do nothing if the user cannot edit the site configuration.
  if (!\Drupal::currentUser()->hasPermission('administer site configuration')) {
    return;
  }

  // Remove the alias from the fixed path aliases config table.
  $config = \Drupal::configFactory()->getEditable('fixed_path_alias.aliases');
  $aliases = $config->get('aliases') ?: [];

  foreach ($aliases as $key => $alias) {
    if ($alias['alias'] == $path->getAlias()) {
      unset($aliases[$key]);

      $config->set('aliases', $aliases);
      $config->save();
      break;
    }
  }
}

/**
 * Path alias form submit handler. Updates configuration with fixed aliases.
 *
 * @param array $form
 *   The submitted form.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The form state.
 */
function _fixed_path_alias_update_on_save(array &$form, FormStateInterface $form_state) {
  /** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
  $form_object = $form_state->getFormObject();
  /** @var \Drupal\path_alias\PathAliasInterface $path_alias_entity */
  $path_alias_entity = $form_object->getEntity();

  // Reads the configured fixed aliases.
  $config = \Drupal::configFactory()->getEditable('fixed_path_alias.aliases');
  $aliases = $config->get('aliases') ?: [];

  $new_alias = [
    'alias' => $path_alias_entity->getAlias(),
    'source' => $path_alias_entity->getPath(),
    'langcode' => $path_alias_entity->langcode->value,
  ];

  // Search for current config alias.
  if ($old_alias_path = $form['alias']['widget'][0]['value']['#default_value']) {
    foreach ($aliases as $key => $alias) {
      if ($old_alias_path == $alias['alias']) {
        $current_alias = $alias;
        break;
      }
    }
  }

  if ($form_state->getValue('fixed_path_alias')) {
    if (isset($current_alias)) {
      // Update.
      if ($updated = array_diff($new_alias, $current_alias)) {
        $aliases[$key] = $new_alias;
      }
    }
    else {
      // Add.
      $aliases[] = $new_alias;
      $updated = TRUE;
    }
  }
  else {
    if ($updated = isset($current_alias)) {
      // Delete.
      unset($aliases[$key]);
    }
  }

  if ($updated) {
    $config->set('aliases', $aliases);
    $config->save();
  }
}

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

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