You are here

protected function TamperListForm::buildTampersTable in Feeds Tamper 8.2

Builds a table of tampers for the specified source -> targets.

Parameters

array $form: An associative array containing the structure of the form.

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

string $source: The source name.

array $targets: An array of the source targets.

Return value

array Form table element.

1 call to TamperListForm::buildTampersTable()
TamperListForm::buildForm in src/Form/TamperListForm.php
Form constructor.

File

src/Form/TamperListForm.php, line 147

Class

TamperListForm
Provides a form to manage tamper plugins for a feed type.

Namespace

Drupal\feeds_tamper\Form

Code

protected function buildTampersTable(array $form, FormStateInterface $form_state, $source, array $targets) {
  $header = [
    'label' => $this
      ->t('Label'),
    'description' => $this
      ->t('Description'),
    'weight' => $this
      ->t('Weight'),
    'plugin' => $this
      ->t('Plugin'),
    'operations' => $this
      ->t('Operations'),
  ];
  $url_parameters = [
    'feeds_feed_type' => $this->feedsFeedType
      ->id(),
  ];
  $view_tampers_url = Url::fromRoute('entity.feeds_feed_type.tamper', $url_parameters)
    ->toString();
  $destination_query = [
    'destination' => $view_tampers_url,
  ];
  $target_labels = [];
  $item = [
    '#type' => 'table',
    '#header' => $header,
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'tamper-weight',
      ],
    ],
  ];
  foreach ($targets as $target_key => $columns) {

    /* @var \Drupal\feeds\FieldTargetDefinition $target */
    $target = $this->targets[$target_key];
    $source_label = Html::escape($this->sources[$source]['label']);
    $label = Html::escape($target
      ->getLabel()) . ': ';
    foreach ($columns as $column => $mapping) {
      if (count($mapping['map']) > 1) {
        $target_labels[] = $label . $target
          ->getPropertyLabel($column);
      }
      else {
        $target_labels[] = $label . ($target
          ->getDescription() ?: $column);
      }
    }
  }
  $item['#caption'] = $source_label . ' -> ' . implode(', ', $target_labels);
  $add_plugin_weight = 0;
  if (!empty($this->tampers[$source])) {

    // Calculate the range (delta) needed for the weight field. By default,
    // the range is from -10 to 10, which means 21 slots in total. If there
    // are however more than 21 tamper instances, the range should increase in
    // order to be able to assign an unique weight to each tamper instance.
    $tampers_count = round(count($this->tampers[$source]) / 2);
    $tamper_weight_delta = $tampers_count < 10 ? 10 : $tampers_count;

    /** @var \Drupal\tamper\TamperInterface $tamper */
    foreach ($this->tampers[$source] as $id => $tamper) {
      $row = [
        '#attributes' => [
          'class' => [
            'draggable',
          ],
        ],
        '#weight' => $tamper
          ->getSetting('weight'),
      ];

      // Label.
      $row['label'] = [
        '#plain_text' => $tamper
          ->getSetting('label') ? $tamper
          ->getSetting('label') : '',
      ];

      // Plugin instance description.
      $row['description'] = [
        '#plain_text' => $tamper
          ->getPluginDefinition()['description'],
      ];

      // Weight field.
      $row['weight'] = [
        '#title' => $this
          ->t('Weight'),
        '#title_display' => 'invisible',
        '#type' => 'weight',
        '#default_value' => $tamper
          ->getSetting('weight'),
        '#attributes' => [
          'class' => [
            'tamper-weight',
          ],
        ],
        '#delta' => $tamper_weight_delta,
      ];
      $row['plugin'] = [
        '#plain_text' => $tamper
          ->getPluginDefinition()['label'],
      ];
      $operations_params = $url_parameters + [
        'tamper_uuid' => $id,
      ];
      $row['operations'] = [
        '#type' => 'operations',
        '#links' => [
          'edit' => [
            'title' => $this
              ->t('Edit'),
            'url' => Url::fromRoute('entity.feeds_feed_type.tamper_edit', $operations_params, [
              'query' => $destination_query,
            ]),
          ],
          'delete' => [
            'title' => $this
              ->t('Delete'),
            'url' => Url::fromRoute('entity.feeds_feed_type.tamper_delete', $operations_params),
          ],
        ],
      ];

      // @todo Implement enabled.
      // $row['enabled'] = '';
      $item[$id] = $row;
      $add_plugin_weight = $tamper
        ->getSetting('weight') + 1;
    }
  }
  $add_tamper_url = Url::fromRoute('entity.feeds_feed_type.tamper_add', $url_parameters + [
    'source_field' => $source,
  ], [
    'query' => array_merge($destination_query, [
      'weight' => $add_plugin_weight,
    ]),
  ]);
  $item['add']['link'] = [
    '#type' => 'link',
    '#title' => $this
      ->t('Add plugin'),
    '#url' => $add_tamper_url,
    '#wrapper_attributes' => [
      'colspan' => count($header),
    ],
  ];
  return $item;
}