You are here

protected function FieldClonerForm::createFieldsElements in Scheduled Updates 8

Create field elements for all field on the entity type to update.

Parameters

\Drupal\Core\Form\FormStateInterface $form_state:

Return value

array

1 call to FieldClonerForm::createFieldsElements()
FieldClonerForm::buildForm in src/Form/FieldClonerForm.php
Form constructor.

File

src/Form/FieldClonerForm.php, line 98
Contains \Drupal\scheduled_updates\Form\FieldClonerForm.

Class

FieldClonerForm
Class FieldClonerForm.

Namespace

Drupal\scheduled_updates\Form

Code

protected function createFieldsElements(FormStateInterface $form_state) {
  $base_options = [];

  /** @var FieldStorageDefinitionInterface[] $config_fields */
  $config_fields = [];
  $entity_type = $this->entity
    ->getUpdateEntityType();
  $target_entity_label = $this
    ->targetTypeLabel($this->entity);
  $target_bundle_label = $this
    ->targetTypeBundleLabel($this->entity);
  $elements = [
    '#type' => 'container',
  ];
  $destination_fields = $this
    ->getDestinationFields($entity_type);
  $map = $this->entity
    ->getFieldMap();
  foreach ($destination_fields as $destination_field) {
    $field_name = $destination_field
      ->getName();
    if (!in_array($field_name, $map)) {
      if ($destination_field
        ->isBaseField()) {
        $base_options[$field_name] = $destination_field
          ->getLabel();
      }
      else {
        $config_fields[] = $destination_field;
      }
    }
  }
  $elements['base_fields'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Base Fields'),
    '#options' => $base_options,
    '#description' => $this
      ->t('These fields are available on all @label entities and may not be configurable.', [
      '@label' => $target_entity_label,
    ]),
  ];
  $elements['config_fields'] = [
    '#tree' => TRUE,
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Configurable Fields'),
  ];
  if ($this
    ->targetSupportBundles($this->entity)) {
    $elements['config_fields']['#description'] = $this
      ->t('These fields have been added to different @bundle_label bundles of @entity_label. They may not be on all @entity_label entities.', [
      '@entity_label' => $target_entity_label,
      '@bundle_label' => $target_bundle_label,
    ]);
  }
  else {
    $elements['config_fields']['#description'] = $this
      ->t('These fields have been added to @entity_label entities.', [
      '@entity_label' => $target_entity_label,
    ]);
  }
  foreach ($config_fields as $config_field) {
    $instances = $this->fieldManager
      ->getAllFieldConfigsForField($config_field, $entity_type);
    if ($instances) {
      $instance_options = [
        '' => $this
          ->t('(Don\'t clone)'),
      ];
      foreach ($instances as $bundle => $instance) {
        $instance_options[$instance
          ->id()] = $this
          ->t('As it is configured in @bundle as @label', [
          '@bundle' => $bundle,
          '@label' => $instance
            ->getLabel(),
        ]);
      }
      $elements['config_fields'][$config_field
        ->getName()] = [
        '#type' => 'select',
        '#title' => $config_field
          ->getLabel(),
        '#options' => $instance_options,
      ];
    }
  }

  //$this->entity_field_manager->getFieldDefinitions();
  return $elements;
}