Skip to content

Commit

Permalink
Passed resource and value annotation to template for displayValues().
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Berthereau authored and Daniel Berthereau committed Jan 23, 2023
1 parent 89db125 commit c923506
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
1 change: 0 additions & 1 deletion application/src/Api/Adapter/ResourceAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
21 changes: 21 additions & 0 deletions application/src/Api/Adapter/ValueAnnotationAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
<?php
namespace Omeka\Api\Representation;

use Omeka\Api\Adapter\ValueAnnotationAdapter;
use Omeka\Entity\ValueAnnotation;

class ValueAnnotationRepresentation extends AbstractResourceEntityRepresentation
{
/**
* @var \Omeka\Api\Representation\ValueRepresentation
*/
protected $value;

/**
* Construct the value annotation representation object.
*/
public function __construct(
ValueAnnotation $valueAnnotation,
ValueAnnotationAdapter $adapter,
ValueRepresentation $value
) {
parent::__construct($valueAnnotation, $adapter);
$this->value = $value;
}

public function getResourceJsonLdType()
{
return 'o:ValueAnnotation';
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion application/src/Api/Representation/ValueRepresentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit c923506

Please sign in to comment.