You are here

public function ViewsReferenceTrait::itemCurrentValues in Views Reference Field 8.2

Build a field element for a viewsreference field.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: Array of default values for this field.

int $delta: The order of this item in the array of sub-elements (0, 1, 2, etc.).

array $element: The field item element.

array $form: The overall form array.

\Drupal\Core\Form\FormStateInterface $form_state: Array of default values for this field.

Return value

array The changed field element.

1 call to ViewsReferenceTrait::itemCurrentValues()
ViewsReferenceTrait::fieldElement in src/Plugin/Field/FieldWidget/ViewsReferenceTrait.php
Build a field element for a viewsreference field.

File

src/Plugin/Field/FieldWidget/ViewsReferenceTrait.php, line 213

Class

ViewsReferenceTrait
Trait for shared code in Viewsreference Field Widgets.

Namespace

Drupal\viewsreference\Plugin\Field\FieldWidget

Code

public function itemCurrentValues(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
  $values = [];
  $field_name = $this->fieldDefinition
    ->getName();
  $value_parents = [];
  if (isset($element['#field_parents'])) {
    $value_parents = $element['#field_parents'];
  }
  elseif (isset($element['target_id']['#field_parents'])) {
    $value_parents = $element['target_id']['#field_parents'];
  }
  $value_parents = array_merge($value_parents, [
    $field_name,
    $delta,
  ]);

  // Get the current value.
  $form_input_values = $form_state
    ->getUserInput();
  $input_value_exists = NULL;
  if ($form_input_values) {

    // User input.
    $input_values = NestedArray::getValue($form_input_values, $value_parents, $input_value_exists);
    if ($input_value_exists) {
      $values = $input_values;
    }
  }
  if (!$input_value_exists) {
    if ($item_state_values = $form_state
      ->getValue($value_parents)) {

      // Stored form state.
      $values = $item_state_values;
    }
    elseif ($item_values = $items[$delta]
      ->getValue()) {

      // Saved items values.
      $values = $item_values;
    }
  }
  return $values;
}