View source
<?php
namespace Drupal\entityreference\Plugin\field\widget;
use Drupal\Core\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Widget\WidgetBase;
abstract class AutocompleteWidgetBase extends WidgetBase {
public function settingsForm(array $form, array &$form_state) {
$form['match_operator'] = array(
'#type' => 'select',
'#title' => t('Autocomplete matching'),
'#default_value' => $this
->getSetting('match_operator'),
'#options' => array(
'STARTS_WITH' => t('Starts with'),
'CONTAINS' => t('Contains'),
),
'#description' => t('Select the method used to collect autocomplete suggestions. Note that <em>Contains</em> can cause performance issues on sites with thousands of nodes.'),
);
$form['size'] = array(
'#type' => 'textfield',
'#title' => t('Size of textfield'),
'#default_value' => $this
->getSetting('size'),
'#element_validate' => array(
'form_validate_number',
),
'#min' => 1,
'#required' => TRUE,
);
return $form;
}
public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
$element = $this
->prepareElement($items, $delta, $element, $langcode, $form, $form_state, 'entityreference/autocomplete/single');
return array(
'target_id' => $element,
);
}
protected function prepareElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state, $default_path) {
$instance = $this->instance;
$field = $this->field;
$entity = isset($element['#entity']) ? $element['#entity'] : NULL;
$path = $this
->getSetting('path');
$autocomplete_path = !empty($path) ? $path : $default_path;
$autocomplete_path .= '/' . $field['field_name'] . '/' . $instance['entity_type'] . '/' . $instance['bundle'] . '/';
$id = 'NULL';
if ($entity) {
if ($eid = $entity
->id()) {
$id = $eid;
}
}
$autocomplete_path .= $id;
$element += array(
'#type' => 'textfield',
'#maxlength' => 1024,
'#default_value' => implode(', ', $this
->getLabels($items)),
'#autocomplete_path' => $autocomplete_path,
'#size' => $this
->getSetting('size'),
'#element_validate' => array(
array(
$this,
'elementValidate',
),
),
);
return $element;
}
public function errorElement(array $element, array $error, array $form, array &$form_state) {
return $element['target_id'];
}
public function elementValidate($element, &$form_state) {
}
protected function getLabels(array $items) {
$instance = $this->instance;
$field = $this->field;
$entity = isset($element['#entity']) ? $element['#entity'] : NULL;
$handler = entityreference_get_selection_handler($field, $instance, $entity);
$entity_ids = array();
$entity_labels = array();
foreach ($items as $item) {
$entity_ids[] = $item['target_id'];
}
$entities = entity_load_multiple($field['settings']['target_type'], $entity_ids);
foreach ($entities as $entity_id => $entity_item) {
$label = $entity_item
->label();
$key = "{$label} ({$entity_id})";
if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
$key = '"' . str_replace('"', '""', $key) . '"';
}
$entity_labels[] = $key;
}
return $entity_labels;
}
}