protected function mapEntityValueToField($field_name, $entity_value) { $field = $this->form->getField($field_name); if ($field !== null) { switch ($field->type) { case 'label': $field->setValueRaw($entity_value); break; case 'date': if ($entity_value instanceof \DateTime) { $field->setValue($entity_value->format('Y-m-d')); } else { $field->setValue($entity_value); } break; case 'datetime-local': if ($entity_value instanceof \DateTime) { $field->setValue($entity_value->format('Y-m-d\\TH:i')); } else { $field->setValue($entity_value); } break; default: $field->setValue($entity_value); break; } } }
/** * Maps data from the form to the entity where the field name matches the name of an entity's property. * Optionally specify a prefix if the form fields have one. * @param FormBase $form Form containing the values to map * @param DomainObjectBase $entity Entity to map the values to * @param string $name_prefix Field name prefix that may be applied to form field names * @param boolean $strict Where a name prefix is supplied, set this to true if you ONLY want to map fields that have a matching prefix (otherwise it will map fields that have the prefix or no prefix) * @return DomainObjectBase */ protected function mapScalarFormElements(FormBase $form, DomainObjectBase $entity, $name_prefix = '', $strict = false) { $properties = $entity->getProperties(true); foreach ($properties as $property) { if ($form->fieldExists($name_prefix . $property)) { $entity->{$property} = $form->getField($name_prefix . $property)->value; } else { if (strlen($name_prefix) > 0 && !$strict) { if ($form->fieldExists($property)) { $entity->{$property} = $form->getField($property)->value; } } } } return $entity; }