You are here

protected function MappingForm::buildRow in Feeds 8.3

Builds a single mapping row.

Parameters

array $form: The complete mapping form.

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

array $mapping: A single configured mapper, which is expected to consist of the following:

  • map An array of target subfield => source field.
  • target The name of the target plugin.
  • unique (optional) An array of subfield => enabled as unique.
  • settings (optional) An array of settings for the target.

int $delta: The index number of the mapping.

Return value

array The form structure for a single mapping row.

1 call to MappingForm::buildRow()
MappingForm::buildForm in src/Form/MappingForm.php
Form constructor.

File

src/Form/MappingForm.php, line 202

Class

MappingForm
Provides a form for mapping settings.

Namespace

Drupal\feeds\Form

Code

protected function buildRow(array $form, FormStateInterface $form_state, array $mapping, $delta) {
  try {

    /** @var \Drupal\feeds\Plugin\Type\TargetInterface $plugin */
    $plugin = $this->feedType
      ->getTargetPlugin($delta);
  } catch (MissingTargetException $e) {

    // The target plugin is missing!
    $this
      ->messenger()
      ->addWarning($e
      ->getMessage());
    watchdog_exception('feeds', $e);
    $plugin = NULL;
  }

  // Check if the target exists.
  if (!empty($this->targets[$mapping['target']])) {

    /** @var \Drupal\feeds\TargetDefinitionInterface $target_definition */
    $target_definition = $this->targets[$mapping['target']];
  }
  else {

    // The target is missing! Create a placeholder target definition, so that
    // the mapping row is still being displayed.
    $target_definition = MissingTargetDefinition::create();
  }
  $ajax_delta = -1;
  $triggering_element = (array) $form_state
    ->getTriggeringElement() + [
    '#op' => '',
  ];
  if ($triggering_element['#op'] === 'configure') {
    $ajax_delta = $form_state
      ->getTriggeringElement()['#delta'];
  }
  $row = [
    '#attributes' => [
      'class' => [
        'draggable',
        'tabledrag-leaf',
      ],
    ],
  ];
  $row['map'] = [
    '#type' => 'container',
  ];
  $row['targets'] = [
    '#theme' => 'item_list',
    '#items' => [],
    '#attributes' => [
      'class' => [
        'target',
      ],
    ],
  ];
  if ($target_definition instanceof MissingTargetDefinition) {
    $row['#attributes']['class'][] = 'missing-target';
    $row['#attributes']['class'][] = 'color-error';
  }
  foreach ($mapping['map'] as $column => $source) {
    if (!$target_definition
      ->hasProperty($column)) {
      unset($mapping['map'][$column]);
      continue;
    }
    $row['map'][$column] = [
      'select' => [
        '#type' => 'select',
        '#options' => $this->sourceOptions,
        '#default_value' => $source,
        '#empty_option' => $this
          ->t('- Select a source -'),
        '#attributes' => [
          'class' => [
            'feeds-table-select-list',
          ],
        ],
      ],
      '__new' => [
        '#type' => 'container',
        '#states' => [
          'visible' => [
            ':input[name="mappings[' . $delta . '][map][' . $column . '][select]"]' => [
              'value' => '__new',
            ],
          ],
        ],
        'value' => [
          '#type' => 'textfield',
          '#states' => [
            'visible' => [
              ':input[name="mappings[' . $delta . '][map][' . $column . '][select]"]' => [
                'value' => '__new',
              ],
            ],
          ],
        ],
        'machine_name' => [
          '#type' => 'machine_name',
          '#machine_name' => [
            'exists' => [
              $this,
              'customSourceExists',
            ],
            'source' => [
              'mappings',
              $delta,
              'map',
              $column,
              '__new',
              'value',
            ],
            'standalone' => TRUE,
            'label' => '',
          ],
          '#default_value' => '',
          '#required' => FALSE,
          '#disabled' => '',
        ],
      ],
    ];
    $label = Html::escape($target_definition
      ->getLabel() . ' (' . $mapping['target'] . ')');
    if (count($mapping['map']) > 1) {
      $desc = $target_definition
        ->getPropertyLabel($column);
    }
    else {
      $desc = $target_definition
        ->getDescription();
    }
    if ($desc) {
      $label .= ': ' . $desc;
    }
    $row['targets']['#items'][] = $label;
  }
  $default_button = [
    '#ajax' => [
      'callback' => '::ajaxCallback',
      'wrapper' => 'feeds-mapping-form-ajax-wrapper',
      'effect' => 'fade',
      'progress' => 'none',
    ],
    '#delta' => $delta,
  ];
  $row['settings']['#markup'] = '';
  $row['configure']['#markup'] = '';
  if ($plugin && $this
    ->pluginHasSettingsForm($plugin, $form_state)) {
    if ($delta == $ajax_delta) {
      $row['settings'] = $plugin
        ->buildConfigurationForm([], $form_state);
      $row['settings']['actions'] = [
        '#type' => 'actions',
        'save_settings' => $default_button + [
          '#type' => 'submit',
          '#button_type' => 'primary',
          '#value' => $this
            ->t('Update'),
          '#op' => 'update',
          '#name' => 'target-save-' . $delta,
        ],
        'cancel_settings' => $default_button + [
          '#type' => 'submit',
          '#value' => $this
            ->t('Cancel'),
          '#op' => 'cancel',
          '#name' => 'target-cancel-' . $delta,
          '#limit_validation_errors' => [
            [],
          ],
        ],
      ];
      $row['#attributes']['class'][] = 'feeds-mapping-settings-editing';
    }
    else {
      $row['settings'] = [
        '#parents' => [
          'config_summary',
          $delta,
        ],
      ] + $this
        ->buildSummary($plugin);
      $row['configure'] = $default_button + [
        '#type' => 'image_button',
        '#op' => 'configure',
        '#name' => 'target-settings-' . $delta,
        '#src' => 'core/misc/icons/787878/cog.svg',
      ];
    }
  }
  elseif ($plugin instanceof ConfigurableTargetInterface) {
    $summary = $this
      ->buildSummary($plugin);
    if (!empty($summary)) {
      $row['settings'] = [
        '#parents' => [
          'config_summary',
          $delta,
        ],
      ] + $this
        ->buildSummary($plugin);
    }
  }
  $mappings = $this->feedType
    ->getMappings();
  foreach ($mapping['map'] as $column => $source) {
    if ($target_definition
      ->isUnique($column)) {
      $row['unique'][$column] = [
        '#title' => $this
          ->t('Unique'),
        '#type' => 'checkbox',
        '#default_value' => !empty($mappings[$delta]['unique'][$column]),
        '#title_display' => 'invisible',
      ];
    }
    else {
      $row['unique']['#markup'] = '';
    }
  }
  if ($delta != $ajax_delta) {
    $row['remove'] = $default_button + [
      '#title' => $this
        ->t('Remove'),
      '#type' => 'checkbox',
      '#default_value' => FALSE,
      '#title_display' => 'invisible',
      '#parents' => [
        'remove_mappings',
        $delta,
      ],
      '#remove' => TRUE,
    ];
  }
  else {
    $row['remove']['#markup'] = '';
  }
  return $row;
}