You are here

public function FieldBulkCloneForm::submitForm in Field tools 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/FieldBulkCloneForm.php, line 121

Class

FieldBulkCloneForm
Provides a form to clone multiple fields from an entity bundle.

Namespace

Drupal\field_tools\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Get the original parameters given to buildForm().
  // TODO: is this the right way to do this?
  $build_info = $form_state
    ->getBuildInfo();
  list($entity_type_id, $bundle) = $build_info['args'];
  $destinations = array_filter($form_state
    ->getValue('destinations'));
  $fields_to_clone = array_filter($form_state
    ->getValue('fields'));
  foreach ($destinations as $destination) {
    list($destination_entity_type, $destination_bundle) = explode('::', $destination);
    foreach ($fields_to_clone as $field_id) {
      $field_config = $this->entityTypeManager
        ->getStorage('field_config')
        ->load($field_id);

      // Check the field is not already on the destination bundle.
      $field_ids = $this->entityTypeManager
        ->getStorage('field_config')
        ->getQuery()
        ->condition('entity_type', $destination_entity_type)
        ->condition('bundle', $destination_bundle)
        ->condition('field_name', $field_config
        ->getName())
        ->execute();
      if ($field_ids) {
        $this
          ->messenger()
          ->addMessage($this
          ->t("Field @name is already on @entity_type @bundle, skipping.", [
          '@name' => $field_config
            ->getName(),
          // TODO: use labels!
          '@entity_type' => $destination_entity_type,
          '@bundle' => $destination_bundle,
        ]));
        continue;
      }

      // Check the field is not already on the destination entity type but
      // with a different type.
      $existing_destination_field_storage_ids = $this->entityTypeManager
        ->getStorage('field_storage_config')
        ->getQuery()
        ->condition('entity_type', $destination_entity_type)
        ->condition('field_name', $field_config
        ->getName())
        ->execute();
      if ($existing_destination_field_storage_ids) {

        // There will be only one.
        $existing_field_storage_config = $this->entityTypeManager
          ->getStorage('field_storage_config')
          ->load(reset($existing_destination_field_storage_ids));
        if ($existing_field_storage_config
          ->getType() != $field_config
          ->getType()) {
          $this
            ->messenger()
            ->addMessage($this
            ->t("Field @name is already on @entity_type with a different field type, skipping.", [
            '@name' => $field_config
              ->getName(),
            // TODO: use labels!
            '@entity_type' => $destination_entity_type,
          ]));
          continue;
        }
      }
      $this->fieldCloner
        ->cloneField($field_config, $destination_entity_type, $destination_bundle);
    }
  }
  $this
    ->messenger()
    ->addMessage($this
    ->t("The fields have been cloned."));
}