You are here

public function GathercontentMappingImportForm::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/GathercontentMappingImportForm.php, line 78

Class

GathercontentMappingImportForm
Class GathercontentMappingImportForm.

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 = array(
          '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');
}