class MetaEventSubscriber

Event subscriber which tests adding metadata to ResourceObjects and relationships.

@internal

Hierarchy

  • class \Drupal\jsonapi_test_meta_events\EventSubscriber\MetaEventSubscriber extends \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of MetaEventSubscriber

1 string reference to 'MetaEventSubscriber'
jsonapi_test_meta_events.services.yml in core/modules/jsonapi/tests/modules/jsonapi_test_meta_events/jsonapi_test_meta_events.services.yml
core/modules/jsonapi/tests/modules/jsonapi_test_meta_events/jsonapi_test_meta_events.services.yml
1 service uses MetaEventSubscriber
jsonapi_test_meta_events.meta_subscriber in core/modules/jsonapi/tests/modules/jsonapi_test_meta_events/jsonapi_test_meta_events.services.yml
Drupal\jsonapi_test_meta_events\EventSubscriber\MetaEventSubscriber

File

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

Namespace

Drupal\jsonapi_test_meta_events\EventSubscriber
View source
class MetaEventSubscriber implements EventSubscriberInterface {
  
  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() : array {
    return [
      CollectResourceObjectMetaEvent::class => 'addResourceObjectMeta',
      CollectRelationshipMetaEvent::class => 'addRelationshipMeta',
    ];
  }
  
  /**
   * @param \Drupal\jsonapi\Events\CollectResourceObjectMetaEvent $event
   *   Event to be processed.
   */
  public function addResourceObjectMeta(CollectResourceObjectMetaEvent $event) : void {
    $config = \Drupal::state()->get('jsonapi_test_meta_events.object_meta', [
      'enabled_type' => FALSE,
      'enabled_id' => FALSE,
      'fields' => FALSE,
      'user_is_admin_context' => FALSE,
    ]);
    // Only continue if the recourse 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;
    }
    if ($config['fields'] === FALSE) {
      return;
    }
    if ($config['user_is_admin_context']) {
      $event->addCacheContexts([
        'user.roles',
      ]);
      $event->setMetaValue('resource_meta_user_has_admin_role', $this->currentUserHasAdminRole());
      $event->setMetaValue('resource_meta_user_id', \Drupal::currentUser()->id());
    }
    // 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) {
      $event->setMetaValue('resource_meta_' . $field_name, $event->getResourceObject()
        ->getField($field_name)->value);
    }
    $event->addCacheTags([
      'jsonapi_test_meta_events.object_meta',
    ]);
  }
  
  /**
   * @param \Drupal\jsonapi\Events\CollectRelationshipMetaEvent $event
   *   Event to be processed.
   */
  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);
    }
  }
  
  /**
   * @return string
   *   The value 'yes' if the current user has an admin role, 'no' otherwise.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  protected function currentUserHasAdminRole() : string {
    $admin_roles = \Drupal::entityTypeManager()->getStorage('user_role')
      ->loadByProperties([
      'is_admin' => TRUE,
    ]);
    $has_admin_role = 'yes';
    if (count(array_intersect(\Drupal::currentUser()->getRoles(), array_keys($admin_roles))) === 0) {
      $has_admin_role = 'no';
    }
    return $has_admin_role;
  }

}

Members


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