You are here

public function ContentSingleExportForm::buildForm in Content Synchronization 8.2

Same name and namespace in other branches
  1. 8 src/Form/ContentSingleExportForm.php \Drupal\content_sync\Form\ContentSingleExportForm::buildForm()
  2. 3.0.x src/Form/ContentSingleExportForm.php \Drupal\content_sync\Form\ContentSingleExportForm::buildForm()

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/ContentSingleExportForm.php, line 76

Class

ContentSingleExportForm
Provides a form for exporting a single content file.

Namespace

Drupal\content_sync\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $content_type = 'node', $content_name = NULL, $content_entity = NULL) {
  $entity_types = [];
  $entity_type_definitions = $this->entityTypeManager
    ->getDefinitions();
  foreach ($entity_type_definitions as $entity_type => $definition) {
    if ($definition instanceof ContentEntityType) {
      $entity_types[$entity_type] = $definition
        ->getLabel();
    }
  }
  uasort($entity_types, 'strnatcasecmp');
  $content_types = $entity_types;
  $form['content_type'] = [
    '#title' => $this
      ->t('Content type'),
    '#type' => 'select',
    '#options' => $content_types,
    '#default_value' => $content_type,
    '#attributes' => array(
      'onchange' => 'this.form.content_name.value = null; if(this.form.content_entity){ this.form.content_entity.value = null; } this.form.export.value = null; this.form.submit();',
    ),
  ];
  $default_type = $form_state
    ->getValue('content_type', $content_type);
  $default_name = $form_state
    ->getValue('content_name', $content_name);
  $form['content_name'] = [
    '#title' => $this
      ->t('Content name'),
    '#type' => 'select',
    '#options' => $this
      ->findContent($default_type),
    '#default_value' => $content_name,
    '#attributes' => array(
      'onchange' => 'if(this.form.content_entity){ this.form.content_entity.value = null; } this.form.export.value = null; this.form.submit();',
    ),
  ];

  // Auto-complete field for the content entity
  if ($default_type && $default_name) {
    $form['content_entity'] = [
      '#title' => $this
        ->t('Content Entity'),
      '#type' => 'entity_autocomplete',
      '#target_type' => $default_type,
      '#selection_handler' => 'default',
      '#selection_settings' => [
        'target_bundles' => [
          $default_name,
        ],
      ],
      '#ajax' => [
        'callback' => '::updateExport',
        'wrapper' => 'edit-export-wrapper',
        'event' => 'autocompleteclose',
      ],
    ];

    // Autocomplete doesn't support target bundles parameter on bundle-less entities.
    $target_type = $this->entityTypeManager
      ->getDefinition($default_type);
    $target_type_bundles = $target_type
      ->getBundleEntityType();
    if (is_null($target_type_bundles)) {
      unset($form['content_entity']['#selection_settings']);
    }
  }
  $form['export'] = [
    '#title' => $this
      ->t('Here is your configuration:'),
    '#type' => 'textarea',
    '#rows' => 24,
    '#prefix' => '<div id="edit-export-wrapper">',
    '#suffix' => '</div>',
  ];
  return $form;
}