Example #1
0
 /**
  * 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;
 }
Example #2
0
 /**
  * @param DomainObjectBase $domain_object
  * @param array $properties
  * @param int $direction
  * @return DomainObjectBase|FormBase
  */
 protected function applyPropertyMappings(DomainObjectBase $domain_object, $properties, $direction, $prefix = '', $labels_only = false)
 {
     foreach ($properties as $property) {
         $field = null;
         $field_name = null;
         $entity_value = null;
         $form_value = null;
         if ($this->form->fieldExists($prefix . $property)) {
             $field_name = $prefix . $property;
         }
         if ($field_name !== null) {
             $entity_value = $domain_object->{$property};
             $field = $this->form->getField($field_name);
             if ($labels_only && !$field instanceof FieldLabel) {
                 $field = null;
             } else {
                 $form_value = $field->value;
             }
         }
         if ($field != null) {
             if ($direction == self::DIRECTION_ENTITY_TO_FORM) {
                 if ($entity_value !== null) {
                     $this->mapEntityValueToField($field_name, $this->convertCountries($field, $entity_value, true));
                 }
             } else {
                 if ($form_value !== null && !$this->form->getField($field_name)->is_read_only) {
                     $this->mapFieldValueToEntity($property, $this->convertCountries($field, $form_value, false), $domain_object);
                 }
             }
         }
     }
     if ($direction == self::DIRECTION_ENTITY_TO_FORM) {
         return $this->form;
     } else {
         return $domain_object;
     }
 }