From c9235065539d6ec4503df91456ddd0caf1d60abf Mon Sep 17 00:00:00 2001 From: Daniel Berthereau Date: Mon, 23 Jan 2023 00:00:00 +0000 Subject: [PATCH] Passed resource and value annotation to template for displayValues(). --- .../src/Api/Adapter/ResourceAdapter.php | 1 - .../Api/Adapter/ValueAnnotationAdapter.php | 21 ++++++++++ .../ValueAnnotationRepresentation.php | 39 +++++++++++++++++++ .../Representation/ValueRepresentation.php | 2 +- 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/application/src/Api/Adapter/ResourceAdapter.php b/application/src/Api/Adapter/ResourceAdapter.php index f717702a5e..66821ff4de 100644 --- a/application/src/Api/Adapter/ResourceAdapter.php +++ b/application/src/Api/Adapter/ResourceAdapter.php @@ -33,7 +33,6 @@ public function hydrate(Request $request, EntityInterface $entity, ErrorStore $e * * This version simply proxies to the "real" getRepresentation for each resource's adapter. * - * @param string|int $id The unique identifier of the resource * @param mixed $data Whatever data is needed to compose the representation. * @return ResourceInterface|null */ diff --git a/application/src/Api/Adapter/ValueAnnotationAdapter.php b/application/src/Api/Adapter/ValueAnnotationAdapter.php index 1b8b4d04ee..9f04c4f4b6 100644 --- a/application/src/Api/Adapter/ValueAnnotationAdapter.php +++ b/application/src/Api/Adapter/ValueAnnotationAdapter.php @@ -2,7 +2,9 @@ namespace Omeka\Api\Adapter; use Omeka\Api\Representation\ValueAnnotationRepresentation; +use Omeka\Api\Representation\ValueRepresentation; use Omeka\Api\Request; +use Omeka\Api\ResourceInterface; use Omeka\Entity\ValueAnnotation; class ValueAnnotationAdapter extends AbstractResourceEntityAdapter @@ -46,4 +48,23 @@ public function delete(Request $request) { return AbstractAdapter::delete($request); } + + /** + * Compose a resource representation object. + * + * The value is not directly available in ValueAnnotation data, because + * their one-to-one relation is unidirectional, so append it here. + * + * @param mixed $data Whatever data is needed to compose the representation. + * @param ValueRepresentation|null + * @return ResourceInterface|null + */ + public function getRepresentation(ResourceInterface $data = null, ?ValueRepresentation $value = null) + { + if (!$data instanceof ValueAnnotation || !$value) { + // Do not attempt to compose a null representation. + return null; + } + return new ValueAnnotationRepresentation($data, $this, $value); + } } diff --git a/application/src/Api/Representation/ValueAnnotationRepresentation.php b/application/src/Api/Representation/ValueAnnotationRepresentation.php index 2c2eaa52b2..3be796fff3 100644 --- a/application/src/Api/Representation/ValueAnnotationRepresentation.php +++ b/application/src/Api/Representation/ValueAnnotationRepresentation.php @@ -1,8 +1,28 @@ value = $value; + } + public function getResourceJsonLdType() { return 'o:ValueAnnotation'; @@ -13,12 +33,31 @@ public function getResourceJsonLd() return []; } + /** + * Get the resource representation. + */ + public function resource(): AbstractResourceEntityRepresentation + { + return $this->value->resource(); + } + + /** + * Get the value representation. + */ + public function resourceValue(): ValueRepresentation + { + return $this->value; + } + public function displayValues(array $options = []) { $eventManager = $this->getEventManager(); $args = $eventManager->prepareArgs(['values' => $this->values()]); $eventManager->trigger('rep.resource.value_annotation_display_values', $this, $args); + $options['resource'] = $this->resource(); + $options['valueAnnotation'] = $this; $options['values'] = $args['values']; + $options['templateProperties'] = []; $partial = $this->getViewHelper('partial'); return $partial('common/value-annotation-resource-values', $options); diff --git a/application/src/Api/Representation/ValueRepresentation.php b/application/src/Api/Representation/ValueRepresentation.php index f6d7ef686c..6eb747f6a1 100644 --- a/application/src/Api/Representation/ValueRepresentation.php +++ b/application/src/Api/Representation/ValueRepresentation.php @@ -202,7 +202,7 @@ public function isPublic() public function valueAnnotation() { $valueAnnotation = $this->value->getValueAnnotation(); - return $valueAnnotation ? $this->getAdapter('value_annotations')->getRepresentation($valueAnnotation) : null; + return $valueAnnotation ? $this->getAdapter('value_annotations')->getRepresentation($valueAnnotation, $this) : null; } /**