You are here

public function ConvertNodesForm::buildForm in Convert Nodes 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/ConvertNodesForm.php, line 231

Class

ConvertNodesForm
ConvertNodesForm.

Namespace

Drupal\convert_nodes\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  if (isset($this->form)) {
    $form = $this->form;
  }
  $this
    ->messenger()
    ->addWarning($this
    ->t('This module is experimental. PLEASE do not use on production databases without prior testing and a complete database dump.'));
  switch ($this->step) {
    case 1:

      // Get content types and put them in the form.
      $contentTypesList = ConvertNodes::getContentTypes();
      $form['convert_nodes_content_type_from'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('From Content Type'),
        '#options' => $contentTypesList,
      ];
      $form['convert_nodes_content_type_to'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('To Content Type'),
        '#options' => $contentTypesList,
      ];
      $form['actions']['submit'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Next'),
        '#button_type' => 'primary',
      ];
      break;
    case 2:

      // Get the fields.
      $entityManager = $this->entityManager;
      $this->fieldsFrom = $entityManager
        ->getFieldDefinitions('node', $this->fromType);
      $this->fieldsTo = $entityManager
        ->getFieldDefinitions('node', $this->toType);
      $fields_to = ConvertNodes::getToFields($this->fieldsTo);
      $fields_to_names = $fields_to['fields_to_names'];
      $fields_to_types = $fields_to['fields_to_types'];
      $fields_from = ConvertNodes::getFromFields($this->fieldsFrom, $fields_to_names, $fields_to_types);
      $fields_from_names = $fields_from['fields_from_names'];
      $fields_from_form = $fields_from['fields_from_form'];

      // Find missing fields. allowing values to be input later.
      $fields_to_names = array_diff($fields_to_names, [
        'remove',
        'append_to_body',
      ]);
      $this->fieldsNewTo = array_diff(array_keys($fields_to_names), $fields_from_names);
      $form = array_merge($form, $fields_from_form);
      $form['actions']['submit'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Next'),
        '#button_type' => 'primary',
      ];
      break;
    case 3:
      $form['create_new'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Create field values for new fields in target content type'),
      ];
      $form['actions']['submit'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Next'),
        '#button_type' => 'primary',
      ];
      break;
    case 4:

      // Put the to fields in the form for new values.
      foreach ($this->fieldsNewTo as $field_name) {
        if (!in_array($field_name, $this->userInput)) {

          // TODO - Date widgets are relative. Fix.
          // Create an arbitrary entity object.
          $ids = (object) [
            'entity_type' => 'node',
            'bundle' => $this->toType,
            'entity_id' => NULL,
          ];
          $fake_entity = _field_create_entity_from_ids($ids);
          $items = $fake_entity
            ->get($field_name);
          $temp_form_element = [];
          $temp_form_state = new FormState();
          $form[$field_name] = $items
            ->defaultValuesForm($temp_form_element, $temp_form_state);
        }
      }
      $form['actions']['submit'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Next'),
        '#button_type' => 'primary',
      ];
      break;
    case 5:
      $this
        ->messenger()
        ->addWarning($this
        ->t('Are you sure you want to convert all nodes of type <em>@from_type</em> to type <em>@to_type</em>?', [
        '@from_type' => $this->fromType,
        '@to_type' => $this->toType,
      ]));
      $form['actions']['submit'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Convert'),
        '#button_type' => 'primary',
      ];
      break;
  }
  return $form;
}