You are here

config_readonly.module in Configuration Read-only mode 7

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

File

config_readonly.module
View source
<?php

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Entity\EntityFormControllerInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Config\Entity\ConfigEntityListController;

/**
 * Implements hook_form_alter().
 */
function config_readonly_form_alter(array &$form, array &$form_state) {

  // If settings.php doesn't say to lock config changes, then don't do anything.
  if (!settings()
    ->get('config_readonly')) {
    return;
  }

  // Check if the form is a ConfigFormBase or a ConfigEntityListController.
  $form_object = $form_state['build_info']['callback_object'];
  $is_config_form = $form_object instanceof ConfigFormBase || $form_object instanceof ConfigEntityListController;

  // Check if the form is an EntityFormControllerInterface and if the entity is
  // a config entity.
  if (!$is_config_form && isset($form_state['controller']) && $form_state['controller'] instanceof EntityFormControllerInterface) {
    $is_config_form = $form_state['controller']
      ->getEntity() instanceof ConfigEntityInterface;
  }

  // Display a message informing the user that this form cannot be saved.
  if ($is_config_form) {
    drupal_set_message('This form will not be saved because the configuration active store is read-only.', 'warning');
    $form['#validate'][] = '_config_readonly_validate_failure';
  }
}

/**
 * Helper validation function that always returns false.
 */
function _config_readonly_validate_failure(array $form, array &$form_state) {
  \Drupal::formBuilder()
    ->setErrorByName(NULL, $form_state, t('This configuration form cannot be saved because the configuration active store is read-only.'));
}

Functions

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