You are here

function permissions_by_term_form_taxonomy_term_form_alter in Permissions by Term 8

Same name and namespace in other branches
  1. 8.2 permissions_by_term.module \permissions_by_term_form_taxonomy_term_form_alter()

Implements hook_form_alter().

File

./permissions_by_term.module, line 112
Allows access to terms in a vocabulary to be limited by user or role.

Code

function permissions_by_term_form_taxonomy_term_form_alter(&$form, FormStateInterface $formState, $form_id) {
  if (\Drupal::currentUser()
    ->hasPermission('show term permission form on term page')) {
    $termId = $formState
      ->getFormObject()
      ->getEntity()
      ->id();

    /* @var \Drupal\permissions_by_term\Service\AccessStorage $access_storage */
    $access_storage = \Drupal::service('permissions_by_term.access_storage');
    $description = <<<EOT
To limit access to this term by user(s) or role(s), select users or roles above.
If left empty, all users will have access to content, related to this taxonomy term
and this taxonomy term itself.
EOT;
    $form['access'] = [
      '#type' => 'details',
      '#title' => t('Permissions'),
      '#description' => t($description),
      '#attributes' => [
        'id' => 'fieldset_term_access',
      ],
      '#weight' => -5,
      '#tree' => TRUE,
    ];
    $langcode = \Drupal::languageManager()
      ->getCurrentLanguage()
      ->getId();
    if (!empty($formState
      ->getValue('langcode'))) {
      $langcode = $formState
        ->getValue('langcode')['0']['value'];
    }
    $aAllowedUsers = $access_storage
      ->getAllowedUserIds($termId, $langcode);
    if (!empty($aAllowedUsers)) {
      $aAllowedUsers = user_load_multiple($aAllowedUsers);
      $sUserFormValue = $access_storage
        ->getUserFormValue($aAllowedUsers);
    }
    else {
      $sUserFormValue = NULL;
    }
    $description = <<<EOT
Enter a comma-separated list of user names who will be able to access content,
related to this taxonomy term.
EOT;

    // Note that the autocomplete widget will only enable for users with the
    // 'access profiles' permission. Other users will have to specify the name
    // manually.
    $form['access']['user'] = [
      '#type' => 'entity_autocomplete',
      '#target_type' => 'user',
      '#title' => t('Allowed users'),
      '#description' => t($description),
      '#value' => $sUserFormValue,
      '#size' => 60,
      '#autocomplete_route_name' => 'permissions_by_term.autocomplete_multiple',
      '#weight' => -10,
    ];
    $aAllowedRoles = $access_storage
      ->getRoleTermPermissionsByTid($termId, $langcode);

    // Firstly fetch all translated allowed role names.
    $aTranslatedAllowedRoleNames = [];
    foreach ($aAllowedRoles as $role) {
      $aTranslatedAllowedRoleNames[] = $role;
    }

    // Get all roles for the complete form and translate them.
    $aTranslatedUserRoles = [];
    $array_key_counter = 1;
    foreach (user_roles() as $user_role_id => $user_role_name) {
      $aTranslatedUserRoles[$user_role_id] = $user_role_name
        ->label();
      $array_key_counter++;
    }

    // Generate the default values for the form.
    $aSetRoles = [];
    if (!empty($aTranslatedAllowedRoleNames)) {
      foreach ($aTranslatedAllowedRoleNames as $role_name) {
        $aSetRoles[] = $role_name;
      }
    }
    $description = <<<EOT
Select user roles who will be able to access content, related to this taxonomy term.
EOT;

    // Now, lets do the Roles table.
    $form['access']['role'] = [
      '#type' => 'checkboxes',
      '#title' => t('Allowed roles'),
      '#description' => t($description),
      '#default_value' => $aSetRoles,
      '#options' => $aTranslatedUserRoles,
      '#multiple' => FALSE,
      '#weight' => 5,
    ];
    $form['#validate'][] = 'permissions_by_term_validate';
    $form['actions']['submit']['#submit'][] = 'permissions_by_term_submit';
  }
}