Modifying values with rep.resource.values


I’m trying to append some additional text to a value via a module, and am wondering if this is possible with rep.resource.values. I understand how to do this using rep.value.html, but the problem is that it only needs to be done for one property per item, so I don’t want to cycle through every and each value because it also involves querying the database. So I’d rather just query the database once, so I think I can do that easier using rep.resource.values

But whenever I try to modify the ‘values’ param and then re-set it, it breaks. I see from the Hide Properties module how you could remove entire values, but I want to just modify the value of the value. So I’ve tried something like this…

$values = $event->getParam('values');
$values["dcterms:creator"]["values"][0] = json_decode(json_encode($values["dcterms:creator"]["values"][0]));
$values["dcterms:creator"]["values"][0]["o:label"] = $values["dcterms:creator"]["values"][0]["o:label"] . " (Appended text)";
$event->setParam('values', $values);

I’ve been able to get something like this to work when using rep.resource.json, but doesn’t seem to work here the same.


Joseph Anderson

You’re reassigning a Omeka\Api\Representation\ValueRepresentation object as a JSON object. That’s why it’s breaking. Any subsequent code is expecting a value representation, not JSON. I’m pretty sure that you can’t use the rep.resource.values event to append text, since the representation interface doesn’t allow it. Instead, try the rep.value.html event.

I’m not sure your exact specifications but something like this may work, or at the very least put you on the right track:

    function (ZendEvent $event) {
        $value = $event->getTarget();
        if ('uri' !== $value->type() || 'dcterms:creator' !== $value->property()->term()) {
        $uri = $value->uri();
        $uriLabel = $value->value() . ' (Appended text)';
        if (!$uriLabel) {
            $uriLabel = $uri;
        $view = $this->getServiceLocator()->get('ViewRenderer');
        $event->setParam('html', $view->hyperlink($uriLabel, $uri, ['class' => 'uri-value-link', 'target' => '_blank']));

Thanks so much Jim. I’ll play around with rep.value.html then. The reason why I was trying the previous method with the json step was because, and I’m not sure where exactly, I saw in another module something like that with rep.resource.json. So when I want to append text to a value in the json-ld, I can do something like this:

$jsonLd = $event->getParam('jsonLd');
$jsonLd["dcterms:creator"][0] = json_decode(json_encode($jsonLd["dcterms:creator"][0]), true);
$jsonLd["dcterms:creator"][0]["@value"] = $jsonLd["dcterms:creator"][0]["@value"] . " (Appended text)";
$event->setParam('jsonLd', $jsonLd);

I guess what’s happening here is because you’re dealing with ValueRepresentation, to directly modify it you need to convert it to json and then back to an array. In this case, I guess the subsequent code is alright if it it doesn’t remain the ValueRepresentation, whereas in the case of rep.resource.values it needs to be stay as a ValueRepresentation.

I will try rep.value.html instead. Not sure if you remember my question from a long while ago, but this is related to my quest to add relator terms for contributors, which I just picked up working on again :grimacing:

Thanks for your help as always!

This topic was automatically closed 250 days after the last reply. New replies are no longer allowed.