function MetaEventSubscriber::addRelationshipMeta

Parameters

\Drupal\jsonapi\Events\CollectRelationshipMetaEvent $event: Event to be processed.

Return value

void

File

core/modules/jsonapi/tests/modules/jsonapi_test_meta_events/src/EventSubscriber/MetaEventSubscriber.php, line 78

Class

MetaEventSubscriber
Event subscriber which tests adding metadata to ResourceObjects and relationships.

Namespace

Drupal\jsonapi_test_meta_events\EventSubscriber

Code

public function addRelationshipMeta(CollectRelationshipMetaEvent $event) : void {
    $config = \Drupal::state()->get('jsonapi_test_meta_events.relationship_meta', [
        'enabled_type' => FALSE,
        'enabled_id' => FALSE,
        'enabled_relation' => FALSE,
        'fields' => FALSE,
        'user_is_admin_context' => FALSE,
    ]);
    // Only continue if the resource type is enabled.
    if ($config['enabled_type'] === FALSE || $config['enabled_type'] !== $event->getResourceObject()
        ->getTypeName()) {
        return;
    }
    // Only apply on the referenced ID of the resource.
    if ($config['enabled_id'] !== FALSE && $config['enabled_id'] !== $event->getResourceObject()
        ->getId()) {
        return;
    }
    // Only continue if this is the correct relation.
    if ($config['enabled_relation'] === FALSE || $config['enabled_relation'] !== $event->getRelationshipFieldName()) {
        return;
    }
    $relationshipFieldName = $event->getRelationshipFieldName();
    $field = $event->getResourceObject()
        ->getField($relationshipFieldName);
    $referencedEntities = [];
    if ($field instanceof EntityReferenceFieldItemListInterface) {
        $referencedEntities = $field->referencedEntities();
        $event->addCacheTags([
            'jsonapi_test_meta_events.relationship_meta',
        ]);
    }
    if ($config['user_is_admin_context'] ?? FALSE) {
        $event->addCacheContexts([
            'user.roles',
        ]);
        $event->setMetaValue('resource_meta_user_has_admin_role', $this->currentUserHasAdminRole());
    }
    // If no fields are specified just add a list of UUIDs to the relations.
    if ($config['fields'] === FALSE) {
        $referencedEntityIds = [];
        foreach ($referencedEntities as $entity) {
            $referencedEntityIds[] = $entity->uuid();
        }
        $event->setMetaValue('relationship_meta_' . $event->getRelationshipFieldName(), $referencedEntityIds);
        return;
    }
    // Add the metadata for each field. The field configuration must be an array
    // of field values keyed by the field name.
    foreach ($config['fields'] as $field_name) {
        $fieldValues = [];
        foreach ($referencedEntities as $entity) {
            $fieldValues[] = $entity->get($field_name)->value;
        }
        $event->setMetaValue('relationship_meta_' . $field_name, $fieldValues);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.