Repository URL to install this package:
|
Version:
3.1.1 ▾
|
<?php
namespace Drupal\dds_content\Element;
use Drupal\Core\Entity\Element\EntityAutocomplete;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides an entity autocomplete form element.
*
* The #default_value accepted by this element is either an entity object or an
* array of entity objects.
*
* @FormElement("dds_content_entity_autocomplete")
*/
class DDSContentEntityAutocomplete extends EntityAutocomplete {
/**
* {@inheritdoc}
*/
public function getInfo() {
$info = parent::getInfo();
// Apply default form element properties.
$info['#target_type'] = 'multiple';
$info['#target_types'] = [];
$info['#autocreate'] = FALSE;
return $info;
}
/**
* Adds entity autocomplete functionality to a form element.
*
* @param array $element
* The form element to process. Properties used:
* - #target_type: The ID of the target entity type.
* - #selection_handler: The plugin ID of the entity reference selection
* handler.
* - #selection_settings: An array of settings that will be passed to the
* selection handler.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $complete_form
* The complete form structure.
*
* @return array
* The form element.
*
* @throws \InvalidArgumentException
* Exception thrown when the #target_type or #autocreate['bundle'] are
* missing.
*/
public static function processEntityAutocomplete(array &$element, FormStateInterface $form_state, array &$complete_form) {
$element = parent::processEntityAutocomplete($element, $form_state, $complete_form);
$element['#autocomplete_route_name'] = 'dds_content.entity_autocomplete';
$element['#autocomplete_route_parameters']['target_type'] = 'multiple';
return $element;
}
}