You are here

public function MappingImportForm::form in GatherContent 8.4

Same name and namespace in other branches
  1. 8.5 gathercontent_ui/src/Form/MappingImportForm.php \Drupal\gathercontent_ui\Form\MappingImportForm::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

gathercontent_ui/src/Form/MappingImportForm.php, line 48

Class

MappingImportForm
Class MappingImportForm.

Namespace

Drupal\gathercontent_ui\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $account_id = DrupalGatherContentClient::getAccountId();

  /** @var \Cheppers\GatherContent\DataTypes\Project[] $projects */
  $projects = [];
  if ($account_id) {
    $projects = $this->client
      ->getActiveProjects($account_id);
  }
  $form['description'] = [
    '#type' => 'html_tag',
    '#tag' => 'div',
    '#value' => t("Please select the GatherContent Templates you'd like to map. Only Templates you've not selected will be listed."),
    '#attributes' => [
      'class' => [
        'description',
      ],
    ],
  ];
  $form['projects'] = [
    '#type' => 'vertical_tabs',
  ];
  $form['template_counter'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'gather-content-counter-message',
      ],
    ],
    '#attached' => [
      'library' => [
        'gathercontent_ui/template_counter',
      ],
    ],
  ];
  $created_mapping_ids = Mapping::loadMultiple();
  $local_templates = [];
  foreach ($created_mapping_ids as $mapping) {

    /** @var \Drupal\gathercontent\Entity\Mapping $mapping */
    $local_templates[$mapping
      ->getGathercontentTemplateId()] = $mapping
      ->getGathercontentTemplate();
  }
  foreach ($projects as $project_id => $project) {
    $remote_templates = $this->client
      ->getTemplatesOptionArray($project_id);
    $templates = array_diff_assoc($remote_templates, $local_templates);
    if (empty($templates)) {
      continue;
    }
    $form['p' . $project_id] = [
      '#type' => 'details',
      '#title' => $project->name,
      '#group' => 'projects',
      '#tree' => TRUE,
    ];
    $form['p' . $project_id]['templates'] = [
      '#type' => 'checkboxes',
      '#title' => $project->name,
      '#options' => $templates,
      '#attributes' => [
        'class' => [
          'gather-content-counted',
        ],
      ],
    ];
  }
  return $form;
}