/** * Create form elements to update field map. * * @return array * @internal param array $form * @internal param $scheduled_update_type * */ protected function createFieldMapElements() { if ($this->entity->isNew()) { return []; } $field_map_help = 'Select the destination fields for this update type.' . ' Not all field have to have destinations if you using them for other purposes.'; $elements = ['#type' => 'details', '#title' => 'destination fields', '#description' => $this->t($field_map_help), '#tree' => TRUE]; $source_fields = $this->getSourceFields($this->entity); $field_map = $this->entity->getFieldMap(); foreach ($source_fields as $source_field_id => $source_field) { $destination_fields_options = $this->getDestinationFieldsOptions($source_field); $elements[$source_field_id] = ['#type' => 'select', '#title' => $source_field->label(), '#options' => ['' => $this->t("(Not mapped)")] + $destination_fields_options, '#default_value' => isset($field_map[$source_field_id]) ? $field_map[$source_field_id] : '']; } return $elements; }
/** * Transfer field values from update to entity to be updated. * * Because different fields may be on different bundles * not all fields will be transferred to all entities. * * @param $update * @param $entity_to_update */ protected function transferFieldValues(ScheduledUpdateInterface $update, ContentEntityInterface $entity_to_update) { $field_map = $this->scheduled_update_type->getFieldMap(); foreach ($field_map as $from_field => $to_field) { if ($to_field) { if ($entity_to_update->hasField($to_field) && $update->hasField($from_field)) { $new_value = $update->get($from_field)->getValue(); // @todo if $new_value is empty. Check to see if the field is required on the target? // If it is require don't update value because this cause a fatal at least on base fields. if (isset($new_value)) { $entity_to_update->set($to_field, $new_value); } } } } }