You are here

public function EntityExportCsvSettings::buildForm in Entity Export CSV 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 ConfigFormBase::buildForm

File

src/Form/EntityExportCsvSettings.php, line 91

Class

EntityExportCsvSettings
Define entity export csv settings form.

Namespace

Drupal\entity_export_csv\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->getConfiguration();
  $private_system_file = PrivateStream::basePath();
  if (!$private_system_file) {
    $this
      ->messenger()
      ->addWarning($this
      ->t('The private system file is not configured. It is highly recommended to configure it. If not available, exports will use the temporary system file.'));
  }
  $form['entity_types'] = [
    '#type' => 'fieldset',
    "#title" => $this
      ->t('Entity types'),
    '#description' => $this
      ->t('Enable the entity types on which you want allow users be able to export them.'),
    '#tree' => TRUE,
  ];
  $entity_types = $this->manager
    ->getSupportedContentEntityTypes(TRUE);

  // We do not use here a checkboxes to be able later to
  // enable / disable per bundle too.

  /** @var \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type */
  foreach ($entity_types as $entity_type_id => $entity_type) {
    $form['entity_types'][$entity_type_id] = [
      '#type' => 'container',
      '#tree' => TRUE,
      '#prefix' => '<div id="' . $entity_type_id . '">',
      '#suffix' => '</div>',
    ];
    $form['entity_types'][$entity_type_id]['enable'] = [
      '#type' => 'checkbox',
      '#title' => $entity_type
        ->getLabel(),
      '#default_value' => $config
        ->get('entity_types.' . $entity_type_id . '.enable'),
    ];
    $bundles_entity_type = $this->manager
      ->getBundlesPerEntityType($entity_type_id, TRUE);
    $default_bundles = $config
      ->get('entity_types.' . $entity_type_id . '.limit_per_bundle') ?: [];
    $default_bundles = array_filter($default_bundles);
    $bundles = $this
      ->getElementPropertyValue([
      'entity_types',
      $entity_type_id,
      'limit_per_bundle',
    ], $form_state, $default_bundles);
    $bundles = array_filter($bundles);
    $form['entity_types'][$entity_type_id]['limit_per_bundle'] = [
      '#type' => 'checkboxes',
      "#title" => $this
        ->t('Limit bundles for @entity_type', [
        '@entity_type' => $entity_type
          ->getLabel(),
      ]),
      "#description" => $this
        ->t('Leave empty to select all.'),
      '#options' => $bundles_entity_type,
      '#default_value' => $bundles,
      '#attributes' => [
        'class' => [
          'inline-checkboxes',
        ],
      ],
      '#states' => [
        'visible' => [
          ':input[name="entity_types[' . $entity_type_id . '][enable]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
      '#ajax' => [
        'event' => 'change',
        'method' => 'replace',
        'wrapper' => 'bundles-' . $entity_type_id,
        'callback' => [
          $this,
          'ajaxReplaceEntityTypeBundlesCallback',
        ],
      ],
    ];
    $form['entity_types'][$entity_type_id]['bundles'] = [
      '#type' => 'container',
      '#prefix' => '<div id="bundles-' . $entity_type_id . '">',
      '#suffix' => '</div>',
      '#tree' => TRUE,
    ];
    if (!empty($bundles)) {
      foreach ($bundles as $bundle) {
        $bundle_default_fields = $config
          ->get('entity_types.' . $entity_type_id . '.bundles.' . $bundle) ?: [];
        $bundle_default_fields_value = $this
          ->getElementPropertyValue([
          'entity_types',
          $entity_type_id,
          'bundles',
          $bundle,
          'wrapper',
        ], $form_state, $bundle_default_fields);
        $form['entity_types'][$entity_type_id]['bundles'][$bundle] = [
          '#type' => 'details',
          '#title' => $this
            ->t('Fields for bundle @label (machine name: @bundle)', [
            '@label' => $bundles_entity_type[$bundle],
            '@bundle' => $bundle,
          ]),
          '#description' => $this
            ->t('Select the fields you want to be exportable. Leave empty to select all.'),
          "#open" => (bool) (!empty(array_filter($bundle_default_fields_value))),
        ];
        $form['entity_types'][$entity_type_id]['bundles'][$bundle]['wrapper'] = [
          '#type' => 'checkboxes',
          "#title" => $this
            ->t('Fields'),
          '#options' => $this->manager
            ->getBundleFields($entity_type_id, $bundle),
          '#default_value' => $bundle_default_fields_value,
          '#attributes' => [
            'class' => [
              'inline-checkboxes',
            ],
          ],
        ];
      }
    }
  }
  $form['multiple'] = [
    '#type' => 'fieldset',
    "#title" => $this
      ->t('Multiple fields'),
    '#tree' => TRUE,
  ];
  $form['multiple']['columns'] = [
    '#type' => 'select',
    "#title" => $this
      ->t('Number of columns'),
    '#options' => $this
      ->getColumnsOptions(),
    '#default_value' => $config
      ->get('multiple.columns'),
    '#description' => $this
      ->t('Select the maximum number of columns a user can select to export a multiple field into several columns.'),
    '#required' => TRUE,
  ];
  $form['#attached']['library'][] = 'entity_export_csv/admin';
  return parent::buildForm($form, $form_state);
}