You are here

public function WeightSelector::viewsForm in Weight 8.3

File

src/Plugin/views/field/WeightSelector.php, line 86

Class

WeightSelector
Field handler to present a weight selector element.

Namespace

Drupal\weight\Plugin\views\field

Code

public function viewsForm(array &$form, FormStateInterface $form_state) {

  // The view is empty, abort.
  if (empty($this->view->result)) {
    return;
  }
  $form[$this->options['id']] = [
    '#tree' => TRUE,
  ];
  $options = WeightSelectorWidget::rangeOptions($this->options['range']);

  // At this point the query already run, so we can access the results.
  foreach ($this->view->result as $row_index => $row) {
    $entity = $this
      ->getEntity($row);
    $field_langcode = $entity
      ->getEntityTypeId() . '__' . $this->field . '_langcode';
    $form[$this->options['id']][$row_index]['weight'] = [
      '#type' => 'select',
      '#options' => $options,
      '#default_value' => $this
        ->getValue($row),
      '#attributes' => [
        'class' => [
          'weight-selector',
        ],
      ],
    ];
    $form[$this->options['id']][$row_index]['entity'] = [
      '#type' => 'value',
      '#value' => $entity,
    ];
    $form[$this->options['id']][$row_index]['langcode'] = [
      '#type' => 'value',
      '#value' => isset($row->{$field_langcode}) ? $row->{$field_langcode} : NULL,
    ];
  }
  $form['views_field'] = [
    '#type' => 'value',
    '#value' => $this->field,
  ];
  $form['#action'] = $this->requestStack
    ->getCurrentRequest()
    ->getRequestUri();
}