/**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $base_fields = array_filter($form_state->getValue('base_fields'));
     $new_map = [];
     // Clone Base Fields
     foreach ($base_fields as $base_field) {
         /** @var \Drupal\field\Entity\FieldConfig $new_field */
         $new_field = $this->fieldManager->cloneField($this->entity, $base_field);
         $new_map[$new_field->getName()] = $base_field;
     }
     // Clone Configurable Fields
     if ($config_field_ids = $form_state->getValue('config_fields')) {
         foreach ($config_field_ids as $config_field_id) {
             /** @var FieldConfig $config_definition */
             if ($config_definition = FieldConfig::load($config_field_id)) {
                 $field_name = $config_definition->getFieldStorageDefinition()->getName();
                 $new_field = $this->fieldManager->cloneField($this->entity, $field_name, $config_definition->id());
                 $new_map[$new_field->getName()] = $field_name;
             }
         }
     }
     if ($new_map) {
         // Update Map
         $new_map += $this->entity->getFieldMap();
         $this->entity->setFieldMap($new_map);
         $this->entity->save();
         drupal_set_message($this->t('The fields have been created and mapped.'));
         if ($this->currentUser()->hasPermission('administer scheduled_update form display')) {
             // Redirect to form display so user and adjust settings.
             $form_state->setRedirectUrl(Url::fromRoute("entity.entity_form_display.scheduled_update.default", array($this->entity->getEntityTypeId() => $this->entity->id())));
         } else {
             drupal_set_message($this->t('You do not have permission to administer fields on Scheduled Updates.'), 'warning');
         }
     }
 }
 /**
  * Setup entity reference field for this update type on add.
  *
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  */
 protected function setUpFieldReferences(FormStateInterface $form_state)
 {
     $reference_settings = $form_state->getValue('reference_settings');
     if ($reference_settings['new_field']['create']) {
         $new_field_settings = $reference_settings['new_field'];
         $new_field_settings += $reference_settings['new_field']['cardinality_container'];
         unset($new_field_settings['cardinality_container']);
         $this->fieldManager->createNewReferenceField($new_field_settings, $this->entity);
     }
     if (!empty($reference_settings['existing_fields']['update'])) {
         $existing_field_settings = $reference_settings['existing_fields'];
         $this->fieldManager->updateExistingReferenceFields($existing_field_settings, $this->entity);
     }
 }
 /**
  * Clone a single field from the settings on type add form.
  *
  * This creates a default value for the field if chosen.
  *
  * @param $entity_type
  * @param $bundle
  * @param $clone_field
  */
 protected function cloneSingleField($entity_type,$clone_field, FormStateInterface $form_state, $bundle = NULL) {
   $clone_field_id = NULL;
   $values = $form_state->getValues();
   if (isset($values['default_value_input'])) {
     $default_value = [$values['default_value_input'][$clone_field]];
     $hide = $values['default_value_input']['_no_form_display'];
   }
   else {
     $default_value = [];
     $hide = FALSE;
   }
   if ($bundle) {
     $clone_field_definition = $this->getFieldDefinition($entity_type, $bundle, $clone_field);
     if (!$clone_field_definition instanceof BaseFieldDefinition) {
       $clone_field_id = $clone_field_definition->id();
     }
   }
   $cloned_field = $this->fieldManager->cloneField($this->entity, $clone_field, $clone_field_id, $default_value, $hide);
   $this->entity->setFieldMap([$cloned_field->getName() => $clone_field]);
   $this->entity->save();
 }