You are here

public function EntityFormField::viewsForm in Views Entity Form Field 8

Form constructor for the views 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.

File

src/Plugin/views/field/EntityFormField.php, line 467

Class

EntityFormField
Defines a views form element for an entity field widget.

Namespace

Drupal\views_entity_form_field\Plugin\views\field

Code

public function viewsForm(array &$form, FormStateInterface $form_state) {
  $field_name = $this->definition['field_name'];

  // Initialize form values.
  $form['#cache']['max-age'] = 0;
  $form['#attached']['library'][] = 'views_entity_form_field/views_form';
  $form['#attributes']['class'][] = 'views-entity-form';
  $form['#process'][] = [
    $this,
    'viewsFormProcess',
  ];
  $form['#tree'] = TRUE;
  $form += [
    '#parents' => [],
  ];

  // Only add the buttons if there are results.
  if (!empty($this
    ->getView()->result)) {
    $form[$this->options['id']]['#tree'] = TRUE;
    $form[$this->options['id']]['#entity_form_field'] = TRUE;
    foreach ($this
      ->getView()->result as $row_index => $row) {

      // Initialize this row and column.
      $form[$this->options['id']][$row_index]['#parents'] = [
        $this->options['id'],
        $row_index,
      ];
      $form[$this->options['id']][$row_index]['#tree'] = TRUE;

      // Make sure there's an entity for this row (relationships can be null).
      if ($this
        ->getEntity($row)) {

        // Load field definition based on current entity bundle.
        $entity = $this
          ->getEntityTranslation($this
          ->getEntity($row), $row);
        if ($entity
          ->hasField($field_name) && $this
          ->getBundleFieldDefinition($entity
          ->bundle())
          ->isDisplayConfigurable('form')) {
          $items = $entity
            ->get($field_name)
            ->filterEmptyItems();

          // Add widget to form and add field overrides.
          $form[$this->options['id']][$row_index][$field_name] = $this
            ->getPluginInstance()
            ->form($items, $form[$this->options['id']][$row_index], $form_state);
          $form[$this->options['id']][$row_index][$field_name]['#access'] = $entity
            ->access('update') && $items
            ->access('edit');
          $form[$this->options['id']][$row_index][$field_name]['#cache']['contexts'] = $entity
            ->getCacheContexts();
          $form[$this->options['id']][$row_index][$field_name]['#cache']['tags'] = $entity
            ->getCacheTags();
          $form[$this->options['id']][$row_index][$field_name]['#parents'] = [
            $this->options['id'],
            $row_index,
            $field_name,
          ];

          // Hide field widget title.
          if ($this->options['plugin']['hide_title']) {
            $form[$this->options['id']][$row_index][$field_name]['#attributes']['class'][] = 'views-entity-form-field-field-label-hidden';
          }

          // Hide field widget description.
          if ($this->options['plugin']['hide_description']) {
            $form[$this->options['id']][$row_index][$field_name]['#attributes']['class'][] = 'views-entity-form-field-field-description-hidden';
          }
        }
      }
    }
  }
}