ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
VocabValueFactory.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
32 
34 {
36 
37  public function __construct(
41  VocabulariesInterface $vocabularies
42  ) {
43  parent::__construct($ui_factory, $presenter, $constraint_dictionary);
44  $this->vocabularies = $vocabularies;
45  }
46 
47  protected function rawInput(
48  ElementInterface $element,
49  ElementInterface $context_element,
50  string $condition_value = ''
51  ): FormInput {
52  $data = null;
53  if ($element->getDefinition()->dataType() !== Type::NULL) {
54  $data = $this->dataValueForInput($element->getData());
55  }
56  $values = [];
57  $use_data_as_value = false;
58  foreach ($this->vocabularies->vocabulariesForElement($element) as $vocab) {
59  if ($condition_value !== '' && $vocab->condition()?->value() !== $condition_value) {
60  continue;
61  }
62  foreach ($vocab->values() as $value) {
63  if ($data === $value) {
64  $use_data_as_value = true;
65  }
66  $values[$value] = $this->presenter->data()->vocabularyValue($value);
67  }
68  }
69  $input = $this->ui_factory->select('placeholder', $values);
70  if ($use_data_as_value && isset($data)) {
71  $input = $input->withValue($data);
72  }
73  return $input;
74  }
75 
76  protected function dataValueForInput(DataInterface $data): string
77  {
78  $value = strtolower(
79  preg_replace('/(?<=[a-z])(?=[A-Z])/', ' ', $data->value())
80  );
81  $exceptions = [
82  'is part of' => 'ispartof', 'has part' => 'haspart',
83  'is version of' => 'isversionof', 'has version' => 'hasversion',
84  'is format of' => 'isformatof', 'has format' => 'hasformat',
85  'references' => 'references',
86  'is referenced by' => 'isreferencedby',
87  'is based on' => 'isbasedon', 'is basis for' => 'isbasisfor',
88  'requires' => 'requires', 'is required by' => 'isrequiredby',
89  ];
90  if (array_key_exists($value, $exceptions)) {
91  $value = $exceptions[$value];
92  }
93  return $value;
94  }
95 }
This is what a factory for input fields looks like.
Definition: Factory.php:28
rawInput(ElementInterface $element, ElementInterface $context_element, string $condition_value='')
__construct(VocabulariesInterface $vocabularies)
__construct(UIFactory $ui_factory, PresenterInterface $presenter, ConstraintDictionary $constraint_dictionary, VocabulariesInterface $vocabularies)
$vocab
getDefinition()
Defining properties of the metadata element.
This describes inputs that can be used in forms.
Definition: FormInput.php:31
value()
Value of the data, in a format according to its type.
vocabularies(ElementInterface $element, bool $ignore_marker)