You are here

readonlymode.module in Read only mode 2.0.x

Same filename and directory in other branches
  1. 8 readonlymode.module
  2. 6 readonlymode.module
  3. 7 readonlymode.module

The readonlymode module file.

Read Only Mode provides an alternative to the built in 'Maintenance Mode' in Drupal. Instead of displaying a static text file to users while the site is in maintenance mode, Read Only Mode will allow access (reading) of existing content while preventing changing or adding content (posting / submitting. forms / etc).

This allows the site to remain functional while maintenance is performed. This module also provides messaging to users and administrators to indicate that the site is in maintenance mode.

File

readonlymode.module
View source
<?php

/**
 * @file
 * The readonlymode module file.
 *
 * Read Only Mode provides an alternative to the built in 'Maintenance Mode' in
 * Drupal. Instead of displaying a static text file to users while the site is
 * in maintenance mode, Read Only Mode will allow access (reading) of existing
 * content while preventing changing or adding content (posting / submitting.
 * forms / etc).
 *
 * This allows the site to remain functional while maintenance is performed.
 * This module also provides messaging to users and administrators to indicate
 * that the site is in maintenance mode.
 */
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_entity_type_alter().
 */
function readonlymode_entity_type_alter(array &$entity_types) {

  // Add the readonly constraint to all entity types.
  foreach ($entity_types as &$entity_type) {
    $entity_type
      ->addConstraint('ReadonlymodeConstraint');
  }
}

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

  /** @var \Drupal\readonlymode\ReadonlymodeManager $manager */
  $manager = \Drupal::service('readonlymode.manager');

  // Get editable settings.
  $settings = \Drupal::configFactory()
    ->getEditable('readonlymode.settings');
  $form['read_only'] = [
    '#title' => t('Read Only Mode'),
    '#type' => 'details',
    '#weight' => 1,
    '#open' => TRUE,
  ];
  $form['read_only']['enable_readonly'] = [
    '#type' => 'checkbox',
    '#title' => t('Enable "Read Only" mode'),
    '#description' => t('When set to "Read Only", all content moderation (add/edit) will be impossible.'),
    '#weight' => 0,
    '#default_value' => $manager
      ->isReadonly(),
  ];

  // Message configuration is in a collapsed fieldset
  // so that it doesn't clutter the display.
  $form['read_only']['settings'] = [
    '#title' => t('Messages and redirects'),
    '#type' => 'details',
    '#description' => t('Configure the redirect URL and messages to display to users while the site is in Read Only Mode.'),
  ];
  $form['read_only']['settings']['default_message'] = [
    '#type' => 'textarea',
    '#title' => t('Read Only Mode warning'),
    '#description' => t('This warning will be displayed to users with the appropriate permission while in Read Only Mode.'),
    '#default_value' => $settings
      ->get('messages.default'),
    '#rows' => 3,
    '#required' => TRUE,
  ];
  $form['read_only']['settings']['not_saved_message'] = [
    '#type' => 'textarea',
    '#title' => t('Form submission error'),
    '#description' => t('This error will be displayed when a entity is edited or created while in Read Only Mode. This scenario occurs when a user ignores the warning or starts filling out a form during normal site operation and then attempts to submit the form after Read Only Mode has been enabled.'),
    '#default_value' => $settings
      ->get('messages.not_saved'),
    '#rows' => 3,
    '#required' => TRUE,
  ];
  $form['#submit'][] = 'readonlymode_settings_form_submit';
}

/**
 * Settings form submission handler.
 *
 * See readonlymode_form_system_site_maintenance_mode_alter().
 */
function readonlymode_settings_form_submit(array &$form, FormStateInterface $form_state) {

  // Save the activation state.
  \Drupal::service('readonlymode.manager')
    ->setReadonly($form_state
    ->getValue('enable_readonly', FALSE));

  // Update the config with the messages if necessary.
  $config = \Drupal::configFactory()
    ->getEditable('readonlymode.settings');
  $updated = FALSE;
  $settings = [
    'messages.default' => 'default_message',
    'messages.not_saved' => 'not_saved_message',
  ];
  foreach ($settings as $key => $element) {
    if ($config
      ->get($key) != $form_state
      ->getValue($element)) {
      $config
        ->set($key, $form_state
        ->getValue($element));
      $updated = TRUE;
    }
  }
  if ($updated) {
    $config
      ->save();
  }
}