You are here

public function MappingImportForm::save in GatherContent 8.4

Same name and namespace in other branches
  1. 8.5 gathercontent_ui/src/Form/MappingImportForm.php \Drupal\gathercontent_ui\Form\MappingImportForm::save()

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

gathercontent_ui/src/Form/MappingImportForm.php, line 125

Class

MappingImportForm
Class MappingImportForm.

Namespace

Drupal\gathercontent_ui\Form

Code

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

    // Load all projects.
    $account_id = DrupalGatherContentClient::getAccountId();
    if (!$account_id) {
      drupal_set_message($this
        ->t('No available accounts.'), 'error');
      $form_state
        ->setRedirect('entity.gathercontent_mapping.collection');
      return;
    }

    /** @var \Cheppers\GatherContent\DataTypes\Project[] $projects */
    $projects = $this->client
      ->getActiveProjects($account_id);
    $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) {
        $template = $this->client
          ->templateGet($template_id);
        $templateBody = $this->client
          ->getBody(TRUE);
        $mapping_values = [
          'id' => $template_id,
          'gathercontent_project_id' => $template->projectId,
          'gathercontent_project' => $projects[$template->projectId]->name,
          'gathercontent_template_id' => $template_id,
          'gathercontent_template' => $template->name,
          'template' => serialize($templateBody),
        ];
        $mapping = \Drupal::entityManager()
          ->getStorage('gathercontent_mapping')
          ->create($mapping_values);
        if (is_object($mapping)) {
          $mapping
            ->save();
        }
      }
    }
  }
  $form_state
    ->setRedirect('entity.gathercontent_mapping.collection');
}