/** * 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; }
/** * @param DomainObjectBase $domain_object * @param int $direction * @return DomainObjectBase|FormBase */ protected function mapEntityFields(DomainObjectBase $domain_object, $direction = self::DIRECTION_ENTITY_TO_FORM, $labels_only = false) { $properties = $domain_object->getProperties(false, true); foreach ($properties as $property => $data_type) { if ($domain_object->{$property} instanceof DomainObjectBase) { $child_object = $domain_object->{$property}; $child_properties = $child_object->getProperties(true); $prefix = $property . '_'; $this->applyPropertyMappings($child_object, $child_properties, $direction, $prefix, $labels_only); } else { if ($data_type == 'DateTime' || $data_type == '\\DateTime') { if ($direction == self::DIRECTION_ENTITY_TO_FORM) { $date_value = $domain_object->{$property}; if ($date_value && $date_value instanceof \DateTime) { $this->mapEntityValueToField($property, $domain_object->{$property}); } } else { $date_field = $this->form->getField($property, true); if ($date_field) { $date_value = $date_field->getValue(); if ($date_value) { $date_value = new \DateTime($date_value); } $this->mapFieldValueToEntity($property, $date_value ? $date_value : null, $domain_object); } } } } } return $this->applyPropertyMappings($domain_object, $domain_object->getProperties(true), $direction, '', $labels_only); }