You are here

function gc_mapping_edit_form in GatherContent 8

Edit mapping form.

@inheritdoc

1 string reference to 'gc_mapping_edit_form'
gc_form_alter in ./_old.gc.module
Implements hook_form_alter().

File

forms/gc.mapping-edit.inc, line 19
Multistep mapping form.

Code

function gc_mapping_edit_form($form, &$form_state, $id) {
  $mapping_entity = \Drupal::entityManager()
    ->getStorage('gc_mapping', array(
    $id,
  ));
  $mapping = reset($mapping_entity);
  $content_types = node_type_get_names();
  $tmp = new Template();
  $template = $tmp
    ->getTemplate($mapping->gc_template_id);
  $new = empty($mapping->data);
  $form = array();
  $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->data);
    $content_type = $content_types[$mapping->content_type];
    $form['info'] = array(
      '#markup' => '<div class="project-name">' . t('Project name: @name', array(
        '@name' => $mapping->gc_project,
      )) . '</div>' . '<div class="gather-content">' . t('GatherContent template: @gc_template', array(
        '@gc_template' => $mapping->gc_template,
      )) . '</div>' . '<div class="drupal-content-type">' . t('Drupal content type: @content_type', array(
        '@content_type' => $content_type,
      )) . '</div>',
    );
    $form['id'] = array(
      '#type' => 'value',
      '#value' => $id,
    );
    $form['updated'] = array(
      '#type' => 'value',
      '#value' => $template->updated_at,
    );
    $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,
        );
        if (\Drupal::moduleHandler()
          ->moduleExists('metatag')) {
          $form['mapping'][$fieldset->name]['type'] = array(
            '#type' => 'select',
            '#options' => array(
              'content' => t('Content'),
              'metatag' => t('Metatag'),
            ),
            '#title' => t('Type'),
            '#default_value' => isset($mapping_data[$fieldset->name]['type']) || isset($form_state['values'][$fieldset->name]['type']) ? isset($form_state['values'][$fieldset->name]['type']) ? $form_state['values'][$fieldset->name]['type'] : $mapping_data[$fieldset->name]['type'] : NULL,
            '#ajax' => array(
              'callback' => 'gc_mapping_form_mapping_get_table',
              'wrapper' => 'edit-mapping',
              'method' => 'replace',
              'effect' => 'fade',
            ),
          );
        }
        if (\Drupal::moduleHandler()
          ->moduleExists('entity_translation') && \Drupal::moduleHandler()
          ->moduleExists('title') && entity_translation_node_supported_type($mapping->content_type) && title_field_replacement_enabled('node', $mapping->content_type, 'title')) {
          $form['mapping'][$fieldset->name]['language'] = array(
            '#type' => 'select',
            '#options' => array(
              'und' => t('None'),
            ) + locale_language_list('name'),
            '#title' => t('Language'),
            '#default_value' => isset($mapping_data[$fieldset->name]['language']) ? $mapping_data[$fieldset->name]['language'] : NULL,
          );
        }
        foreach ($fieldset->elements as $gc_field) {
          $d_fields = array();
          if (isset($form_state['triggering_element']['#name'])) {

            // We need different handling for changed fieldset.
            if ($form_state['triggering_element']['#array_parents'][1] === $fieldset->name) {
              if ($form_state['triggering_element']['#value'] === 'content') {
                $d_fields = _filter_fields($gc_field, $mapping->content_type);
              }
              elseif ($form_state['triggering_element']['#value'] === 'metatag') {
                $d_fields = _filter_metatag($gc_field);
              }
            }
            else {
              if ($form_state['values'][$fieldset->name]['type'] === 'content') {
                $d_fields = _filter_fields($gc_field, $mapping->content_type);
              }
              else {
                $d_fields = _filter_metatag($gc_field);
              }
            }
          }
          else {
            if ($mapping_data[$fieldset->name]['type'] === 'content') {
              $d_fields = _filter_fields($gc_field, $mapping->content_type);
            }
            else {
              $d_fields = _filter_metatag($gc_field);
            }
          }
          $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('- Select -'),
          );
        }
      }
    }
  }
  else {
    $form['info'] = array(
      '#markup' => t('Project name: @name', array(
        '@name' => $mapping->gc_project,
      )) . '<br>' . t('GatherContent template: @gc_template', array(
        '@gc_template' => $mapping->gc_template,
      )),
    );
    $form['id'] = array(
      '#type' => 'value',
      '#value' => $id,
    );
    $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' => 'gc_mapping_form_mapping_get_table',
        'wrapper' => 'edit-mapping',
        'method' => 'replace',
        'effect' => 'fade',
      ),
      '#default_value' => isset($form_state['values']['content_type']) ? $form_state['values']['content_type'] : NULL,
    );
    $form['mapping'] = array(
      '#prefix' => '<div id="edit-mapping">',
      '#suffix' => '</div>',
    );
    if (isset($form_state['values']['content_type'])) {
      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,
          );
          if ($i === 0) {
            $form['mapping'][$fieldset->name]['#prefix'] = '<div id="edit-mapping">';
          }
          if (end($template->config) === $fieldset) {
            $form['mapping'][$fieldset->name]['#suffix'] = '</div>';
          }
          if (\Drupal::moduleHandler()
            ->moduleExists('metatag')) {
            $form['mapping'][$fieldset->name]['type'] = array(
              '#type' => 'select',
              '#options' => array(
                'content' => t('Content'),
                'metatag' => t('Metatag'),
              ),
              '#title' => t('Type'),
              '#default_value' => isset($form_state['values'][$fieldset->name]['type']) ? $form_state['values'][$fieldset->name]['type'] : 'content',
              '#ajax' => array(
                'callback' => 'gc_mapping_form_mapping_get_table',
                'wrapper' => 'edit-mapping',
                'method' => 'replace',
                'effect' => 'fade',
              ),
            );
          }
          if (\Drupal::moduleHandler()
            ->moduleExists('entity_translation') && \Drupal::moduleHandler()
            ->moduleExists('title') && entity_translation_node_supported_type($form_state['values']['content_type']) && title_field_replacement_enabled('node', $form_state['values']['content_type'], 'title')) {
            $form['mapping'][$fieldset->name]['language'] = array(
              '#type' => 'select',
              '#options' => array(
                'und' => t('None'),
              ) + locale_language_list('name'),
              '#title' => t('Language'),
              '#default_value' => isset($form_state['values'][$fieldset->name]['language']) ? $form_state['values'][$fieldset->name]['language'] : 'und',
            );
          }
          foreach ($fieldset->elements as $gc_field) {
            $d_fields = array();
            if ($form_state['triggering_element']['#name'] !== 'content_type') {

              // We need different handling for changed fieldset.
              if ($form_state['triggering_element']['#array_parents'][1] === $fieldset->name) {
                if ($form_state['triggering_element']['#value'] === 'content') {
                  $d_fields = _filter_fields($gc_field, $form_state['values']['content_type']);
                }
                elseif ($form_state['triggering_element']['#value'] === 'metatag') {
                  $d_fields = _filter_metatag($gc_field);
                }
              }
              else {
                if ($form_state['values'][$fieldset->name]['type'] === 'content') {
                  $d_fields = _filter_fields($gc_field, $form_state['values']['content_type']);
                }
                else {
                  $d_fields = _filter_metatag($gc_field);
                }
              }
            }
            else {
              $d_fields = _filter_fields($gc_field, $form_state['values']['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"),
              '#default_value' => isset($form_state['values'][$fieldset->name]['elements'][$gc_field->name]) ? $form_state['values'][$fieldset->name]['elements'][$gc_field->name] : NULL,
            );
          }
        }
      }
    }
  }
  if (!$new || isset($form_state['values']['content_type'])) {
    $form['mapping']['submit'] = array(
      '#type' => 'submit',
      '#value' => !$new ? t('Update mapping') : t('Create mapping'),
    );
  }
  $form['cancel'] = array(
    '#type' => 'submit',
    '#value' => t('Cancel'),
  );
  return $form;
}