You are here

class SchemeForm in Taxonomy Access Control Lite 8

Builds the scheme configuration form.

Hierarchy

Expanded class hierarchy of SchemeForm

3 files declare their use of SchemeForm
tac_lite.module in ./tac_lite.module
Control access to site content based on taxonomy, roles and users.
tac_lite_create.module in tac_lite_create/tac_lite_create.module
Control term visibility in node edit form.
UserAccessForm.php in src/Form/UserAccessForm.php

File

src/Form/SchemeForm.php, line 14

Namespace

Drupal\tac_lite\Form
View source
class SchemeForm extends ConfigFormBase {
  private $scheme;

  /**
   * {@inheritdoc}
   */
  public function getEditableConfigNames() {
    return [
      'tac_lite.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'tac_lite_admin_scheme_form';
  }

  /**
   * {@inheritdoc}
   */
  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);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state, $scheme = NULL) {
    $scheme = $this->scheme;
    $values = $form_state
      ->getValues();
    $this
      ->config('tac_lite.settings')
      ->set('menu_rebuild_needed', TRUE)
      ->set('tac_lite_config_scheme_' . $scheme, $values['tac_lite_config_scheme_' . $scheme])
      ->set('tac_lite_grants_scheme_' . $scheme, $values['tac_lite_grants_scheme_' . $scheme])
      ->save();
    if ($values['tac_lite_rebuild']) {
      node_access_rebuild(TRUE);
    }
    else {
      $this
        ->messenger()
        ->addWarning($this
        ->t('Do not forget to <a href=:url>rebuild node access permissions</a> after you have configured taxonomy-based access.', [
        ':url' => Url::fromRoute('node.configure_rebuild_confirm')
          ->toString(),
      ]));
    }
    parent::submitForm($form, $form_state);
  }

  /**
   * Helper function to get configuration of scheme.
   */
  public static function tacLiteConfig($scheme) {
    $settings = \Drupal::config('tac_lite.settings');
    $config = $settings
      ->get('tac_lite_config_scheme_' . $scheme);
    $config['name'] = !empty($config['name']) ? $config['name'] : NULL;
    $config['perms'] = !empty($config['perms']) ? $config['perms'] : [];
    $config += [
      'term_visibility' => isset($config['perms']['grant_view']) && $config['perms']['grant_view'],
      'unpublished' => FALSE,
      'realm' => 'tac_lite_scheme_' . $scheme,
    ];
    return $config;
  }

  /**
   * Helper function to build a taxonomy term select element for a form.
   *
   * @param object $v
   *   A vocabulary object containing a vid and name.
   * @param array $default_values
   *   An array of values to use for the default_value argument for this
   *   form element.
   */
  public static function tacLiteTermSelect($v, $default_values = []) {
    $tree = \Drupal::service('entity_type.manager')
      ->getStorage('taxonomy_term')
      ->loadTree($v
      ->get('vid'));
    $options = [
      0 => '<none>',
    ];
    if ($tree) {
      foreach ($tree as $term) {
        $choice = new \stdClass();
        $choice->option = [
          $term->tid => str_repeat('-', $term->depth) . $term->name,
        ];
        $options[] = $choice;
      }
    }
    $field_array = [
      '#type' => 'select',
      '#title' => $v
        ->get('name'),
      '#default_value' => $default_values,
      '#options' => $options,
      '#multiple' => TRUE,
      '#description' => $v
        ->get('description'),
    ];
    return $field_array;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 13
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
SchemeForm::$scheme private property
SchemeForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SchemeForm::getEditableConfigNames public function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SchemeForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SchemeForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SchemeForm::tacLiteConfig public static function Helper function to get configuration of scheme.
SchemeForm::tacLiteTermSelect public static function Helper function to build a taxonomy term select element for a form.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.