You are here

function _rac_get_entity_reference_roles in Role Access Control 8

Same name and namespace in other branches
  1. 8.2 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

5 calls to _rac_get_entity_reference_roles()
rac_fca_field_collection_access_restriction_message_alter in contrib/rac_fca/rac_fca.module
Implements hook_collection_access_restriction_message_alter().
rac_fca_field_collection_item_access_records in contrib/rac_fca/rac_fca.module
Implements hook_field_collection_item_access_records().
rac_na_node_access_records in contrib/rac_na/rac_na.module
Implements hook_node_access_records().
rac_pa_paragraphs_access_records in contrib/rac_pa/rac_pa.module
Implements hook_paragraph_access_records().
rac_pa_paragraphs_access_restriction_message_alter in contrib/rac_pa/rac_pa.module
Implements hook_paragraph_access_restriction_message_alter().

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] : [];
}