You are here

public function ContentUploadSelectForm::buildForm in GatherContent 8.5

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

gathercontent_upload_ui/src/Form/ContentUploadSelectForm.php, line 107

Class

ContentUploadSelectForm
Class ContentUpdateSelectForm.

Namespace

Drupal\gathercontent_upload_ui\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = [];
  if (empty($this->step)) {
    $this->step = 1;
  }

  // Step 1 - select project + select entities.
  // Step 2 - confirm screen.
  if ($this->step === 1) {
    $createdMappings = Mapping::loadMultiple();
    $projects = [];
    $mappingArray = [];
    $migrationIds = [];
    foreach ($createdMappings as $mapping) {

      /** @var \Drupal\gathercontent\Entity\Mapping $mapping */
      if ($mapping
        ->hasMapping()) {
        $projects[$mapping
          ->getGathercontentProjectId()] = $mapping
          ->getGathercontentProject();
        $mappingArray[$mapping
          ->getGathercontentTemplateId()] = [
          'gc_template' => $mapping
            ->getGathercontentTemplate(),
          'id' => $mapping
            ->id(),
        ];
        $migrationIds = array_merge($migrationIds, $mapping
          ->getMigrations());
      }
    }
    $form['project'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Select project'),
      '#options' => $projects,
      '#empty_option' => $this
        ->t('- Select -'),
      '#required' => TRUE,
      '#ajax' => [
        'callback' => '::getContentTable',
        'wrapper' => 'edit-upload',
        'method' => 'replace',
        'effect' => 'fade',
      ],
      '#default_value' => !empty($this->projectId) ? $this->projectId : 0,
      '#description' => $this
        ->t('You can only see projects with mapped templates in the dropdown.'),
    ];
    $form['upload'] = [
      '#prefix' => '<div id="edit-upload">',
      '#suffix' => '</div>',
    ];
    if (($form_state
      ->hasValue('project') || !empty($this->projectId)) && !empty($form_state
      ->getValue('project'))) {
      $form['upload']['filter'] = [
        '#type' => 'markup',
        '#markup' => '<div class="gc-table--filter-wrapper clearfix"></div>',
        '#weight' => 0,
      ];
      $form['upload']['counter'] = [
        '#type' => 'markup',
        '#markup' => '<div class="gc-table--counter"></div>',
        '#weight' => 1,
      ];
      $form['upload']['entities'] = [
        '#tree' => TRUE,
        '#type' => 'table',
        '#header' => [
          'selected' => [
            'class' => [
              'select-all',
            ],
            'data' => '',
          ],
          'title' => $this
            ->t('Item Name'),
          'template' => $this
            ->t('GatherContent Template Name'),
        ],
        '#empty' => $this
          ->t('No content available.'),
        '#weight' => 2,
        '#attributes' => [
          'class' => [
            'tablesorter-enabled',
          ],
        ],
        '#attached' => [
          'library' => [
            'gathercontent_ui/tablesorter-mottie',
            'gathercontent_ui/filter',
          ],
          'drupalSettings' => [
            'gatherContent' => [
              'tableSorterOptionOverrides' => [
                'headers' => [
                  '0' => [
                    'sorter' => FALSE,
                  ],
                  '5' => [
                    'sorter' => FALSE,
                  ],
                ],
              ],
            ],
          ],
        ],
      ];
      $projectId = $form_state
        ->hasValue('project') ? $form_state
        ->getValue('project') : $this->projectId;
      foreach ($createdMappings as $mapping) {

        /** @var \Drupal\gathercontent\Entity\Mapping $mapping */
        if (!$mapping
          ->hasMapping() || $mapping
          ->getGathercontentProjectId() != $projectId) {
          continue;
        }
        $entityType = $mapping
          ->getMappedEntityType();
        $entityStorage = $this->entityTypeManager
          ->getStorage($entityType);
        $bundle = $this->entityTypeManager
          ->getDefinition($entityType)
          ->getKey('bundle');
        $entities = $entityStorage
          ->loadByProperties([
          $bundle => $mapping
            ->getContentType(),
        ]);
        foreach ($entities as $entity) {
          $entityId = $entity
            ->id();
          $key = $entityType . '_' . $entityId;
          $gcId = $this->database
            ->select('gathercontent_entity_mapping')
            ->fields('gathercontent_entity_mapping', [
            'gc_id',
          ])
            ->condition('migration_id', $migrationIds, 'IN')
            ->condition('entity_type', $entityType)
            ->condition('entity_id', $entityId)
            ->execute()
            ->fetchField();
          $this->items[$key] = [
            'template' => $mapping
              ->getGathercontentTemplate(),
            'title' => $entity
              ->label(),
          ];
          $form['upload']['entities'][$key] = [
            '#tree' => TRUE,
            'data' => [
              'selected' => [
                '#type' => 'checkbox',
                '#title' => $this
                  ->t('Selected'),
                '#title_display' => 'invisible',
                '#default_value' => !empty($this->entities[$key]),
                '#attributes' => [
                  'class' => [
                    'gathercontent-select-import-items',
                  ],
                ],
              ],
              'entity_type' => [
                '#type' => 'hidden',
                '#value' => $entityType,
              ],
              'entity_id' => [
                '#type' => 'hidden',
                '#value' => $entityId,
              ],
              'gc_id' => [
                '#type' => 'hidden',
                '#value' => $gcId,
              ],
              'mapping_id' => [
                '#type' => 'hidden',
                '#value' => $mapping
                  ->id(),
              ],
            ],
            'title' => [
              'data' => [
                '#type' => 'markup',
                '#markup' => $entity
                  ->label(),
              ],
              '#wrapper_attributes' => [
                'class' => [
                  'gc-item',
                  'gc-item--name',
                ],
              ],
            ],
            'template' => [
              'data' => [
                '#markup' => $mapping
                  ->getGathercontentTemplate(),
              ],
              '#wrapper_attributes' => [
                'class' => [
                  'template-name-item',
                ],
              ],
            ],
          ];
        }
      }
      $form['upload']['actions']['#type'] = 'actions';
      $form['upload']['actions']['submit'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Next'),
        '#button_type' => 'primary',
        '#weight' => 10,
      ];
      $form['upload']['actions']['back'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Back'),
        '#weight' => 11,
      ];
    }
  }
  elseif ($this->step === 2) {
    $form['title'] = [
      'form_title' => [
        '#type' => 'html_tag',
        '#tag' => 'h2',
        '#value' => $this
          ->formatPlural(count($this->entities), 'Confirm upload selection (@count item)', 'Confirm upload selection (@count items)'),
      ],
      'form_description' => [
        '#type' => 'html_tag',
        '#tag' => 'p',
        '#value' => $this
          ->t('Please review your upload selection before uploading.'),
      ],
    ];
    $header = [
      'title' => $this
        ->t('Item name'),
      'template' => $this
        ->t('GatherContent Template'),
    ];
    $rows = [];
    foreach ($this->entities as $key => $data) {
      $rows[$key] = [
        'title' => $this->items[$key]['title'],
        'template' => $this->items[$key]['template'],
      ];
    }
    $form['table'] = [
      '#type' => 'table',
      '#header' => $header,
      '#rows' => $rows,
    ];
    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Upload'),
      '#button_type' => 'primary',
      '#weight' => 10,
    ];
    $form['actions']['back'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Back'),
      '#weight' => 11,
    ];
  }
  return $form;
}