You are here

function readonlymode_form_alter in Read only mode 8

Same name and namespace in other branches
  1. 6 readonlymode.module \readonlymode_form_alter()
  2. 7 readonlymode.module \readonlymode_form_alter()

Implements hook_form_alter().

Permit posting of content.

File

./readonlymode.module, line 135
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_alter(&$form, FormStateInterface $form_state, $form_id) {
  $settings = \Drupal::config('readonlymode.settings');
  if (!_readonlymode_form_check($form, $form_id, FALSE)) {

    // If a redirect URL is set, then we redirect to it.
    if ($url = $settings
      ->get('url')) {
      return new RedirectResponse($url);
    }
    else {

      // Remove FAPI #after_build handlers.
      $form['#after_build'] = [];

      // Remove all elements of the form.
      foreach (Element::children($form) as $key) {
        if (!in_array($key, [
          'form_id',
          'form_token',
          'form_build_id',
        ])) {
          unset($form[$key]);
        }
      }
      $form['readonly_notice'] = [
        '#markup' => \Drupal::token()
          ->replace($settings
          ->get('messages.default')),
        '#prefix' => '<div class="messages warning">',
        '#suffix' => '</div>',
      ];
    }
  }
  if (\Drupal::currentUser()
    ->hasPermission('readonlymode access forms') && $settings
    ->get('enabled')) {
    \Drupal::messenger()
      ->addMessage(t('The site is currently set to Read Only, content moderation is disabled for all users without the "Access all forms while in Read Only Mode" permission.'), 'warning', FALSE);
  }
  $form['#validate'][] = 'readonlymode_check_form_validate';
}