You are here

public function SchemeForm::buildForm in Taxonomy Access Control Lite 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/SchemeForm.php, line 33

Class

SchemeForm
Builds the scheme configuration form.

Namespace

Drupal\tac_lite\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $scheme = NULL) {
  $settings = $this->configFactory
    ->get('tac_lite.settings');
  $this->scheme = $scheme;
  $vids = $settings
    ->get('tac_lite_categories');
  $roles = user_roles();
  $config = self::tacLiteConfig($scheme);
  $form['#tac_lite_config'] = $config;
  if (count($vids)) {
    $form['tac_lite_config_scheme_' . $scheme] = [
      '#tree' => TRUE,
    ];
    $form['tac_lite_config_scheme_' . $scheme]['name'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Scheme name'),
      '#description' => $this
        ->t("A human-readable name for administrators to see. For example, 'read' or 'read and write'."),
      '#default_value' => $config['name'],
      '#required' => TRUE,
    ];

    // Currently, only view, update and delete are supported by node_access.
    $options = [
      'grant_view' => 'view',
      'grant_update' => 'update',
      'grant_delete' => 'delete',
    ];
    $form['tac_lite_config_scheme_' . $scheme]['perms'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Permissions'),
      '#multiple' => TRUE,
      '#options' => $options,
      '#default_value' => $config['perms'],
      '#description' => $this
        ->t('Select which permissions are granted by this scheme.  <br/>Note when granting update, it is best to enable visibility on all terms.  Otherwise a user may unknowingly remove invisible terms while editing a node.'),
      '#required' => FALSE,
    ];
    $form['tac_lite_config_scheme_' . $scheme]['unpublished'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Apply to unpublished content'),
      '#description' => $this
        ->t('If checked, permissions in this scheme will apply to unpublished content.  If this scheme includes the view permission, then <strong>unpublished nodes will be visible</strong> to users whose roles would grant them access to the published node.'),
      '#default_value' => $config['unpublished'],
    ];
    $form['tac_lite_config_scheme_' . $scheme]['term_visibility'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Visibility'),
      '#description' => $this
        ->t('If checked, this scheme determines whether a user can view <strong>terms</strong>.  Note the <em>view</em> permission in the select field above refers to <strong>node</strong> visibility.  This checkbox refers to <strong>term</strong> visibility, for example in a content edit form or tag cloud.'),
      '#default_value' => $config['term_visibility'],
    ];
    $form['helptext'] = [
      '#type' => 'markup',
      '#markup' => $this
        ->t('To grant to an individual user, visit the <em>access by taxonomy</em> tab on the account edit page.'),
      '#prefix' => '<p>',
      '#suffix' => '</p>',
    ];
    $form['helptext2'] = [
      '#type' => 'markup',
      '#markup' => $this
        ->t('To grant by role, select the terms below.'),
      '#prefix' => '<p>',
      '#suffix' => '</p>',
    ];
    $vocabularies = Vocabulary::loadMultiple();
    $all_defaults = $settings
      ->get('tac_lite_grants_scheme_' . $scheme);
    $form['tac_lite_grants_scheme_' . $scheme] = [
      '#tree' => TRUE,
    ];
    foreach ($roles as $rid => $role) {
      $role_name = $role
        ->get('label');
      $form['tac_lite_grants_scheme_' . $scheme][$rid] = [
        '#type' => 'details',
        '#tree' => TRUE,
        '#title' => Html::escape($this
          ->t('Grant permission by role: :role', [
          ':role' => $role_name,
        ])),
        '#open' => TRUE,
      ];
      $defaults = isset($all_defaults[$rid]) ? $all_defaults[$rid] : NULL;
      foreach ($vids as $vid) {

        // Build a taxonomy select form element for this vocab.
        $v = $vocabularies[$vid];
        $default_values = isset($defaults[$vid]) ? $defaults[$vid] : NULL;
        $form['tac_lite_grants_scheme_' . $scheme][$rid][$vid] = self::tacLiteTermSelect($v, $default_values);
      }
    }
    $form['tac_lite_rebuild'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Rebuild content permissions now'),
      // Default false because usually only needed after scheme has
      // been changed.
      '#default_value' => FALSE,
      '#description' => $this
        ->t('Do this once, after you have fully configured access by taxonomy.'),
      '#weight' => 9,
    ];
  }
  else {
    $form['tac_lite_help'] = [
      '#type' => 'markup',
      '#prefix' => '<p>',
      '#suffix' => '</p>',
      '#markup' => $this
        ->t('First, select one or more vocabularies on the <a href=:url>settings tab</a>. Then, return to this page to complete configuration.', [
        ':url' => Url::fromRoute('tac_lite.administration')
          ->toString(),
      ]),
    ];
  }
  return parent::buildForm($form, $form_state);
}