You are here

public function DraggableViewsField::viewsForm in DraggableViews 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/views/field/DraggableViewsField.php \Drupal\draggableviews\Plugin\views\field\DraggableViewsField::viewsForm()

Form constructor for the bulk form.

Parameters

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

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

Overrides BulkForm::viewsForm

File

src/Plugin/views/field/DraggableViewsField.php, line 107

Class

DraggableViewsField
Defines a draggableviews form element.

Namespace

Drupal\draggableviews\Plugin\views\field

Code

public function viewsForm(&$form, FormStateInterface $form_state) {
  $form[$this->options['id']] = [
    '#tree' => TRUE,
  ];
  $draggableviews = new DraggableViews($this->view);
  foreach ($this->view->result as $row_index => $row) {
    $form[$this->options['id']][$row_index] = [
      '#tree' => TRUE,
    ];

    // Add weight.
    $form[$this->options['id']][$row_index]['weight'] = [
      '#type' => 'textfield',
      '#size' => '5',
      '#maxlength' => '5',
      '#value' => $row->draggableviews_structure_weight,
      '#attributes' => [
        'class' => [
          'draggableviews-weight',
        ],
      ],
    ];

    // Item to keep id of the entity.
    $form[$this->options['id']][$row_index]['id'] = [
      '#type' => 'hidden',
      '#value' => $this
        ->getEntity($row)
        ->id(),
      '#attributes' => [
        'class' => [
          'draggableviews-id',
        ],
      ],
    ];

    // Add parent.
    $form[$this->options['id']][$row_index]['parent'] = [
      '#type' => 'hidden',
      '#default_value' => $draggableviews
        ->getParent($row_index),
      '#attributes' => [
        'class' => [
          'draggableviews-parent',
        ],
      ],
    ];
  }
  if ($this->currentUser
    ->hasPermission('access draggableviews')) {
    $options = [
      'table_id' => $draggableviews
        ->getHtmlId(),
      'action' => 'match',
      'relationship' => 'group',
      'group' => 'draggableviews-parent',
      'subgroup' => 'draggableviews-parent',
      'source' => 'draggableviews-id',
    ];
    drupal_attach_tabledrag($form, $options);
  }
}