You are here

config_readonly.module in Configuration Read-only mode 8

Same filename and directory in other branches
  1. 7 config_readonly.module

File

config_readonly.module
View source
<?php

/**
 * @file
 * Contains config_readonly.module.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Site\Settings;
use Drupal\config_readonly\ReadOnlyFormEvent;

/**
 * Implements hook_form_alter().
 */
function config_readonly_form_alter(array &$form, FormStateInterface &$form_state) {
  if (!Settings::get('config_readonly')) {
    return;
  }
  $event = new ReadOnlyFormEvent($form_state);
  \Drupal::service('event_dispatcher')
    ->dispatch(ReadOnlyFormEvent::NAME, $event);
  if ($event
    ->isFormReadOnly()) {
    \Drupal::messenger()
      ->addWarning('This form will not be saved because the configuration active store is read-only.');
    $form['#validate'][] = '_config_readonly_validate_failure';
    if (isset($form['actions']['submit'])) {
      $form['actions']['submit']['#disabled'] = TRUE;
    }
  }
}

/**
 * Helper validation function that always returns false.
 *
 * @param array $form
 *   A build form array.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The form state.
 */
function _config_readonly_validate_failure(array $form, FormStateInterface &$form_state) {
  $form_state
    ->setErrorByName(NULL, t('This configuration form cannot be saved because the configuration active store is read-only.'));
}

/**
 * Implements hook_config_readonly_whitelist_patterns().
 */
function config_readonly_config_readonly_whitelist_patterns() {
  return Settings::get('config_readonly_whitelist_patterns');
}

Functions

Namesort descending Description
config_readonly_config_readonly_whitelist_patterns Implements hook_config_readonly_whitelist_patterns().
config_readonly_form_alter Implements hook_form_alter().
_config_readonly_validate_failure Helper validation function that always returns false.