You are here

public function ReorderFieldsForm::buildForm in Feed Import 8

Form constructor.

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

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/ReorderFieldsForm.php, line 32
Contains \Drupal\feed_import\Form\ReorderFieldsForm

Class

ReorderFieldsForm

Namespace

Drupal\feed_import\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $fid = NULL) {
  $this->feed = FeedImport::loadFeed($fid);
  $form['table'] = array(
    '#type' => 'table',
    '#header' => array(
      t('Field'),
      t('Weight'),
    ),
    '#empty' => t('No fields have been added.'),
    '#tabledrag' => array(
      array(
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'reorder-table-weight',
      ),
    ),
  );
  $fields = array_keys($this->feed->settings['fields']);
  foreach ($fields as $delta => $field) {
    $form['table'][$field] = array(
      '#attributes' => array(
        'class' => array(
          'draggable',
        ),
      ),
      '#weight' => $delta,
      'label' => array(
        '#plain_text' => $field,
      ),
      'weight' => array(
        '#type' => 'weight',
        '#title' => t('Weight for @title', array(
          '@title' => $field,
        )),
        '#title_display' => 'invisible',
        '#default_value' => $delta,
        '#attributes' => array(
          'class' => array(
            'reorder-table-weight',
          ),
        ),
      ),
    );
  }
  if ($fields) {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save order'),
    );
  }
  return $form;
}