You are here

ViewsReferenceWidget.php in Views Reference Field 8

Same filename and directory in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/ViewsReferenceWidget.php

File

src/Plugin/Field/FieldWidget/ViewsReferenceWidget.php
View source
<?php

namespace Drupal\viewsreference\Plugin\Field\FieldWidget;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget;

/**
 * Plugin implementation of the 'entity_reference_autocomplete' widget.
 *
 * @FieldWidget(
 *   id = "viewsreference_autocomplete",
 *   label = @Translation("Views Reference Autocomplete"),
 *   description = @Translation("An autocomplete views reference field."),
 *   field_types = {
 *     "viewsreference"
 *   }
 * )
 */
class ViewsReferenceWidget extends EntityReferenceAutocompleteWidget {
  use ViewsReferenceTrait;

  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items, $delta, $element, $form, $form_state);
    $test = [
      'filled' => TRUE,
    ];
    $element = $this
      ->fieldElement($element, $items, $delta, $test);
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function errorElement(array $element, ConstraintViolationInterface $error, array $form, FormStateInterface $form_state) {
    return isset($element['display_id']) ? $element['display_id'] : FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
    $values = parent::massageFormValues($values, $form, $form_state);

    // Options creates an array which we need to flatten.
    $values = $this
      ->massageValues($values, $form, $form_state);
    return $values;
  }

}

Classes

Namesort descending Description
ViewsReferenceWidget Plugin implementation of the 'entity_reference_autocomplete' widget.