Skip to content
Snippets Groups Projects

3100732 for 11.0 with deprecations removed.

Open Brad Jones requested to merge issue/drupal-3100732:3100732-11.x into 11.x
7 unresolved threads

Closes #3100732

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
1 <?php
2
3 namespace Drupal\jsonapi_test_meta_events\EventSubscriber;
4
5 use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
6 use Drupal\jsonapi\Events\CollectRelationshipMetaEvent;
7 use Drupal\jsonapi\Events\CollectResourceObjectMetaEvent;
8 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
10 /**
11 * @internal
12 */
13 class MetaEventSubscriber implements EventSubscriberInterface {
14
15 public static function getSubscribedEvents() {
  • 7 use Drupal\jsonapi\Events\CollectResourceObjectMetaEvent;
    8 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    9
    10 /**
    11 * @internal
    12 */
    13 class MetaEventSubscriber implements EventSubscriberInterface {
    14
    15 public static function getSubscribedEvents() {
    16 return [
    17 CollectResourceObjectMetaEvent::class => 'addResourceObjectMeta',
    18 CollectRelationshipMetaEvent::class => 'addRelationshipMeta',
    19 ];
    20 }
    21
    22 public function addResourceObjectMeta(CollectResourceObjectMetaEvent $event) {
  • 1 <?php
    2
    3 namespace Drupal\jsonapi_test_meta_events\EventSubscriber;
    4
    5 use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
    6 use Drupal\jsonapi\Events\CollectRelationshipMetaEvent;
    7 use Drupal\jsonapi\Events\CollectResourceObjectMetaEvent;
    8 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    9
    10 /**
    11 * @internal
    12 */
    13 class MetaEventSubscriber implements EventSubscriberInterface {
    14
    15 public static function getSubscribedEvents() {
  • 44 if ($config['user_is_superuser_context']) {
    45 $event->addCacheContexts(['user.is_super_user']);
    46 $event->setMetaValue('resource_meta_user_is_superuser', (int) \Drupal::currentUser()->id() === 1 ? 'yes' : 'no');
    47 $event->setMetaValue('resource_meta_user_id', \Drupal::currentUser()->id());
    48 }
    49
    50 // Fields expect an array of field names, the value of the fields are then
    51 // added to the metadata.
    52 foreach ($config['fields'] as $field_name) {
    53 $event->setMetaValue('resource_meta_' . $field_name, $event->getResourceObject()->getField($field_name)->value);
    54 }
    55
    56 $event->addCacheTags(['jsonapi_test_meta_events.object_meta']);
    57 }
    58
    59 public function addRelationshipMeta(CollectRelationshipMetaEvent $event) {
  • 615 $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Contexts', 'user.is_super_user');
    616 $this->assertSession()->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'MISS');
    617
    618 // Tests if relationship has correct metadata when loading a single resource.
    619 $result = Json::decode($this->drupalGet('jsonapi/node/article/' . $node->uuid()));
    620 $this->assertEquals($expectedMeta, $result['data']['meta']);
    621 // Test if the cache tags bubbled up
    622 $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', 'jsonapi_test_meta_events.object_meta');
    623 $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Contexts', 'user.is_super_user');
    624 $this->assertSession()->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'HIT');
    625 }
    626
    627 /**
    628 * Tests adding metadata to relationship.
    629 */
    630 public function testMetaRelationEvent() {
  • 670
    671 $this->assertCount(count($tags), $tagNames);
    672 foreach ($tags as $tag) {
    673 $this->assertContains($tag->label(), $tagNames);
    674 }
    675
    676 }
    677
    678 // Test if the cache tags bubbled up
    679 $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', 'jsonapi_test_meta_events.relationship_meta');
    680 }
    681
    682 /**
    683 * Tests adding metadata to relationship endpoint.
    684 */
    685 public function testMetaRelationEventOnRelationshipEndpoint() {
  • 38 private EventDispatcherInterface $eventDispatcher;
    39
    32 40 /**
    33 41 * Constructs a ResourceObjectNormalizer object.
    34 42 *
    35 43 * @param \Drupal\jsonapi\EventSubscriber\ResourceObjectNormalizationCacher $cacher
    36 44 * The entity normalization cacher.
    45 * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
    46 * The event dispatcher.
    37 47 */
    38 public function __construct(ResourceObjectNormalizationCacher $cacher) {
    48 public function __construct(ResourceObjectNormalizationCacher $cacher, ?EventDispatcherInterface $event_dispatcher = NULL) {
    39 49 $this->cacher = $cacher;
    50
    51 if ($event_dispatcher === NULL) {
    52 @trigger_error(__METHOD__ . '() without the $event_dispatcher argument is deprecated in drupal:10.3.0 and will be required in drupal:11.0.0. See https://www.drupal.org/node/3280569', E_USER_DEPRECATED);
  • Björn Brala added 1 commit

    added 1 commit

    • 10e76824 - Fix docblock issues and some return tpyes as per smustgrave's review

    Compare with previous version

  • Björn Brala added 1 commit

    added 1 commit

    • a75e49ce - Remove BC later from 11.x branch

    Compare with previous version

  • Björn Brala added 1 commit

    added 1 commit

    Compare with previous version

  • Please register or sign in to reply
    Loading