ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
VocabValueFactory.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
34 
36 {
37  protected VocabularyAdapter $vocabulary_adapter;
38  protected Refinery $refinery;
40 
41  public function __construct(
45  VocabularyAdapter $vocabulary_adapter,
46  Refinery $refinery,
47  PathFactory $path_factory
48  ) {
49  parent::__construct($ui_factory, $presenter, $constraint_dictionary);
50  $this->vocabulary_adapter = $vocabulary_adapter;
51  $this->refinery = $refinery;
52  $this->path_factory = $path_factory;
53  }
54 
55  protected function rawInput(
56  ElementInterface $element,
57  ElementInterface $context_element,
58  SlotIdentifier $slot,
59  bool $add_value_from_data
60  ): FormInput {
61  $data = null;
62  if ($element->getData()->type() !== Type::NULL) {
63  $data = $element->getData()->value();
64  }
65 
66  $values = [];
67  $raw_values = $this->vocabulary_adapter->valuesInVocabulariesForSlot(
68  $slot,
69  $add_value_from_data ? $data : null
70  );
71  foreach ($this->presenter->data()->vocabularyValues($slot, ...$raw_values) as $labelled_value) {
72  $values[$labelled_value->value()] = $labelled_value->label();
73  }
74 
75  $input = $this->ui_factory->select(
76  $this->getInputLabelFromElement($this->presenter, $element, $context_element),
77  $values
78  );
79  if ($add_value_from_data && isset($data)) {
80  $input = $input->withValue($data);
81  }
82 
83  $source_path = $this->getPathToSourceElement($element);
84  $sources_by_value = $this->vocabulary_adapter->sourceMapForSlot($slot);
85  return $this->addConstraintsFromElement($this->constraint_dictionary, $element, $input)
86  ->withAdditionalTransformation(
87  $this->refinery->custom()->transformation(function ($vs) use ($sources_by_value, $source_path) {
88  $source = $sources_by_value((string) $vs);
89  return [
90  $vs,
91  [$source_path->toString() => $source]
92  ];
93  })
94  );
95  }
96 
97  public function getInput(
98  ElementInterface $element,
99  ElementInterface $context_element
100  ): FormInput {
101  return $this->rawInput(
102  $element,
103  $context_element,
104  $slot = $this->vocabulary_adapter->slotForElement($element),
105  true
106  );
107  }
108 
109  public function getInputInCondition(
110  ElementInterface $element,
111  ElementInterface $context_element,
112  SlotIdentifier $conditional_slot
113  ): FormInput {
114  $slot = $this->vocabulary_adapter->slotForElement($element);
115  return $this->rawInput(
116  $element,
117  $context_element,
118  $conditional_slot,
119  $slot === $conditional_slot
120  );
121  }
122 
124  {
125  foreach ($element->getSuperElement()->getSubElements() as $el) {
126  if ($el->getDefinition()->dataType() === Type::VOCAB_SOURCE) {
127  return $this->path_factory->toElement($el, true);
128  }
129  }
130  throw new \ilMDEditorException('Vocab values must not be separated from their source.');
131  }
132 }
getInputInCondition(ElementInterface $element, ElementInterface $context_element, SlotIdentifier $conditional_slot)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
rawInput(ElementInterface $element, ElementInterface $context_element, SlotIdentifier $slot, bool $add_value_from_data)
getInput(ElementInterface $element, ElementInterface $context_element)
__construct(UIFactory $ui_factory, PresenterInterface $presenter, ConstraintDictionary $constraint_dictionary, VocabularyAdapter $vocabulary_adapter, Refinery $refinery, PathFactory $path_factory)
__construct(Container $dic, ilPlugin $plugin)
This describes inputs that can be used in forms.
Definition: FormInput.php:32