Skip to content

Commit

Permalink
Specable fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-vessey committed Feb 23, 2024
1 parent aec014c commit 574666d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
4 changes: 4 additions & 0 deletions islandora_iiif_presentation_api.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ services:
class: 'Drupal\islandora_iiif_presentation_api\Normalizer\V3\FieldSpecificEntityReferenceFieldItemListNormalizer'
tags:
- { name: normalizer, priority: 20 }
- { name: iiif_presentation_api_map.v3 }
arguments: ['@entity_type.manager', 'node', 'field_model']
serializer.normalizer.islandora_iiif_presentation_api.iiif_p_v3.media.field_media_image_entity_reference_field_item_list:
class: 'Drupal\islandora_iiif_presentation_api\Normalizer\V3\FieldSpecificEntityReferenceFieldItemListNormalizer'
tags:
- { name: normalizer, priority: 20 }
- { name: iiif_presentation_api_map.v3 }
arguments: [ '@entity_type.manager', 'media', 'field_media_image' ]
serializer.normalizer.islandora_iiif_presentation_api.iiif_p_v3.node.field_member_of_entity_reference_field_item_list:
class: 'Drupal\islandora_iiif_presentation_api\Normalizer\V3\MemberOfEntityReferenceFieldItemListNormalizer'
tags:
- { name: normalizer, priority: 25 }
- { name: iiif_presentation_api_map.v3 }
arguments: [ '@entity_type.manager', 'node', 'field_member_of' ]
serializer.normalizer.islandora_iiif_presentation_api.iiif_p_v3.node.field_model_entity_reference_item:
class: 'Drupal\islandora_iiif_presentation_api\Normalizer\V3\ModelEntityReferenceItemNormalizer'
tags:
- { name: normalizer, priority: 20 }
- { name: iiif_presentation_api_map.v3 }
arguments: ['@islandora.utils']
serializer.normalizer.islandora_iiif_presentation_api.iiif_p_v3.media_entity:
class: 'Drupal\islandora_iiif_presentation_api\Normalizer\V3\ImageMediaEntityNormalizer'
Expand Down
28 changes: 24 additions & 4 deletions src/Normalizer/FieldSpecificNormalizerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ trait FieldSpecificNormalizerTrait {
*
* @var string
*/
protected string $supportedReferenceField;
protected string $targetFieldName;

/**
* The entity type that this normalizer supports.
*
* @var string
*/
protected string $supportedEntityType;
protected string $targetEntityTypeId;

/**
* Ensures that the only field and entity type defined supports normalization.
Expand All @@ -32,8 +32,28 @@ trait FieldSpecificNormalizerTrait {
*/
public function isSupportedTypeAndReference($data): bool {
return is_a($data, $this->getSupportedType()) &&
$data->getEntity()->getEntityTypeId() === $this->supportedEntityType &&
$this->getFieldName($data) === $this->supportedReferenceField;
$data->getEntity()->getEntityTypeId() === $this->getTargetEntityTypeId() &&
$this->getFieldName($data) === $this->getTargetFieldName();
}

/**
* Get the target entity type.
*
* @return string
* The target entity type.
*/
public function getTargetEntityTypeId() : string {
return $this->targetEntityTypeId;
}

/**
* Get the target field name.
*
* @return string
* The target field name.
*/
public function getTargetFieldName() : string {
return $this->targetFieldName;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
use Drupal\Core\Field\EntityReferenceFieldItemList;
use Drupal\iiif_presentation_api\Normalizer\V3\NormalizerBase;
use Drupal\islandora_iiif_presentation_api\Normalizer\FieldItemListSpecificNormalizerTrait;
use Drupal\iiif_presentation_api\MappedFieldInterface;

/**
* Expands entity reference fields to their referenced entity given constraints.
*/
class FieldSpecificEntityReferenceFieldItemListNormalizer extends NormalizerBase {
class FieldSpecificEntityReferenceFieldItemListNormalizer extends NormalizerBase implements MappedFieldInterface {

use FieldItemListSpecificNormalizerTrait;

Expand Down Expand Up @@ -39,8 +40,8 @@ class FieldSpecificEntityReferenceFieldItemListNormalizer extends NormalizerBase
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, string $entity_type, string $reference_field) {
$this->entityTypeManager = $entity_type_manager;
$this->supportedReferenceField = $reference_field;
$this->supportedEntityType = $entity_type;
$this->targetFieldName = $reference_field;
$this->targetEntityTypeId = $entity_type;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Normalizer/V3/ImageItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
use Drupal\iiif_presentation_api\Normalizer\V3\NormalizerBase;
use Drupal\image\Plugin\Field\FieldType\ImageItem;
use Drupal\islandora_iiif_presentation_api\Normalizer\FieldItemSpecificNormalizerTrait;
use Drupal\iiif_presentation_api\MappedFieldInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Serializer\Exception\LogicException;

/**
* Normalizer for image items.
*/
class ImageItemNormalizer extends NormalizerBase {
class ImageItemNormalizer extends NormalizerBase implements MappedFieldInterface {

use EntityUriTrait;
use FieldItemSpecificNormalizerTrait;
Expand All @@ -34,8 +35,8 @@ public function __construct(
protected EntityTypeManagerInterface $entityTypeManager,
protected EventDispatcherInterface $eventDispatcher,
) {
$this->supportedReferenceField = 'field_media_image';
$this->supportedEntityType = 'media';
$this->targetFieldName = 'field_media_image';
$this->targetEntityTypeId = 'media';
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Normalizer/V3/ModelEntityReferenceItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
use Drupal\iiif_presentation_api\Normalizer\V3\NormalizerBase;
use Drupal\islandora\IslandoraUtils;
use Drupal\islandora_iiif_presentation_api\Normalizer\FieldItemSpecificNormalizerTrait;
use Drupal\iiif_presentation_api\MappedFieldInterface;
use Drupal\taxonomy\TermInterface;
use Symfony\Component\Serializer\Exception\LogicException;

/**
* Normalizer for oddity that is Islandora's field_model to find media.
*/
class ModelEntityReferenceItemNormalizer extends NormalizerBase {
class ModelEntityReferenceItemNormalizer extends NormalizerBase implements MappedFieldInterface {

use FieldItemSpecificNormalizerTrait;

Expand Down Expand Up @@ -43,8 +44,8 @@ class ModelEntityReferenceItemNormalizer extends NormalizerBase {
*/
public function __construct(IslandoraUtils $islandora_utils) {
$this->islandoraUtils = $islandora_utils;
$this->supportedReferenceField = IslandoraUtils::MODEL_FIELD;
$this->supportedEntityType = 'node';
$this->targetFieldName = IslandoraUtils::MODEL_FIELD;
$this->targetEntityTypeId = 'node';
}

/**
Expand Down

0 comments on commit 574666d

Please sign in to comment.