You are here

public function FieldBulkCloneForm::buildForm in Field tools 8

Form constructor.

Parameters

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

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

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/FieldBulkCloneForm.php, line 77

Class

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

Namespace

Drupal\field_tools\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL, $bundle = NULL) {
  $field_ids = $this->entityTypeManager
    ->getStorage('field_config')
    ->getQuery()
    ->condition('entity_type', $entity_type_id)
    ->condition('bundle', $bundle)
    ->execute();
  $current_bundle_fields = $this->entityTypeManager
    ->getStorage('field_config')
    ->loadMultiple($field_ids);
  $field_options = array();
  foreach ($current_bundle_fields as $field_id => $field) {
    $field_options[$field_id] = $this
      ->t("@field-label (machine name: @field-name)", array(
      '@field-label' => $field
        ->getLabel(),
      '@field-name' => $field
        ->getName(),
    ));
  }
  asort($field_options);
  $form['fields'] = array(
    '#title' => $this
      ->t('Fields to clone'),
    '#type' => 'checkboxes',
    '#options' => $field_options,
    '#description' => $this
      ->t("Select fields to clone onto one or more bundles."),
  );
  $form['destinations'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t("Bundles to clone the fields to"),
    '#options' => $this
      ->getDestinationOptions($this->entityTypeManager, $this->entityTypeBundleInfo),
  ];

  // Mark the current bundle as disabled.
  $form['destinations']["{$entity_type_id}::{$bundle}"]['#disabled'] = TRUE;
  $form['destinations']["{$entity_type_id}::{$bundle}"]['#description'] = $this
    ->t("This is the current bundle.");
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => $this
      ->t('Clone fields'),
  );
  return $form;
}