You are here

public function MappingImportForm::save in GatherContent 8.3

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/MappingImportForm.php, line 96

Class

MappingImportForm
Class MappingImportForm.

Namespace

Drupal\gathercontent\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  if ($form_state
    ->getTriggeringElement()['#id'] == 'edit-submit') {

    // Load all projects.
    $pr_obj = new Project();
    $projects = $pr_obj
      ->getProjects();
    $values = $form_state
      ->getValues();
    foreach ($values as $k => $tree) {
      if (!is_array($tree)) {
        continue;
      }
      $templates = array_filter($values[$k]['templates']);
      foreach ($templates as $template_id => $selected) {
        $tmp_obj = new Template();
        $template = $tmp_obj
          ->getTemplate($template_id);
        $mapping_values = [
          'id' => $template_id,
          'gathercontent_project_id' => $template->project_id,
          'gathercontent_project' => $projects[$template->project_id],
          'gathercontent_template_id' => $template_id,
          'gathercontent_template' => $template->name,
          'template' => serialize($template),
        ];
        $mapping = \Drupal::entityManager()
          ->getStorage('gathercontent_mapping')
          ->create($mapping_values);
        if (is_object($mapping)) {
          $mapping
            ->save();
        }
      }
    }
  }
  $form_state
    ->setRedirect('entity.gathercontent_mapping.collection');
}