You are here

public function MappingForm::customSourceExists in Feeds 8.3

Checks if a particular source already exists on the saved feed type.

Parameters

string $name: The name to check.

array $element: The form element using the machine name.

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

Return value

bool True if the source already exists, false otherwise.

File

src/Form/MappingForm.php, line 514

Class

MappingForm
Provides a form for mapping settings.

Namespace

Drupal\feeds\Form

Code

public function customSourceExists($name, array $element, FormStateInterface $form_state) {

  // Get unchanged feed type.
  $unchanged_feed_type = $this->feedTypeStorage
    ->loadUnchanged($this->feedType
    ->getOriginalId());

  // Check if the custom source already exists on the last saved feed type.
  if ($unchanged_feed_type && $unchanged_feed_type
    ->customSourceExists($name)) {
    return TRUE;
  }

  // Get the delta and the column of the passed form element. The delta is the
  // position of the mapping row on the form, the column refers to a property
  // of the target plugin.
  $element_delta = $element['#array_parents'][1];
  $element_column = $element['#array_parents'][3];

  // Check other mappings.
  foreach ($form_state
    ->getValue('mappings') as $delta => $mapping) {
    foreach ($mapping['map'] as $column => $value) {

      // Check if this value belongs to our own element.
      if ($delta == $element_delta && $element_column == $column) {

        // Don't compare name to our own element.
        continue;
      }

      // Check if for this mapping row a new source is selected.
      if ($value['select'] == '__new') {

        // Compare the new source's name with the name to check.
        $map_name = $mappings[$delta]['map'][$column] = $value['__new']['machine_name'];
        if ($name == $map_name) {

          // Name is already used by an other mapper.
          return TRUE;
        }
      }
    }
  }

  // Name does not exist yet for custom source.
  return FALSE;
}