You are here

function permissions_by_term_form_taxonomy_term_form_alter in Permissions by Term 8.2

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

Implements hook_form_alter().

File

./permissions_by_term.module, line 129
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')) {
    $term = $formState
      ->getFormObject()
      ->getEntity();
    $settings = \Drupal::config('permissions_by_term.settings');
    $target_bundles = $settings
      ->get('target_bundles');

    // Check if permissions can be managed for this taxonomy term bundle.
    if ($target_bundles && !in_array($term
      ->bundle(), $target_bundles)) {
      return;
    }
    $termId = $term
      ->id();
    $settings = \Drupal::config('permissions_by_term.settings');
    $target_bundles = $settings
      ->get('target_bundles');

    // Check if permissions can be managed for this taxonomy term bundle.
    if ($target_bundles && !in_array($term
      ->bundle(), $target_bundles)) {
      return;
    }

    /* @var \Drupal\permissions_by_term\Service\AccessStorage $access_storage */
    $access_storage = \Drupal::service('permissions_by_term.access_storage');
    $form['access'] = [
      '#type' => 'details',
      '#title' => t('Permissions'),
      '#description' => t('To limit access to this term by user(s) or role(s), select users or roles below. If left empty, all users will have access to content, related to this taxonomy term and this taxonomy term itself.'),
      '#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;
    }

    // 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('Enter a comma-separated list of user names who will be able to access content, related to this taxonomy term.'),
      '#value' => $sUserFormValue,
      '#size' => 60,
      '#maxlength' => FALSE,
      '#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;
      }
    }

    // Now, lets do the Roles table.
    $form['access']['role'] = [
      '#type' => 'checkboxes',
      '#title' => t('Allowed roles'),
      '#description' => t('Select user roles who will be able to access content, related to this taxonomy term.'),
      '#default_value' => $aSetRoles,
      '#options' => $aTranslatedUserRoles,
      '#multiple' => FALSE,
      '#weight' => 5,
    ];
    $form['#validate'][] = 'permissions_by_term_validate';
    $form['actions']['submit']['#submit'][] = 'permissions_by_term_submit';
  }
}