public function Field::access

Same name in this branch
  1. 8.x drupal/core/lib/Drupal/Core/Entity/Field/Type/Field.php \Drupal\Core\Entity\Field\Type\Field::access()
  2. 8.x drupal/core/modules/field/lib/Drupal/field/Plugin/views/field/Field.php \Drupal\field\Plugin\views\field\Field::access()

Implements \Drupal\Core\TypedData\AccessibleInterface::access().

Overrides AccessibleInterface::access

File

drupal/core/lib/Drupal/Core/Entity/Field/Type/Field.php, line 148
Contains \Drupal\Core\Entity\Field\Type\Field.

Class

Field
Represents an entity field; that is, a list of field item objects.

Namespace

Drupal\Core\Entity\Field\Type

Code

public function access($operation = 'view', AccountInterface $account = NULL) {
  global $user;
  if (!isset($account) && $user->uid) {
    $account = user_load($user->uid);
  }

  // Get the default access restriction that lives within this field.
  $access = $this
    ->defaultAccess($operation, $account);

  // Invoke hook and collect grants/denies for field access from other
  // modules. Our default access flag is masked under the ':default' key.
  $grants = array(
    ':default' => $access,
  );
  $hook_implementations = \Drupal::moduleHandler()
    ->getImplementations('entity_field_access');
  foreach ($hook_implementations as $module) {
    $grants = array_merge($grants, array(
      $module => module_invoke($module, 'entity_field_access', $operation, $this, $account),
    ));
  }

  // Also allow modules to alter the returned grants/denies.
  $context = array(
    'operation' => $operation,
    'field' => $this,
    'account' => $account,
  );
  drupal_alter('entity_field_access', $grants, $context);

  // One grant being FALSE is enough to deny access immediately.
  if (in_array(FALSE, $grants, TRUE)) {
    return FALSE;
  }

  // At least one grant has the explicit opinion to allow access.
  if (in_array(TRUE, $grants, TRUE)) {
    return TRUE;
  }

  // All grants are NULL and have no opinion - deny access in that case.
  return FALSE;
}