Esempio n. 1
0
 /**
  * @param DataUnitComponentInterface $input
  * @param FormState $state
  * @param array $defaultValues
  * @throws ComponentException
  */
 public function compile(DataUnitComponentInterface $input, FormState $state, array $defaultValues)
 {
     // extract the input name
     $name = $input->getName();
     $fullName = $input->getFullName();
     // if there was a form scope default value provided, set that
     if (array_key_exists($name, $defaultValues) && !is_null($defaultValues[$name])) {
         $input->setValue($defaultValues[$name]);
     }
     // if there was a submit
     if ($this->shouldReadInput($input, $state)) {
         // read the raw value
         $input->setValue($this->readInput($input, $state));
         // if there was no value from the input
         if ($input->hasValue() === false) {
             // required check
             if ($input instanceof RequiredInterface && $input->isRequired()) {
                 $params = array();
                 if ($input instanceof TranslatableComponentInterface) {
                     $key = $input->getTranslationKey();
                     $params = array('control' => $state->getServices()->getTranslator()->translateRaw($key, $input, $state));
                 }
                 throw new ComponentException($input, '', 'validator.required', $params);
             }
         } else {
             // evaluate the value
             if ($input instanceof EvaluatableInterface) {
                 $input->setValue($state->getServices()->getEvaluationHandler()->evaluate($input));
             }
             // encode the value (if converter set)
             if ($input instanceof ConvertableInterface) {
                 // extract the converter config from the input
                 $config = $input->getConverter();
                 if (!is_null($config)) {
                     $converter = $state->getServices()->getConverters()->get($config->getType());
                     /** @var DataUnitComponentInterface $input */
                     $input->setValue($converter->encode($input->getValue(), $config->getArgs()));
                 }
             }
             // store the read input if it should be persisted
             if ($input->isPersistent() || $state->isPersistent()) {
                 $state->getServices()->getPersistenceHandler()->setValue($fullName, $input->getValue(), $state->getFormId());
             }
         }
     } else {
         // if persistent and the persistence handler has a value for this data unit, load it
         if (($input->isPersistent() || $state->isPersistent()) && $state->getServices()->getPersistenceHandler()->hasValue($fullName, $state->getFormId())) {
             $input->setValue($state->getServices()->getPersistenceHandler()->getValue($fullName, $state->getFormId()));
         }
     }
 }
Esempio n. 2
0
 /**
  * @param DataUnitComponentInterface $child
  */
 protected function storeValue(DataUnitComponentInterface $child)
 {
     // add the value of the component to the values of this container
     $this->values[$child->getName()] = $child->getValue();
     // also store the value the standard way
     parent::storeValue($child);
 }