ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
VocabValueFactory.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
32use ILIAS\Refinery\Factory as Refinery;
34
36{
37 protected VocabularyAdapter $vocabulary_adapter;
39 protected PathFactory $path_factory;
40
41 public function __construct(
42 UIFactory $ui_factory,
44 ConstraintDictionary $constraint_dictionary,
45 VocabularyAdapter $vocabulary_adapter,
47 PathFactory $path_factory
48 ) {
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}
Builds data types.
Definition: Factory.php:36
getInput(ElementInterface $element, ElementInterface $context_element)
rawInput(ElementInterface $element, ElementInterface $context_element, SlotIdentifier $slot, bool $add_value_from_data)
getInputInCondition(ElementInterface $element, ElementInterface $context_element, SlotIdentifier $conditional_slot)
__construct(UIFactory $ui_factory, PresenterInterface $presenter, ConstraintDictionary $constraint_dictionary, VocabularyAdapter $vocabulary_adapter, Refinery $refinery, PathFactory $path_factory)
return true
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This describes inputs that can be used in forms.
Definition: FormInput.php:33
This is what a factory for input fields looks like.
Definition: Factory.php:31
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc