You are here

protected function ScheduledUpdateTypeBaseForm::cloneSingleField in Scheduled Updates 8

Clone a single field from the settings on type add form.

This creates a default value for the field if chosen.

Parameters

$entity_type:

$bundle:

$clone_field:

2 calls to ScheduledUpdateTypeBaseForm::cloneSingleField()
ScheduledUpdateTypeAddAsFieldForm::save in src/Form/ScheduledUpdateTypeAddAsFieldForm.php
Form submission handler for the 'save' action.
ScheduledUpdateTypeForm::save in src/Form/ScheduledUpdateTypeForm.php
Form submission handler for the 'save' action.

File

src/Form/ScheduledUpdateTypeBaseForm.php, line 741
Contains \Drupal\scheduled_updates\Form\ScheduledUpdateTypeBaseForm.

Class

ScheduledUpdateTypeBaseForm

Namespace

Drupal\scheduled_updates\Form

Code

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();
}