You are here

function book_access_form_alter in Book access 1.x

Same name and namespace in other branches
  1. 5 book_access.module \book_access_form_alter()
  2. 6.2 book_access.module \book_access_form_alter()
  3. 6 book_access.module \book_access_form_alter()
  4. 7.2 book_access.module \book_access_form_alter()

Implements hook_form_alter().

See also

book_outline_form()

book_access_outline_form_submit()

book_access_edit_form_submit()

File

./book_access.module, line 137

Code

function book_access_form_alter(&$form, FormStateInterface &$form_state, $form_id) {

  /* @var \Drupal\book_access\BookAccessHelper $bookAccessHelper */
  $bookAccessHelper = \Drupal::service('book_access.book_access_helper');
  if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] . '_node_form' == $form_id) {
    if (isset($form['book']['bid']['#options'])) {

      // BookAccess::restrictOptions($form['book']['bid']['#options']);.
    }
    $form['#submit'][] = 'book_access_edit_form_submit';
  }
  elseif ($form_id == 'book_outline_form') {
    if (isset($form['book']['bid']['#options'])) {

      // BookAccess::restrictOptions($form['book']['bid']['#options']);.
    }
    $form['#submit'][] = 'book_access_outline_form_submit';
    if (isset($form['remove'])) {
      $form['remove']['#submit'][] = 'book_access_edit_form_submit';
    }
  }
  elseif ($form_id == 'book_admin_settings') {
    $form['bad_fs'] = [
      '#type' => 'fieldset',
      '#title' => t('Book Access Settings'),
      '#description' => t('These are the default access rules for authors and roles. Users can only be specified on a per-book basis.'),
    ];
    $form['bad_fs']['book_access_defaults'] = [
      '#type' => 'table',
      '#caption' => t('Warning: these defaults only apply to books that have yet to be created. Books that already exist must be manually changed.'),
      '#header' => [
        t('Access Type'),
        t('View Book'),
        t('Edit Pages'),
        t('Delete Pages'),
        t('Administer Access'),
        t('Add Child Pages'),
        t('Edit Book Outlines'),
      ],
    ];
    $author_defaults = $bookAccessHelper
      ->getAuthorBookAccessDefaults();
    $grants = $author_defaults
      ->getGrants();
    $form['bad_fs']['book_access_defaults']['book_access_author_defaults'] = [
      'book_access_author_defaults_access_type' => [
        '#markup' => t("Default author's access"),
      ],
    ];
    foreach ($grants as $grant_type => $grant) {
      $form['bad_fs']['book_access_defaults']['book_access_author_defaults'][$grant_type] = [
        '#type' => 'checkbox',
        '#title' => t('Author %grant_type', [
          '%grant_type' => $grant_type,
        ]),
        '#title_display' => 'invisible',
        '#default_value' => $grant,
      ];
    }
    $role_defaults = $bookAccessHelper
      ->getRoleBookAccessDefaults();
    foreach ($role_defaults as $id => $role_default) {
      $grants = $role_default['default_grants']
        ->getGrants();
      $form['bad_fs']['book_access_defaults'][$id] = [
        "{$id}_access_type" => [
          '#markup' => t("Default access for %role", [
            '%role' => $role_default['label'],
          ]),
        ],
      ];
      foreach ($grants as $grant_type => $grant) {
        $form['bad_fs']['book_access_defaults'][$id][$grant_type] = [
          '#type' => 'checkbox',
          '#title' => t('%role_label %grant_type', [
            '%role_label' => $role_default['label'],
            '%grant_type' => $grant_type,
          ]),
          '#title_display' => 'invisible',
          '#default_value' => $grant,
        ];
      }
    }
    $form['#submit'][] = 'book_access_admin_settings_submit';
  }
}