You are here

function readonlymode_form_system_site_maintenance_mode_alter in Read only mode 8

Same name and namespace in other branches
  1. 7 readonlymode.module \readonlymode_form_system_site_maintenance_mode_alter()
  2. 2.0.x readonlymode.module \readonlymode_form_system_site_maintenance_mode_alter()

Implements hook_form_FORM_ID_alter().

File

./readonlymode.module, line 23
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…

Code

function readonlymode_form_system_site_maintenance_mode_alter(&$form, FormStateInterface $form_state) {
  $settings = \Drupal::config('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' => $settings
      ->get('enabled'),
  ];

  // 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 when viewing a page that has a blocked form 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 blocked form is submitted while in Read Only Mode. This scenario occurs when a user 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['read_only']['settings']['url'] = [
    '#type' => 'textfield',
    '#title' => t('Redirect path'),
    '#description' => t('When given, Drupal will redirect the user to this URL when a user tries to add/edit content instead of displaying the message above.'),
    '#default_value' => $settings
      ->get('url'),
  ];

  // Allowed forms configuration is in a collapsed fieldset
  // so that it doesn't clutter the display.
  $form['read_only']['forms'] = [
    '#title' => t('Allowed forms'),
    '#type' => 'details',
    '#description' => t('Configure which forms will be excluded from restriction when in read-only mode.'),
  ];
  $form['read_only']['forms']['additional_edit'] = [
    '#type' => 'textarea',
    '#title' => t('Forms that can be submitted'),
    '#description' => t("These forms are not restricted when in read only mode. Enter one form id per line. You may use the wildcard character '*' to use loose matches. For example: webform* will match all webforms. Note that the following forms will always be allowed: %allowed_forms.", [
      '%allowed_forms' => empty($settings
        ->get('forms.default.edit')) ? '' : implode(', ', $settings
        ->get('forms.default.edit')),
    ]),
    '#default_value' => $settings
      ->get('forms.additional.edit'),
  ];
  $form['read_only']['forms']['additional_view'] = [
    '#type' => 'textarea',
    '#title' => t('Forms that can be viewed'),
    '#description' => t("These forms are allowed to be viewed but will not accept form submissions. Enter one form id per line. You may use the wildcard character '*' to use loose matches. For example: webform* will match all webforms. Note that the following forms will always be allowed: %allowed_forms.", [
      '%allowed_forms' => empty($settings
        ->get('forms.default.view')) ? '' : implode(', ', $settings
        ->get('forms.default.view')),
    ]),
    '#default_value' => $settings
      ->get('forms.additional.view'),
  ];
  $form['#validate'][] = 'readonlymode_settings_form_validate';
  $form['#submit'][] = 'readonlymode_settings_form_submit';
}