You are here

public function EntityExportCsv::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 FormInterface::buildForm

File

src/Form/EntityExportCsv.php, line 95

Class

EntityExportCsv
Define entity export csv form.

Namespace

Drupal\entity_export_csv\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['#attached']['library'][] = 'entity_export_csv/export_form';
  $form['#attached']['library'][] = 'entity_export_csv/download';
  $options = $this->manager
    ->getContentEntityTypesEnabled(TRUE);
  if (empty($options)) {
    $this
      ->messenger()
      ->addWarning($this
      ->t('No entity type have been configured to be exported.'));
    return [];
  }
  if ($this->languageManager
    ->isMultilingual()) {
    $languages = $this->languageManager
      ->getLanguages();
    $languages_options = [];
    foreach ($languages as $language_id => $language) {
      $languages_options[$language
        ->getId()] = $language
        ->getName();
    }
    $form['langcode'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Language'),
      '#description' => $this
        ->t('Select the language you want export'),
      '#options' => $languages_options,
      '#default_vlue' => $this->languageManager
        ->getDefaultLanguage()
        ->getId(),
    ];
  }
  $user_data = $this->userData
    ->get('entity_export_csv', $this
    ->currentUser()
    ->id(), 'entity_export_csv') ?: [];
  $form['entity_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Entity type'),
    '#required' => TRUE,
    '#options' => $options,
    '#ajax' => [
      'event' => 'change',
      'method' => 'replace',
      'wrapper' => 'bundle-wrapper',
      'callback' => [
        $this,
        'ajaxReplaceBundleCallback',
      ],
    ],
  ];
  $form['bundle_wrapper'] = [
    '#type' => 'container',
    '#prefix' => '<div id="bundle-wrapper">',
    '#suffix' => '</div>',
  ];
  $entity_type_id = $this
    ->getElementPropertyValue('entity_type', $form_state);
  $bundles = $this->manager
    ->getBundlesEnabledPerEntityType($entity_type_id, TRUE);
  if ($entity_type_id) {
    $form['bundle_wrapper']['bundle'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Bundle'),
      '#description' => $this
        ->t('Select the bundle to export.'),
      '#options' => [
        '' => $this
          ->t('- Select -'),
      ] + $bundles,
      '#default_value' => $this
        ->getElementPropertyValue('bundle', $form_state, ''),
      '#required' => TRUE,
      '#ajax' => [
        'event' => 'change',
        'method' => 'replace',
        'wrapper' => 'fields-wrapper',
        'callback' => [
          $this,
          'ajaxReplaceFieldsCallback',
        ],
      ],
    ];
    $form['bundle_wrapper']['fields'] = [
      '#type' => 'container',
      '#prefix' => '<div id="fields-wrapper">',
      '#suffix' => '</div>',
      '#tree' => TRUE,
      '#attributes' => [
        'class' => [
          'inline-elements',
        ],
      ],
    ];
    $bundle = $this
      ->getElementPropertyValue('bundle', $form_state, '');
    if ($bundle) {
      $triggering_element = $form_state
        ->getTriggeringElement();

      // We need to reset the fields user input otherwise when switching
      // between bundle with same fields, the first settings are still selected
      // because the user input is already set for this field (but for
      // an another bundle).
      if (!empty($triggering_element['#name']) && $triggering_element['#name'] === 'bundle') {
        $user_input = $form_state
          ->getUserInput();
        $user_input['fields'] = [];
        $form_state
          ->setUserInput($user_input);
      }
      $form['bundle_wrapper']['fields']['#title'] = $this
        ->t('Select fields to export');
      $fields = $this->manager
        ->getBundleFieldsEnabled($entity_type_id, $bundle, TRUE);
      $user_default_values = [];
      if (!empty($user_data[$entity_type_id][$bundle])) {
        $user_default_values = $user_data[$entity_type_id][$bundle];
      }
      if (!empty($user_default_values)) {
        $this->manager
          ->sortNaturalFields($fields, $user_default_values);
      }
      $default_delimiter = !empty($user_default_values['delimiter']) ? $user_default_values['delimiter'] : ',';
      $form['bundle_wrapper']['fields']['delimiter'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Delimiter'),
        '#default_value' => $this
          ->getElementPropertyValue([
          'fields',
          'delimiter',
        ], $form_state, $default_delimiter, $triggering_element),
        '#options' => $this->manager
          ->getDelimiters(),
        '#wrapper_attributes' => [
          'class' => [
            'reset-flex',
          ],
        ],
        '#states' => [
          'invisible' => [
            ':input[name="entity_type"]' => [
              'value' => '',
            ],
          ],
        ],
      ];
      foreach ($fields as $field_name => $definition) {
        $field_name_class = Html::cleanCssIdentifier($field_name);
        $form['bundle_wrapper']['fields'][$field_name] = [
          '#type' => 'fieldset',
          '#title' => $definition
            ->getLabel(),
          '#tree' => TRUE,
        ];
        $enable_default = isset($user_default_values[$field_name]['enable']) ? $user_default_values[$field_name]['enable'] : TRUE;
        $form['bundle_wrapper']['fields'][$field_name]['enable'] = [
          '#type' => 'checkbox',
          '#title' => $this
            ->t('Enable'),
          '#default_value' => $this
            ->getElementPropertyValue([
            'fields',
            $field_name,
            'enable',
          ], $form_state, $enable_default, $triggering_element),
        ];
        $order_default = isset($user_default_values[$field_name]['order']) ? $user_default_values[$field_name]['order'] : 0;
        $form['bundle_wrapper']['fields'][$field_name]['order'] = [
          '#type' => 'number',
          '#title' => $this
            ->t('Order'),
          '#required' => TRUE,
          '#default_value' => $this
            ->getElementPropertyValue([
            'fields',
            $field_name,
            'order',
          ], $form_state, $order_default, $triggering_element),
        ];
        $field_type = $definition
          ->getType();
        $exporters = $this->fieldTypeExportManager
          ->getFieldTypeOptions($field_type, $entity_type_id, $bundle, $field_name);
        $exporter_ids = array_keys($exporters);
        $default_exporter = isset($user_default_values[$field_name]['exporter']) && isset($exporters[$user_default_values[$field_name]['exporter']]) ? $user_default_values[$field_name]['exporter'] : $this
          ->getDefaultExporterId($exporter_ids);
        $default_exporter_value = $this
          ->getElementPropertyValue([
          'fields',
          $field_name,
          'exporter',
        ], $form_state, $default_exporter, $triggering_element);
        $form['bundle_wrapper']['fields'][$field_name]['exporter'] = [
          '#type' => 'select',
          '#title' => $this
            ->t('Export format'),
          '#options' => $exporters,
          '#default_value' => $default_exporter_value,
          '#required' => TRUE,
          '#ajax' => [
            'event' => 'change',
            'method' => 'replace',
            'wrapper' => 'fields-wrapper',
            'callback' => [
              $this,
              'ajaxReplaceFieldsCallback',
            ],
          ],
        ];
        $form['bundle_wrapper']['fields'][$field_name]['form'] = [
          '#type' => 'container',
          '#prefix' => '<div id="export-form-wrapper-"' . $field_name_class . '>',
          '#suffix' => '</div>',
        ];
        $triggering = $form_state
          ->getTriggeringElement();
        if ($triggering['#name'] === 'fields[' . $field_name . '][exporter]') {
          $default_exporter_value = $triggering['#value'];
        }

        // @TODO handle configuration values
        $configuration_default = isset($user_default_values[$field_name]['form']['options']) ? $user_default_values[$field_name]['form']['options'] : [];
        $configuration = $this
          ->getElementPropertyValue([
          'fields',
          $field_name,
          'form',
          'options',
        ], $form_state, $configuration_default, $triggering_element);

        /** @var \Drupal\entity_export_csv\Plugin\FieldTypeExportInterface $plugin */
        $plugin = $this->fieldTypeExportManager
          ->createInstance($default_exporter_value, $configuration);
        $form['bundle_wrapper']['fields'][$field_name]['form']['options'] = $plugin
          ->buildConfigurationForm([], $form_state, $definition);
      }
      $save_default = TRUE;
      $form['bundle_wrapper']['fields']['save'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Save this settings for this bundle (leave unchecked will delete any settings saved).'),
        '#default_value' => $this
          ->getElementPropertyValue([
          'fields',
          'save',
        ], $form_state, $save_default, $triggering_element),
        '#wrapper_attributes' => [
          'class' => [
            'reset-flex',
          ],
        ],
        '#states' => [
          'invisible' => [
            ':input[name="entity_type"]' => [
              'value' => '',
            ],
          ],
        ],
      ];
    }
  }
  $form['actions']['#type'] = 'actions';
  $form['actions']['export'] = [
    '#type' => 'submit',
    '#name' => 'export',
    '#value' => $this
      ->t('Export'),
    '#states' => [
      'invisible' => [
        ':input[name="entity_type"]' => [
          'value' => '',
        ],
      ],
    ],
  ];
  $form['actions']['save'] = [
    '#type' => 'submit',
    '#name' => 'save',
    '#value' => $this
      ->t('Save settings'),
    '#attributes' => [
      'class' => [
        'btn-secondary',
      ],
    ],
    '#states' => [
      'invisible' => [
        [
          ':input[name="fields[save]"]' => [
            'checked' => FALSE,
          ],
        ],
        'OR',
        [
          ':input[name="entity_type"]' => [
            'value' => '',
          ],
        ],
      ],
    ],
  ];
  return $form;
}