Skip to content

Commit

Permalink
Fix CustomVocab integration for newer versions
Browse files Browse the repository at this point in the history
  • Loading branch information
jimsafley committed Apr 24, 2023
1 parent 0562a4d commit ba53b50
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/Api/Representation/CollectingFormRepresentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,42 @@ public function getForm()
break;
case 'custom_vocab':
try {
$response = $api->read('custom_vocabs', $prompt->customVocab());
$customVocab = $api->read('custom_vocabs', $prompt->customVocab())->getContent();
} catch (NotFoundException $e) {
// The custom vocab does not exist.
continue 3;
} catch (BadRequestException $e) {
// The CustomVocab module is not installed or active.
continue 3;
}
$terms = array_map('trim', explode(PHP_EOL, $response->getContent()->terms()));
$isNewerVersion = method_exists($customVocab, 'uris');
if ($isNewerVersion) {
// This is a newer version of CustomVocab. Use the
// original heuristic to determine which vocab type
// is used.
if ($customVocab->itemSet()) {
// "Items" type not implemented
continue 3;
} elseif ($customVocab->uris()) {
// "Uris" type not implemented
continue 3;
} elseif ($customVocab->terms()) {
// On newer versions, terms could be a newline
// -delimited string or an array.
$customVocabValues = is_string($customVocab->terms())
? array_map('trim', explode(PHP_EOL, $customVocab->terms()))
: $customVocab->terms();
} else {
// Invalid type.
continue 3;
}
} else {
// This is an older version of CustomVocab.
$customVocabValues = array_map('trim', explode(PHP_EOL, $customVocab->terms()));
}
$element = new Element\PromptSelect($name);
$element->setEmptyOption('Please choose one...') // @translate
->setValueOptions(array_combine($terms, $terms));
->setValueOptions(array_combine($customVocabValues, $customVocabValues));
break;
case 'numeric:timestamp':
if (!$collecting->inputTypeIsAvailable('numeric:timestamp')) {
Expand Down
5 changes: 5 additions & 0 deletions src/View/Helper/Collecting.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ public function customVocabs()
$response = $this->getView()->api()->search('custom_vocabs');
$this->customVocabs = [];
foreach ($response->getContent() as $customVocab) {
$isNewerVersion = method_exists($customVocab, 'uris');
if ($isNewerVersion && !$customVocab->terms()) {
// The newer "URIs" and "Items" vocab types are not implemented.
continue;
}
$this->customVocabs[$customVocab->id()] = $customVocab->label();
}
} catch (BadRequestException $e) {
Expand Down

0 comments on commit ba53b50

Please sign in to comment.