You are here

public function GathercontentMappingEditForm::form in GatherContent 8

Gets the actual form array to be built.

Overrides EntityForm::form

See also

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

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

File

src/Form/GathercontentMappingEditForm.php, line 20

Class

GathercontentMappingEditForm
Class GathercontentMappingEditForm.

Namespace

Drupal\gathercontent\Form

Code

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

  /** @var $mapping GathercontentMapping */
  $mapping = $this->entity;
  $content_types = node_type_get_names();
  $tmp = new Template();
  $template = $tmp
    ->getTemplate($mapping
    ->getGathercontentTemplateId());
  $new = !$mapping
    ->hasMapping();
  $form = array();
  $form['#attached']['library'][] = 'gathercontent/theme';
  $form['form_description'] = array(
    '#type' => 'html_tag',
    '#tag' => 'i',
    '#value' => t('Please map your GatherContent Template fields to your Drupal
    Content Type Fields. Please note that a GatherContent field can only be
    mapped to a single Drupal field. So each field can only be mapped to once.'),
  );
  if (!$new) {
    $mapping_data = unserialize($mapping
      ->getData());
    $content_type = $mapping
      ->getContentTypeName();
    $form['info'] = array(
      '#markup' => '<div class="project-name">' . t('Project name: @name', array(
        '@name' => $mapping
          ->getGathercontentProject(),
      )) . '</div>' . '<div class="gather-content">' . t('GatherContent template: @gc_template', array(
        '@gc_template' => $mapping
          ->getGathercontentTemplate(),
      )) . '</div>' . '<div class="drupal-content-type">' . t('Drupal content type: @content_type', array(
        '@content_type' => $content_type,
      )) . '</div>',
    );
    $form['mapping'] = array(
      '#prefix' => '<div id="edit-mapping">',
      '#suffix' => '</div>',
    );
    foreach ($template->config as $i => $fieldset) {
      if ($fieldset->hidden === FALSE) {
        $form['mapping'][$fieldset->name] = array(
          '#type' => 'fieldset',
          '#title' => $fieldset->label,
          '#collapsible' => TRUE,
          '#collapsed' => $i === 0 ? FALSE : TRUE,
          '#tree' => TRUE,
        );
        foreach ($fieldset->elements as $gc_field) {
          $d_fields = array();
          if (isset($form_state
            ->getTriggeringElement()['#name'])) {

            // We need different handling for changed fieldset.
            if ($form_state
              ->getTriggeringElement()['#array_parents'][1] === $fieldset->name) {
              if ($form_state
                ->getTriggeringElement()['#value'] === 'content') {
                $d_fields = self::filter_fields($gc_field, $mapping
                  ->getContentType());
              }
            }
            else {
              if ($form_state
                ->getValue($fieldset->name)['type'] === 'content') {
                $d_fields = self::filter_fields($gc_field, $mapping
                  ->getContentType());
              }
            }
          }
          else {
            if (isset($mapping_data[$fieldset->name]['type']) && $mapping_data[$fieldset->name]['type'] === 'content' || !isset($mapping_data[$fieldset->name]['type'])) {
              $d_fields = self::filter_fields($gc_field, $mapping
                ->getContentType());
            }
          }
          $form['mapping'][$fieldset->name]['elements'][$gc_field->name] = array(
            '#type' => 'select',
            '#options' => $d_fields,
            '#title' => isset($gc_field->label) ? $gc_field->label : $gc_field->title,
            '#default_value' => isset($mapping_data[$fieldset->name]['elements'][$gc_field->name]) ? $mapping_data[$fieldset->name]['elements'][$gc_field->name] : NULL,
            '#empty_option' => t("Don't map"),
          );
        }
      }
    }
  }
  else {
    $form['info'] = array(
      '#markup' => t('Project name: @name', array(
        '@name' => $mapping
          ->getGathercontentProject(),
      )) . '<br>' . t('GatherContent template: @gc_template', array(
        '@gc_template' => $mapping
          ->getGathercontentTemplate(),
      )),
    );
    $form['updated'] = array(
      '#type' => 'value',
      '#value' => $template->updated_at,
    );
    $form['content_type'] = array(
      '#type' => 'select',
      '#title' => t('Drupal Content Types'),
      '#options' => $content_types,
      '#required' => TRUE,
      '#ajax' => array(
        'callback' => 'Drupal\\gathercontent\\Form\\GathercontentMappingEditForm::getMappingTable',
        'wrapper' => 'edit-mapping',
        'method' => 'replace',
        'effect' => 'fade',
      ),
      '#default_value' => $form_state
        ->getValue('content_type'),
    );
    $form['mapping'] = array(
      '#prefix' => '<div id="edit-mapping">',
      '#suffix' => '</div>',
    );
    if ($form_state
      ->hasValue('content_type')) {
      foreach ($template->config as $i => $fieldset) {
        if ($fieldset->hidden === FALSE) {
          $form['mapping'][$fieldset->name] = array(
            '#type' => 'details',
            '#title' => $fieldset->label,
            '#open' => $i === 0 ? TRUE : FALSE,
            '#tree' => TRUE,
          );
          if ($i === 0) {
            $form['mapping'][$fieldset->name]['#prefix'] = '<div id="edit-mapping">';
          }
          if (end($template->config) === $fieldset) {
            $form['mapping'][$fieldset->name]['#suffix'] = '</div>';
          }
          foreach ($fieldset->elements as $gc_field) {
            $d_fields = array();
            $content_type = $form_state
              ->getValue('content_type');
            if ($form_state
              ->getTriggeringElement()['#name'] !== 'content_type') {

              // We need different handling for changed fieldset.
              if ($form_state
                ->getTriggeringElement()['#array_parents'][1] === $fieldset->name) {
                if ($form_state
                  ->getTriggeringElement()['#value'] === 'content') {
                  $d_fields = self::filter_fields($gc_field, $content_type);
                }
              }
              else {
                if ($form_state['values'][$fieldset->name]['type'] === 'content') {
                  $d_fields = self::filter_fields($gc_field, $content_type);
                }
              }
            }
            else {
              $d_fields = self::filter_fields($gc_field, $content_type);
            }
            $form['mapping'][$fieldset->name]['elements'][$gc_field->name] = array(
              '#type' => 'select',
              '#options' => $d_fields,
              '#title' => isset($gc_field->label) ? $gc_field->label : $gc_field->title,
              '#empty_option' => t("Don't map"),
            );
          }
        }
      }
    }
  }
  return $form;
}