You are here

function gathercontent_mapping_edit_form in GatherContent 7.3

Edit mapping form.

@inheritdoc

2 string references to 'gathercontent_mapping_edit_form'
gathercontent_form_alter in ./gathercontent.module
Implements hook_form_alter().
gathercontent_menu in ./gathercontent.module
Implements hook_menu().

File

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

Code

function gathercontent_mapping_edit_form($form, &$form_state, $id) {
  $mapping_entity = entity_load('gathercontent_mapping', array(
    $id,
  ));
  $mapping = reset($mapping_entity);
  $content_types = node_type_get_names();
  $tmp = new Template();
  $template = $tmp
    ->getTemplate($mapping->gathercontent_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->gathercontent_project,
      )) . '</div>' . '<div class="gather-content">' . t('GatherContent template: @gathercontent_template', array(
        '@gathercontent_template' => $mapping->gathercontent_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 (module_exists('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' => 'gathercontent_mapping_form_mapping_get_table',
              'wrapper' => 'edit-mapping',
              'method' => 'replace',
              'effect' => 'fade',
            ),
          );
        }
        if (module_exists('entity_translation') && module_exists('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 $gathercontent_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($gathercontent_field, $mapping->content_type);
              }
              elseif ($form_state['triggering_element']['#value'] === 'metatag') {
                $d_fields = _filter_metatag($gathercontent_field);
              }
            }
            else {
              if ($form_state['values'][$fieldset->name]['type'] === 'content') {
                $d_fields = _filter_fields($gathercontent_field, $mapping->content_type);
              }
              else {
                $d_fields = _filter_metatag($gathercontent_field);
              }
            }
          }
          else {
            if (isset($mapping_data[$fieldset->name]['type']) && $mapping_data[$fieldset->name]['type'] === 'content' || !isset($mapping_data[$fieldset->name]['type'])) {
              $d_fields = _filter_fields($gathercontent_field, $mapping->content_type);
            }
            else {
              $d_fields = _filter_metatag($gathercontent_field);
            }
          }
          $form['mapping'][$fieldset->name]['elements'][$gathercontent_field->name] = array(
            '#type' => 'select',
            '#options' => $d_fields,
            '#title' => isset($gathercontent_field->label) ? $gathercontent_field->label : $gathercontent_field->title,
            '#default_value' => isset($mapping_data[$fieldset->name]['elements'][$gathercontent_field->name]) ? $mapping_data[$fieldset->name]['elements'][$gathercontent_field->name] : NULL,
            '#empty_option' => t('- Select -'),
          );
        }
      }
    }
  }
  else {
    $form = array();
    $form['info'] = array(
      '#markup' => t('Project name: @name', array(
        '@name' => $mapping->gathercontent_project,
      )) . '<br>' . t('GatherContent template: @gathercontent_template', array(
        '@gathercontent_template' => $mapping->gathercontent_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' => 'gathercontent_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 (module_exists('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' => 'gathercontent_mapping_form_mapping_get_table',
                'wrapper' => 'edit-mapping',
                'method' => 'replace',
                'effect' => 'fade',
              ),
            );
          }
          if (module_exists('entity_translation') && module_exists('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 $gathercontent_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($gathercontent_field, $form_state['values']['content_type']);
                }
                elseif ($form_state['triggering_element']['#value'] === 'metatag') {
                  $d_fields = _filter_metatag($gathercontent_field);
                }
              }
              else {
                if ($form_state['values'][$fieldset->name]['type'] === 'content') {
                  $d_fields = _filter_fields($gathercontent_field, $form_state['values']['content_type']);
                }
                else {
                  $d_fields = _filter_metatag($gathercontent_field);
                }
              }
            }
            else {
              $d_fields = _filter_fields($gathercontent_field, $form_state['values']['content_type']);
            }
            $form['mapping'][$fieldset->name]['elements'][$gathercontent_field->name] = array(
              '#type' => 'select',
              '#options' => $d_fields,
              '#title' => isset($gathercontent_field->label) ? $gathercontent_field->label : $gathercontent_field->title,
              '#empty_option' => t("Don't map"),
              '#default_value' => isset($form_state['values'][$fieldset->name]['elements'][$gathercontent_field->name]) ? $form_state['values'][$fieldset->name]['elements'][$gathercontent_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;
}