Some sites might want to add metadata to resources or their relations to convey implementation-specific information.
Before this release you could not adjust metadata without overriding internal classes. To allow developers to add metadata a new set of events has been introduced that are dispatched.
CollectResourceObjectMetaEventCollectRelationshipMetaEvent
Subscribers can use the following methods to adjust the metadata of the resource:
CollectResourceObjectMetaEvent::setMetaValue($property, $value)to set the meta property of the resource.CollectRelationshipMetaEvent::setMetaValue($property, $value)to set the meta property of the relations of a resource.
These events have the related resource and context available in the event. See \Drupal\jsonapi_test_meta_events\EventSubscriber\MetaEventSubscriber for an example of how to add meta information to your resources.
Example
Something you might want to do with this feature is add metadata about an external service to your JSON:API response. This would be data that is related to your resource, but it not part of the data model. For this example, we need to add some external payment information on the order for the client. This data is generated based on an external service.
namespace Drupal\custom_payment\EventSubscriber;
use Drupal\jsonapi\Events\CollectResourceObjectMetaEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PaymentMetaEventSubscriber implements EventSubscriberInterface {
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents() {
return [
CollectResourceObjectMetaEvent::class => 'addPaymentInfoToResourceObjectMeta'
];
}
/**
* Custom method to addd payment info to the order object.
*
* @param \Drupal\jsonapi\Events\CollectResourceObjectMetaEvent $event
* The event to add meta resources for.
*
* @return void
*/
public function addPaymentInfoToResourceObjectMeta(CollectResourceObjectMetaEvent $event) {
if ($event->getResourceObject()->getTypeName() !== 'order--order') {
return;
}
$event->setMetaValue('payment_iframe_url', \Drupal::service('custom_payment.payment_data')->getIframeUrl());
}
}