You are here

public function EntityDisplaySettingsBulkCopyForm::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/EntityDisplaySettingsBulkCopyForm.php, line 105

Class

EntityDisplaySettingsBulkCopyForm
Provides a form to copy displays settings between displays.

Namespace

Drupal\field_tools\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL, $bundle = NULL) {
  $bundle_fields = array_filter($this->entityFieldManager
    ->getFieldDefinitions($entity_type_id, $bundle), function ($field_definition) {
    return !$field_definition
      ->isComputed();
  });
  foreach ($bundle_fields as $field_name => $field_definition) {
    $source_field_options[$field_name] = $field_definition
      ->getLabel();
  }
  natcasesort($source_field_options);
  $form['source_fields'] = array(
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Fields to copy'),
    '#description' => $this
      ->t("Select the fields whose display settings should be copied."),
    '#options' => $source_field_options,
    '#required' => TRUE,
  );

  // Source display.
  $form['source_display'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Source display'),
    '#description' => $this
      ->t("Select the display to copy settings from and to"),
    '#required' => TRUE,
    '#options' => $this
      ->getDisplayOptions('entity_form_display', $entity_type_id, $bundle),
    // Workaround for core bug: https://www.drupal.org/node/2906113
    '#empty_value' => '',
  );

  // Destination bundles.
  $destination_bundle_options = [];
  $other_bundles = $this->entityTypeBundleInfo
    ->getBundleInfo($entity_type_id);
  foreach ($other_bundles as $other_bundle_name => $info) {
    $destination_bundle_options[$other_bundle_name] = $info['label'];
  }
  $form['destination_bundles'] = array(
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Destination bundles'),
    '#description' => $this
      ->t("Select the bundles to copy the settings to."),
    '#options' => $destination_bundle_options,
    '#required' => TRUE,
  );

  // Mark the current bundle as disabled.
  $form['destination_bundles'][$bundle]['#disabled'] = TRUE;
  $form['destination_bundles'][$bundle]['#description'] = $this
    ->t("This is the current bundle.");
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => $this
      ->t('Copy field settings'),
  );
  return $form;
}