You are here

function _rac_get_entity_reference_role_fields in Role Access Control 8.2

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

Get list of role reference fields for an entity bundle.

Retrieves a list of field names of Entity reference fields with type role.

Parameters

\Drupal\Core\Entity\EntityTypeInterface $type: Entity type to retrieve fields of.

string $bundle: Name of Entity Bundle to filter fields by. Defaults to Entity type id if none provided.

Return value

array List of field name of role reference fields on the bundle. If there are no fields, the array is empty.

1 call to _rac_get_entity_reference_role_fields()
_rac_get_entity_reference_roles in ./rac.module
Get list of roles refrenced by $entity and cache it.

File

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

Code

function _rac_get_entity_reference_role_fields(EntityTypeInterface $type, $bundle = NULL) {
  $bundle_fields =& drupal_static(__FUNCTION__);
  if (!$type
    ->entityClassImplements('\\Drupal\\Core\\Entity\\FieldableEntityInterface')) {
    return [];
  }
  $type_id = $type
    ->id();
  if ($bundle === NULL) {
    $bundle = $type_id;
  }
  if (!isset($bundle_fields[$type_id]) || !isset($bundle_fields[$type_id][$bundle])) {
    $bundle_fields[$type_id][$bundle] = [];

    // Get fields of bundle.
    $fields = \Drupal::service('entity_field.manager')
      ->getFieldDefinitions($type_id, $bundle);

    // Filter fields on bundle to only role references.
    foreach ($fields as $field) {
      $field_type = method_exists($field, 'getType') ? $field
        ->getType() : NULL;
      $target_type = method_exists($field, 'getSetting') ? $field
        ->getSetting('target_type') : NULL;
      if ($field_type === 'entity_reference' && $target_type === 'user_role') {
        $field_storage = $field
          ->getFieldStorageDefinition();
        if ($field_storage
          ->getThirdPartySetting("rac", "enabled", FALSE)) {
          $bundle_fields[$type_id][$bundle][] = $field
            ->getName();
        }
      }
    }
  }
  return isset($bundle_fields[$type_id][$bundle]) ? $bundle_fields[$type_id][$bundle] : FALSE;
}