You are here

protected function EntityFormField::buildEntities in Views Entity Form Field 8

Update entity objects based upon the submitted form values.

Parameters

array $form: A nested array form elements comprising the form.

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

bool $validate: Validate the entity after extracting form values.

2 calls to EntityFormField::buildEntities()
EntityFormField::viewsFormSubmit in src/Plugin/views/field/EntityFormField.php
EntityFormField::viewsFormValidate in src/Plugin/views/field/EntityFormField.php

File

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

Class

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

Namespace

Drupal\views_entity_form_field\Plugin\views\field

Code

protected function buildEntities(array &$form, FormStateInterface $form_state, $validate = FALSE) {
  $field_name = $this->definition['field_name'];

  // Set this value back to it's relevant entity from each row.
  foreach ($this
    ->getView()->result as $row_index => $row) {

    // Check to make sure that this entity has a relevant field.
    $entity = $this
      ->getEntity($row);
    if ($entity && $entity
      ->hasField($field_name) && $this
      ->getBundleFieldDefinition($entity
      ->bundle())
      ->isDisplayConfigurable('form')) {

      // Get current entity field values.
      $items = $entity
        ->get($field_name)
        ->filterEmptyItems();

      // Extract values.
      $this
        ->getPluginInstance($entity
        ->bundle())
        ->extractFormValues($items, $form[$this->options['id']][$row_index], $form_state);

      // Validate entity and add violations to field widget.
      if ($validate) {
        $violations = $items
          ->validate();
        if ($violations
          ->count() > 0) {
          $this
            ->getPluginInstance($entity
            ->bundle())
            ->flagErrors($items, $violations, $form[$this->options['id']][$row_index], $form_state);
        }
      }
    }
  }
}