You are here

public function GathercontentMappingEditForm::save in GatherContent 8

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

src/Form/GathercontentMappingEditForm.php, line 288

Class

GathercontentMappingEditForm
Class GathercontentMappingEditForm.

Namespace

Drupal\gathercontent\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  if ($form_state
    ->getTriggeringElement()['#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
      ->getValues() as $key => $value) {
      if (!in_array($key, $non_data_elements) && substr_compare($key, 'tab', 0, 3) === 0) {
        $mapping_data[$key] = $value;
      }
    }

    /** @var $mapping GathercontentMapping */
    $mapping = $this->entity;
    $new = !$mapping
      ->hasMapping();
    if ($new) {
      $mapping
        ->setContentType($form_state
        ->getValue('content_type'));
      $content_types = node_type_get_names();
      $mapping
        ->setContentTypeName($content_types[$form_state
        ->getValue('content_type')]);
    }
    $mapping
      ->setData(serialize($mapping_data));
    $mapping
      ->setUpdatedDrupal(time());
    $tmp = new Template();
    $template = $tmp
      ->getTemplate($mapping
      ->getGathercontentTemplateId());
    $mapping
      ->setTemplate(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.'));
    }
    else {
      drupal_set_message(t('Mapping has been updated.'));
    }
  }
  $form_state
    ->setRedirect('entity.gathercontent_mapping.collection');
}