You are here

config_split_ignore.module in Configuration Split Ignore 8

Hooks implemented by the Configuration Split Ignore module.

File

config_split_ignore.module
View source
<?php

/**
 * @file
 * Hooks implemented by the Configuration Split Ignore module.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Config\StorageComparer;
use Drupal\Core\Url;

/**
 * Implements hook_form_FORM_ID_alter().
 */
function config_split_ignore_form_config_split_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $config_split */
  $config_split = $form_state
    ->getFormObject()
    ->getEntity();
  $description = t('One configuration name per line.<br />
Examples: <ul>
<li>contact.settings</li>
<li>webform.webform.* (will ignore all config entities that starts with <em>webform.webform</em>)</li>
<li>*.contact_message.custom_contact_form.* (will ignore all config entities that starts with <em>.contact_message.custom_contact_form.</em> like fields attached to a custom contact form)</li>
<li>~webform.webform.contact (will force import for this configuration, even if ignored by a wildcard)</li>
<li>!webform.webform.contact (will delete this configuration even if it is defined somewhere else)</li>
<li>webform.webform.contact:title (will ignore the title of the webform, but will not ignore other webform.webform.contact configuration.)</li>
</ul>');
  $form['config_split_ignore_fieldset'] = [
    '#type' => 'fieldset',
    '#title' => t('Configuration Split Ignore Settings'),
    '#description' => t('These configuration entities will be ignored when exported or imported, they are deleted when the split becomes inactive. In most cases they are a subset of configuration entities listed in the Complete Split section.'),
  ];
  $form['config_split_ignore_fieldset']['config_split_ignore'] = [
    '#type' => 'textarea',
    '#rows' => 10,
    '#title' => t('Configuration entity names to ignore'),
    '#description' => $description,
    '#default_value' => implode("\n", $config_split
      ->getThirdPartySetting('config_split_ignore', 'entities', [])),
    '#size' => 60,
  ];
  $form['#entity_builders'][] = 'config_split_ignore_config_split_edit_form_builder';
}

/**
 * Entity builder for the Configuration Split config entity.
 */
function config_split_ignore_config_split_edit_form_builder($entity_type, ConfigEntityInterface $config_split, &$form, FormStateInterface $form_state) {
  $entities_str = trim($form_state
    ->getValue('config_split_ignore'));
  $entities = [];
  if (!empty($entities_str)) {
    $entities = array_values(array_filter(array_map('trim', preg_split("[\n|\r]", $entities_str))));
  }
  if (!empty($entities)) {
    $config_split
      ->setThirdPartySetting('config_split_ignore', 'entities', $entities);
  }
  else {
    $config_split
      ->unsetThirdPartySetting('config_split_ignore', 'entities');
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function config_split_ignore_form_config_admin_import_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  /** @var \Drupal\Core\Entity\EntityStorageInterface $entity_storage */
  $entity_storage = \Drupal::service('entity_type.manager')
    ->getStorage('config_split');

  /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
  $config_factory = \Drupal::service('config.factory');

  // Load the list of ignored entities from enabled splits.
  $ignored = [];

  /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $config_split */
  foreach ($entity_storage
    ->loadMultiple() as $config_split) {
    $config_name = $config_split
      ->getConfigDependencyName();
    $config = $config_factory
      ->get($config_name);
    if (!empty($config
      ->get('status'))) {
      $ignored = array_merge($ignored, $config_split
        ->getThirdPartySetting('config_split_ignore', 'entities', []));
    }
  }
  $ignored = array_unique($ignored);

  // Allow modules to alter the list of ignored entities.
  \Drupal::service('module_handler')
    ->invokeAll('config_split_ignore_settings_alter', [
    &$ignored,
  ]);
  $rows = [];
  foreach (array_unique($ignored) as $name) {
    $rows[] = [
      $name,
    ];
  }

  // Build a table of ignored config entities and keys.
  if (!empty($ignored)) {
    $form['split_ignored'] = [
      '#type' => 'table',
      '#header' => [
        'Config name',
      ],
      '#caption' => t('<h3>The following configuration entities and keys are ignored due to the <a href="@url">Configuration Split Ignore Settings</a> and therefore not displayed in the list above</h3>', [
        '@url' => Url::fromRoute('entity.config_split.collection')
          ->toString(),
      ]),
      '#rows' => $rows,
    ];
  }
}