You are here

function _rac_get_entity_reference_roles in Role Access Control 8.2

Same name and namespace in other branches
  1. 8 rac.module \_rac_get_entity_reference_roles()

Get list of roles refrenced by $entity and cache it.

Retrives entity reference values for a node. Return values are cached.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: Entity to find field values for.

See also

tac_lite_node_get_terms

File

./rac.module, line 81
Module providing role access relations.

Code

function _rac_get_entity_reference_roles(EntityInterface $entity) {
  $roles =& drupal_static(__FUNCTION__);
  if (!$entity) {
    return [];
  }
  $id = $entity
    ->id();
  $type = $entity
    ->getEntityType();
  $type_id = $type
    ->id();
  if (!isset($roles[$type_id]) || !isset($roles[$type_id][$id])) {

    // Get list of fields.
    $bundle = $entity
      ->bundle();
    $fields = _rac_get_entity_reference_role_fields($type, $bundle);

    // Get values for each field.
    foreach ($fields as $field_name) {
      if ($items = $entity
        ->get($field_name)
        ->getValue()) {

        // Filter out empty values from items list.
        $items = array_filter($items);
        foreach ($items as $item) {
          $roles[$type_id][$id][] = $item['target_id'];
        }
      }
    }

    // Filter out null values from forms.
    if (isset($roles[$type_id][$id])) {
      $roles[$type_id][$id] = array_filter(array_unique($roles[$type_id][$id]));
    }
    else {
      $roles[$type_id][$id] = [];
    }
  }
  return isset($roles[$type_id][$id]) ? $roles[$type_id][$id] : [];
}