diff --git a/Module.php b/Module.php index eeda30e..af3578c 100644 --- a/Module.php +++ b/Module.php @@ -179,6 +179,14 @@ public function attachListeners(SharedEventManagerInterface $sharedEventManager) 'data_types.value_annotating', [$this, 'addDataTypesToValueAnnotatingConfig'] ); + + $sharedEventManager->attach( + \Omeka\Api\Representation\ValueRepresentation::class, + 'rep.value.html', + [$this, 'handleRepresentationValueHtml'], + -1000 + ); + } public function addVocabularyServices(Event $event) @@ -256,4 +264,55 @@ public function addDataTypesToValueAnnotatingConfig(Event $event) } $event->setParam('data_types', $valueAnnotating); } + + /** + * Convert selected property values to its title. + * + * @todo Factorize handleRepresentationValueHtml(). + * + * Compatible with the converted value to a link of the following modules: + * + * Advanced Resource Template 3.4.43 + * Metadata Browse 1.6.0 + * + */ + public function handleRepresentationValueHtml(Event $event): void + { + + $services = $this->getServiceLocator(); + $status = $services->get('Omeka\Status'); + $apiManager = $services->get('Omeka\ApiManager'); + $isSite = $status->isSiteRequest(); + $isAdmin = $status->isAdminRequest(); + if (!$isSite && !$isAdmin) { + return; + } + $value = $event->getTarget(); + $type = $value->type(); + if(!empty($type) && stripos($type, 'customvocab') !== False){ + $rc = explode(':', $type); + if(!empty($rc[1])){ + $customvocabID = $rc[1]; + $response = $apiManager->read('custom_vocabs', $customvocabID); + if(!empty($response)){ + $listValues = $response->getContent()->listValues(); + $val = (string) $value->value(); + if(!empty($listValues) && !empty($val) && !empty($listValues[$val])){ + $html = $event->getParam('html'); + if(stripos($html, '') !== False){ + $need[] = '/(>.+<\/a>)/i'; + $replace[] = '>'.$listValues[$val].''; + }else{ + $need[] = '/'.$val.'/i'; + $replace[] = $listValues[$val]; + } + $html = preg_replace($need, $replace, $html); + $event->setParam('html', $html); + } + } + } + } + + } + } diff --git a/src/Api/Representation/CustomVocabRepresentation.php b/src/Api/Representation/CustomVocabRepresentation.php index 83fb585..3aa7ed6 100644 --- a/src/Api/Representation/CustomVocabRepresentation.php +++ b/src/Api/Representation/CustomVocabRepresentation.php @@ -152,13 +152,21 @@ public function listItemTitles(array $options = []): ?array /** * List of terms by term when the vocab is a simple list. + * If you need to add values ​​with labels, you need to separate them = */ public function listTerms(): ?array { $terms = $this->resource->getTerms(); - return $terms - ? array_combine($terms, $terms) - : null; + $rc = []; + foreach($terms as $v){ + if(stripos($v, '=') !== False){ + $nval = explode('=', $v); + $rc[trim($nval[0])] = trim($nval[1]); + }else{ + $rc[$v] = $v; + } + } + return $rc; } /**