You are here

function gc_mapping_edit_form_submit in GatherContent 8

Submit callback for edit mapping form.

@inheritdoc

File

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

Code

function gc_mapping_edit_form_submit($form, &$form_state) {
  if ($form_state['triggering_element']['#id'] == 'edit-submit') {
    $form_definition_elements = array(
      'return',
      'form_build_id',
      'form_token',
      'form_id',
      'op',
    );
    $non_data_elements = array_merge($form_definition_elements, array(
      'gc_template',
      'content_type',
      'id',
      'updated',
    ));
    $mapping_data = array();
    foreach ($form_state['values'] as $key => $value) {
      if (!in_array($key, $non_data_elements) && substr_compare($key, 'tab', 0, 3) === 0) {
        $mapping_data[$key] = $value;
      }
    }
    $mapping_entity = \Drupal::entityManager()
      ->getStorage('gc_mapping', array(
      $form_state['values']['id'],
    ));
    $mapping = reset($mapping_entity);
    $new = empty($mapping->data);
    if ($new) {
      $mapping->content_type = $form_state['values']['content_type'];
    }
    $mapping->data = serialize($mapping_data);
    $mapping->updated_drupal = time();
    $mapping->updated_gc = $form_state['values']['updated'];
    $tmp = new Template();
    $template = $tmp
      ->getTemplate($mapping->gc_template_id);
    $mapping->template = serialize($template);
    $mapping
      ->save();

    // We need to modify field for checkboxes and field instance for radios.
    foreach ($template->config as $i => $fieldset) {
      if ($fieldset->hidden === FALSE) {
        foreach ($fieldset->elements as $gc_field) {
          if ($gc_field->type === 'choice_checkbox') {
            if (!empty($mapping_data[$gc_field->name])) {
              $local_options = array();
              foreach ($gc_field->options as $option) {
                $local_options[$option->name] = $option->label;
              }
              $field_data = array(
                'field_name' => $mapping_data[$gc_field->name],
                'settings' => array(
                  'allowed_values' => $local_options,
                ),
              );
              try {
                $field_data
                  ->save();
              } catch (Exception $e) {

                // Log something.
              }
            }
          }
          elseif ($gc_field->type === 'choice_radio') {
            if (!empty($mapping_data[$gc_field->name])) {
              $local_options = array();
              foreach ($gc_field->options as $option) {
                if ($option != end($gc_field->options)) {
                  $local_options[] = $option->name . "|" . $option->label;
                }
              }
              $instance = field_read_instance('node', $mapping_data[$gc_field->name], $mapping->content_type);

              // Make the change.
              $instance['widget']['settings']['available_options'] = implode("\n", $local_options);

              // Save the instance.
              $instance
                ->save();
            }
          }
        }
      }
    }
    if ($new) {
      drupal_set_message(t('Mapping has been created.', array(
        '@id' => $form_state['values']['id'],
      )));
    }
    else {
      drupal_set_message(t('Mapping has been updated.', array(
        '@id' => $form_state['values']['id'],
      )));
    }
  }
  drupal_goto('admin/config/gc/mapping');
}