You are here

public function SelectForm::viewsForm in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/views/field/SelectForm.php \Drupal\entity_browser\Plugin\views\field\SelectForm::viewsForm()

Form constructor for the bulk form.

Parameters

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

File

src/Plugin/views/field/SelectForm.php, line 137

Class

SelectForm
Defines a bulk operation form element that works with entity browser.

Namespace

Drupal\entity_browser\Plugin\views\field

Code

public function viewsForm(&$render) {

  // Only add the bulk form options and buttons if there are results.
  if (!empty($this->view->result)) {

    // Render checkboxes for all rows.
    $render[$this->options['id']]['#tree'] = TRUE;
    $render[$this->options['id']]['#printed'] = TRUE;
    $cardinality = $this
      ->getCardinality();
    $use_field_cardinality = $this->options['use_field_cardinality'];
    $use_radios = $use_field_cardinality && $cardinality === 1;
    foreach ($this->view->result as $row) {
      $value = $this
        ->getRowId($row);
      $render[$this->options['id']][$value] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Select this item'),
        '#title_display' => 'invisible',
        '#return_value' => $value,
        '#attributes' => [
          'name' => "entity_browser_select[{$value}]",
        ],
        '#default_value' => NULL,
      ];
      if ($use_radios) {
        $render[$this->options['id']][$value]['#type'] = 'radio';
        $render[$this->options['id']][$value]['#attributes'] = [
          'name' => "entity_browser_select",
        ];
        $render[$this->options['id']][$value]['#parents'] = [
          'entity_browser_select',
        ];

        // Add the #value property to suppress a php notice in Radio.php.
        $render[$this->options['id']][$value]['#value'] = FALSE;
      }
    }
    $render['entity_browser_select_form_metadata'] = [
      'cardinality' => [
        '#type' => 'hidden',
        '#value' => $cardinality,
      ],
      'use_field_cardinality' => [
        '#type' => 'hidden',
        '#value' => (int) $use_field_cardinality,
      ],
      '#tree' => TRUE,
    ];
  }
  $render['view']['#cache']['tags'][] = 'config:entity_browser.settings';
}