You are here

public static function ContentAccessRoleBasedFormTrait::disableCheckboxes in Content Access 8

Checkboxes access for content.

Formapi #process callback, that disables checkboxes for roles without access to content.

File

src/Form/ContentAccessRoleBasedFormTrait.php, line 88

Class

ContentAccessRoleBasedFormTrait
Common components for Content Access forms.

Namespace

Drupal\content_access\Form

Code

public static function disableCheckboxes(&$element, FormStateInterface $form_state, &$complete_form) {
  $access_roles = content_access_get_permission_access('access content');
  $admin_roles = content_access_get_permission_access('bypass node access');
  foreach (Element::children($element) as $key) {
    if (!in_array($key, $access_roles) && $key == AccountInterface::ANONYMOUS_ROLE && !in_array(AccountInterface::AUTHENTICATED_ROLE, $access_roles)) {
      $element[$key]['#disabled'] = TRUE;
      $element[$key]['#default_value'] = FALSE;
      $element[$key]['#prefix'] = '<span ' . new Attribute([
        'title' => t("This role is lacking the permission '@perm', so it has no access.", [
          '@perm' => t('access content'),
        ]),
      ]) . '>';
      $element[$key]['#suffix'] = "</span>";
    }
    elseif (in_array($key, $admin_roles) || $key != AccountInterface::ANONYMOUS_ROLE && in_array(AccountInterface::AUTHENTICATED_ROLE, $admin_roles)) {

      // Fix the checkbox to be enabled for users with administer node
      // privileges.
      $element[$key]['#disabled'] = TRUE;
      $element[$key]['#default_value'] = TRUE;
      $element[$key]['#prefix'] = '<span ' . new Attribute([
        'title' => t("This role has '@perm' permission, so access is granted.", [
          '@perm' => t('bypass node access'),
        ]),
      ]) . '>';
      $element[$key]['#suffix'] = "</span>";
    }
  }
  return $element;
}