You are here

protected function MappingForm::processFormState in Feeds 8.3

Processes the form state, populating the mappings on the feed type.

Parameters

array $form: The complete mapping form.

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

2 calls to MappingForm::processFormState()
MappingForm::buildForm in src/Form/MappingForm.php
Form constructor.
MappingForm::submitForm in src/Form/MappingForm.php
Form submission handler.

File

src/Form/MappingForm.php, line 561

Class

MappingForm
Provides a form for mapping settings.

Namespace

Drupal\feeds\Form

Code

protected function processFormState(array $form, FormStateInterface $form_state) {

  // Process any plugin configuration.
  $triggering_element = $form_state
    ->getTriggeringElement() + [
    '#op' => '',
  ];
  if ($triggering_element['#op'] === 'update') {
    $this->feedType
      ->getTargetPlugin($triggering_element['#delta'])
      ->submitConfigurationForm($form, $form_state);
  }
  $mappings = $this->feedType
    ->getMappings();
  foreach (array_filter((array) $form_state
    ->getValue('mappings', [])) as $delta => $mapping) {
    foreach ($mapping['map'] as $column => $value) {
      if ($value['select'] == '__new') {

        // Add a new source.
        $this->feedType
          ->addCustomSource($value['__new']['machine_name'], [
          'label' => $value['__new']['value'],
        ] + $value['__new']);
        $mappings[$delta]['map'][$column] = $value['__new']['machine_name'];
      }
      else {
        $mappings[$delta]['map'][$column] = $value['select'];
      }
    }
    if (isset($mapping['unique'])) {
      $mappings[$delta]['unique'] = array_filter($mapping['unique']);
    }
  }
  $this->feedType
    ->setMappings($mappings);

  // Remove any mappings.
  foreach (array_keys(array_filter($form_state
    ->getValue('remove_mappings', []))) as $delta) {
    $this->feedType
      ->removeMapping($delta);
  }

  // Add any targets.
  if ($new_target = $form_state
    ->getValue('add_target')) {
    $map = array_fill_keys($this->targets[$new_target]
      ->getProperties(), '');
    $this->feedType
      ->addMapping([
      'target' => $new_target,
      'map' => $map,
    ]);
  }

  // Allow the #default_value of 'add_target' to be reset.
  $input =& $form_state
    ->getUserInput();
  unset($input['add_target']);
}