diff --git a/core/modules/entityreference/entityreference.module b/core/modules/entityreference/entityreference.module
index dc58994..d7b88ab 100644
--- a/core/modules/entityreference/entityreference.module
+++ b/core/modules/entityreference/entityreference.module
@@ -71,14 +71,25 @@ function entityreference_field_is_empty($item, $field) {
* Get the selection handler for a given entityreference field.
*/
function entityreference_get_selection_handler($field, $instance = NULL, EntityInterface $entity = NULL) {
+ $target_entity_type = $field['settings']['target_type'];
+
+ // Check if the entity type does exist and has a base table.
+ $entity_info = entity_get_info($target_entity_type);
+ if (empty($entity_info['base table'])) {
+ return new SelectionBroken($field, $instance, $entity);
+ }
+
$plugin = drupal_container()->get('plugin.manager.entityreference.selection')->getDefinition($field['settings']['handler']);
$class = $plugin['class'];
+ // Remove 'Drupal' from the entity class.
+ $class_name = '\Drupal\entityreference\Plugin\Type\Selection' . substr($entity_info['entity class'], 6);
+
try {
- return $class::getInstance($field, $instance, $entity);
+ return new $class_name($field, $instance, $entity);
}
catch (Exception $e) {
- return SelectionBroken::getInstance($field, $instance, $entity);
+ return new SelectionBroken($field, $instance, $entity);
}
}
@@ -272,7 +283,7 @@ function entityreference_settings_ajax_submit($form, &$form_state) {
* http://drupal.org/node/1751234
* http://drupal.org/node/1705702
*/
-function entityreference_field_widget_info_alter(&$info) {
+function entityreference_field_widget_info_alter(array &$info) {
if (module_exists('options')) {
$info['options_select']['field types'][] = 'entityreference';
$info['options_buttons']['field types'][] = 'entityreference';
diff --git a/core/modules/entityreference/lib/Drupal/entityreference/Plugin/Type/Selection/SelectionBroken.php b/core/modules/entityreference/lib/Drupal/entityreference/Plugin/Type/Selection/SelectionBroken.php
index 6ba6538..a3f9e47 100644
--- a/core/modules/entityreference/lib/Drupal/entityreference/Plugin/Type/Selection/SelectionBroken.php
+++ b/core/modules/entityreference/lib/Drupal/entityreference/Plugin/Type/Selection/SelectionBroken.php
@@ -17,13 +17,6 @@
*/
class SelectionBroken implements SelectionInterface {
- /**
- * Implements SelectionInterface::getInstance().
- */
- public static function getInstance($field, $instance = NULL, EntityInterface $entity = NULL) {
- return new SelectionBroken($field, $instance, $entity);
- }
-
protected function __construct($field, $instance) {
$this->field = $field;
$this->instance = $instance;
diff --git a/core/modules/entityreference/lib/Drupal/entityreference/Plugin/Type/Selection/SelectionInterface.php b/core/modules/entityreference/lib/Drupal/entityreference/Plugin/Type/Selection/SelectionInterface.php
index 5578870..a91919c 100644
--- a/core/modules/entityreference/lib/Drupal/entityreference/Plugin/Type/Selection/SelectionInterface.php
+++ b/core/modules/entityreference/lib/Drupal/entityreference/Plugin/Type/Selection/SelectionInterface.php
@@ -21,20 +21,6 @@
interface SelectionInterface {
/**
- * Factory function: create a new instance of this handler for a given field.
- *
- * @param array $field
- * A field data structure.
- * @param array $instance
- * A field instance data structure.
- * @param EntityInterface $entity
- * A Drupal entity.
- *
- * @return EntityReferenceHandler
- */
- public static function getInstance($field, $instance = NULL, EntityInterface $entity = NULL);
-
- /**
* Returns a list of referencable entities.
*
* @return array
diff --git a/core/modules/entityreference/lib/Drupal/entityreference/Plugin/entityreference/selection/SelectionBase.php b/core/modules/entityreference/lib/Drupal/entityreference/Plugin/entityreference/selection/SelectionBase.php
index af57189..cb5f7fb 100644
--- a/core/modules/entityreference/lib/Drupal/entityreference/Plugin/entityreference/selection/SelectionBase.php
+++ b/core/modules/entityreference/lib/Drupal/entityreference/Plugin/entityreference/selection/SelectionBase.php
@@ -28,29 +28,6 @@
*/
class SelectionBase extends PluginSettingsBase implements SelectionInterface {
- /**
- * Implements SelectionInterface::getInstance().
- */
- public static function getInstance($field, $instance = NULL, EntityInterface $entity = NULL) {
- $target_entity_type = $field['settings']['target_type'];
-
- // Check if the entity type does exist and has a base table.
- $entity_info = entity_get_info($target_entity_type);
- if (empty($entity_info['base table'])) {
- return SelectionBroken::getInstance($field, $instance);
- }
-
- // Remove 'Drupal' from the entity class.
- $class_name = '\Drupal\entityreference\Plugin\Type\Selection' . substr($entity_info['entity class'], 6);
-
- if (class_exists($class_name)) {
- return new $class_name($field, $instance, $entity);
- }
- else {
- return new SelectionBase($field, $instance, $entity);
- }
- }
-
public function __construct($field, $instance = NULL, EntityInterface $entity = NULL) {
$this->field = $field;
$this->instance = $instance;
diff --git a/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/formatter/DefaultEntityReferenceFormatter.php b/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/formatter/DefaultEntityReferenceFormatter.php
deleted file mode 100644
index 4156a03..0000000
--- a/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/formatter/DefaultEntityReferenceFormatter.php
+++ /dev/null
@@ -1,90 +0,0 @@
- $entity) {
- foreach ($items[$id] as $delta => $item) {
- if (isset($item['target_id'])) {
- $target_ids[] = $item['target_id'];
- }
- }
- }
-
- $target_type = $this->field['settings']['target_type'];
-
- if ($target_ids) {
- $target_entities = entity_load_multiple($target_type, $target_ids);
- }
- else {
- $target_entities = array();
- }
-
- // Iterate through the fieldable entities again to attach the loaded
- // data.
- foreach ($entities as $id => $entity) {
-
- foreach ($items[$id] as $delta => $item) {
- $items[$id][$delta]['entity'] = $target_entities[$item['target_id']];
-
- if (!isset($target_entities[$item['target_id']])) {
- continue;
- }
-
- $entity = $target_entities[$item['target_id']];
-
- // TODO: Improve when we have entity_access().
- $entity_access = $target_type == 'node' ? node_access('view', $entity) : TRUE;
- if (!$entity_access) {
- continue;
- }
-
- // Mark item as accessible.
- $items[$id][$delta]['access'] = TRUE;
- }
- }
- }
-
- /**
- * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements().
- *
- * Remove unaccessible values.
- *
- * @see Drupal\entityreference\Plugin\field\formatter\DefaultEntityReferenceFormatter::prepareView().
- */
- public function viewElements(EntityInterface $entity, $langcode, array $items) {
- // Remove unaccessible items.
- foreach ($items as $delta => $item) {
- if (empty($item['access'])) {
- unset($items[$delta]);
- }
- }
- return array();
- }
-}
diff --git a/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/formatter/EntityReferenceEntityFormatter.php b/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/formatter/EntityReferenceEntityFormatter.php
index 3f072f6..0863172 100644
--- a/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/formatter/EntityReferenceEntityFormatter.php
+++ b/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/formatter/EntityReferenceEntityFormatter.php
@@ -12,7 +12,7 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\entityreference\EntityReferenceRecursiveRenderingException;
-use Drupal\entityreference\Plugin\field\formatter\DefaultEntityReferenceFormatter;
+use Drupal\entityreference\Plugin\field\formatter\EntityReferenceFormatterBase;
/**
* Plugin implementation of the 'entity-reference rendered entity' formatter.
@@ -31,7 +31,7 @@
* }
* )
*/
-class EntityReferenceEntityFormatter extends DefaultEntityReferenceFormatter {
+class EntityReferenceEntityFormatter extends EntityReferenceFormatterBase {
/**
* Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm().
@@ -67,11 +67,11 @@ public function settingsForm(array $form, array &$form_state) {
*/
public function settingsSummary() {
$summary = array();
- $settings = $this->settings;
$entity_info = entity_get_info($this->field['settings']['target_type']);
- $summary[] = t('Rendered as @mode', array('@mode' => isset($entity_info['view modes'][$settings['view_mode']]['label']) ? $entity_info['view modes'][$settings['view_mode']]['label'] : $settings['view_mode']));
- $summary[] = !empty($settings['links']) ? t('Display links') : t('Do not display links');
+ $view_mode = $this->getSetting('view_mode');
+ $summary[] = t('Rendered as @mode', array('@mode' => isset($entity_info['view modes'][$view_mode]['label']) ? $entity_info['view modes'][$view_mode]['label'] : $view_mode));
+ $summary[] = $this->getSetting('links') ? t('Display links') : t('Do not display links');
return implode('
', $summary);
}
@@ -85,7 +85,8 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) {
$instance = $this->instance;
$field = $this->field;
- $settings = $this->settings;
+ $view_mode = $this->getSetting('view_mode');
+ $links = $this->getSetting('links');
$target_type = $field['settings']['target_type'];
@@ -101,9 +102,9 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) {
$entity = clone $item['entity'];
unset($entity->content);
- $elements[$delta] = entity_view($entity, $settings['view_mode'], $langcode);
+ $elements[$delta] = entity_view($entity, $view_mode, $langcode);
- if (empty($settings['links']) && isset($result[$delta][$target_type][$item['target_id']]['links'])) {
+ if (empty($links) && isset($result[$delta][$target_type][$item['target_id']]['links'])) {
// Hide the element links.
$elements[$delta][$target_type][$item['target_id']]['links']['#access'] = FALSE;
}
diff --git a/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/formatter/EntityReferenceFormatterBase.php b/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/formatter/EntityReferenceFormatterBase.php
new file mode 100644
index 0000000..6ecc0b4
--- /dev/null
+++ b/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/formatter/EntityReferenceFormatterBase.php
@@ -0,0 +1,90 @@
+ $entity) {
+ foreach ($items[$id] as $delta => $item) {
+ if (isset($item['target_id'])) {
+ $target_ids[] = $item['target_id'];
+ }
+ }
+ }
+
+ $target_type = $this->field['settings']['target_type'];
+
+ if ($target_ids) {
+ $target_entities = entity_load_multiple($target_type, $target_ids);
+ }
+ else {
+ $target_entities = array();
+ }
+
+ // Iterate through the fieldable entities again to attach the loaded
+ // data.
+ foreach ($entities as $id => $entity) {
+
+ foreach ($items[$id] as $delta => $item) {
+ $items[$id][$delta]['entity'] = $target_entities[$item['target_id']];
+
+ if (!isset($target_entities[$item['target_id']])) {
+ continue;
+ }
+
+ $entity = $target_entities[$item['target_id']];
+
+ // TODO: Improve when we have entity_access().
+ $entity_access = $target_type == 'node' ? node_access('view', $entity) : TRUE;
+ if (!$entity_access) {
+ continue;
+ }
+
+ // Mark item as accessible.
+ $items[$id][$delta]['access'] = TRUE;
+ }
+ }
+ }
+
+ /**
+ * Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements().
+ *
+ * Remove unaccessible values.
+ *
+ * @see Drupal\entityreference\Plugin\field\formatter\EntityReferenceFormatterBase::prepareView().
+ */
+ public function viewElements(EntityInterface $entity, $langcode, array $items) {
+ // Remove unaccessible items.
+ foreach ($items as $delta => $item) {
+ if (empty($item['access'])) {
+ unset($items[$delta]);
+ }
+ }
+ return array();
+ }
+}
diff --git a/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/formatter/EntityReferenceIdFormatter.php b/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/formatter/EntityReferenceIdFormatter.php
index 95aaddb..89f9f98 100644
--- a/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/formatter/EntityReferenceIdFormatter.php
+++ b/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/formatter/EntityReferenceIdFormatter.php
@@ -11,7 +11,7 @@
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\EntityInterface;
-use Drupal\entityreference\Plugin\field\formatter\DefaultEntityReferenceFormatter;
+use Drupal\entityreference\Plugin\field\formatter\EntityReferenceFormatterBase;
/**
* Plugin implementation of the 'entity-reference ID' formatter.
@@ -26,7 +26,7 @@
* }
* )
*/
-class EntityReferenceIdFormatter extends DefaultEntityReferenceFormatter {
+class EntityReferenceIdFormatter extends EntityReferenceFormatterBase {
/**
* Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements().
diff --git a/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/formatter/EntityReferenceLabelFormatter.php b/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/formatter/EntityReferenceLabelFormatter.php
index f97a449..43d7132 100644
--- a/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/formatter/EntityReferenceLabelFormatter.php
+++ b/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/formatter/EntityReferenceLabelFormatter.php
@@ -11,7 +11,7 @@
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\EntityInterface;
-use Drupal\entityreference\Plugin\field\formatter\DefaultEntityReferenceFormatter;
+use Drupal\entityreference\Plugin\field\formatter\EntityReferenceFormatterBase;
/**
* Plugin implementation of the 'entity-reference label' formatter.
@@ -29,7 +29,7 @@
* }
* )
*/
-class EntityReferenceLabelFormatter extends DefaultEntityReferenceFormatter {
+class EntityReferenceLabelFormatter extends EntityReferenceFormatterBase {
/**
* Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::settingsForm().
@@ -63,16 +63,15 @@ public function viewElements(EntityInterface $entity, $langcode, array $items) {
$instance = $this->instance;
$field = $this->field;
- $settings = $this->settings;
$elements = array();
foreach ($items as $delta => $item) {
$entity = $item['entity'];
$label = $entity->label();
- // If the link is to be displayed and the entity has a uri, display a link.
- // Note the assignment ($url = ) here is intended to be an assignment.
- if ($settings['link'] && ($uri = $entity->uri())) {
+ // If the link is to be displayed and the entity has a uri,
+ // display a link.
+ if ($this->getSetting('link') && $uri = $entity->uri()) {
$elements[$delta] = array('#markup' => l($label, $uri['path'], $uri['options']));
}
else {
diff --git a/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/widget/AutocompleteTagsWidget.php b/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/widget/AutocompleteTagsWidget.php
index 2042ca7..bb38650 100644
--- a/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/widget/AutocompleteTagsWidget.php
+++ b/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/widget/AutocompleteTagsWidget.php
@@ -11,7 +11,7 @@
use Drupal\Core\Annotation\Translation;
use Drupal\field\Plugin\Type\Widget\WidgetBase;
-use Drupal\entityreference\Plugin\field\widget\DefaultAutocompleteWidget;
+use Drupal\entityreference\Plugin\field\widget\AutocompleteWidgetBase;
/**
* Plugin implementation of the 'entityreference autocomplete-tags' widget.
@@ -37,7 +37,7 @@
* multiple_values = FIELD_BEHAVIOR_CUSTOM
* )
*/
-class AutocompleteTagsWidget extends DefaultAutocompleteWidget {
+class AutocompleteTagsWidget extends AutocompleteWidgetBase {
/**
* Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().
diff --git a/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/widget/AutocompleteWidget.php b/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/widget/AutocompleteWidget.php
index 4dc099f..ce4e62f 100644
--- a/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/widget/AutocompleteWidget.php
+++ b/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/widget/AutocompleteWidget.php
@@ -9,8 +9,9 @@
use Drupal\Core\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
+use Drupal\field\Plugin\Type\Widget\WidgetBase;
-use Drupal\entityreference\Plugin\field\widget\DefaultAutocompleteWidget;
+use Drupal\entityreference\Plugin\field\widget\AutocompleteWidgetBase;
/**
* Plugin implementation of the 'entityreference autocomplete' widget.
@@ -35,7 +36,7 @@
* }
* )
*/
-class AutocompleteWidget extends DefaultAutocompleteWidget {
+class AutocompleteWidget extends AutocompleteWidgetBase {
/**
* Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().
diff --git a/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/widget/AutocompleteWidgetBase.php b/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/widget/AutocompleteWidgetBase.php
new file mode 100644
index 0000000..810b1ab
--- /dev/null
+++ b/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/widget/AutocompleteWidgetBase.php
@@ -0,0 +1,137 @@
+ '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 Contains 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'),
+ // Minimum value for form_validate_number().
+ '#min' => 1,
+ '#required' => TRUE,
+ );
+
+ return $form;
+ }
+
+ /**
+ * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().
+ */
+ 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);
+ }
+
+ /**
+ * Prepapre the element.
+ *
+ * @default_path
+ * The menu item to be used in the autocomplete path.
+ */
+ 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;
+
+ // Prepare the autocomplete path.
+ $path = $this->getSetting('path');
+ $autocomplete_path = !empty($path) ? $path : $default_path;
+ $autocomplete_path .= '/' . $field['field_name'] . '/' . $instance['entity_type'] . '/' . $instance['bundle'] . '/';
+
+ // Use as a placeholder in the URL when we don't have an entity.
+ // Most webservers collapse two consecutive slashes.
+ $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;
+ }
+
+ /**
+ * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::errorElement().
+ */
+ public function errorElement(array $element, array $error, array $form, array &$form_state) {
+ return $element['target_id'];
+ }
+
+ /**
+ * Element validate.
+ */
+ public function elementValidate($element, &$form_state) {
+ }
+
+ /**
+ * Get the entity labels.
+ */
+ 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();
+
+ // Build an array of entities ID.
+ foreach ($items as $item) {
+ $entity_ids[] = $item['target_id'];
+ }
+
+ // Load those entities and loop through them to extract their labels.
+ $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)";
+ // Labels containing commas or quotes must be wrapped in quotes.
+ if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
+ $key = '"' . str_replace('"', '""', $key) . '"';
+ }
+ $entity_labels[] = $key;
+ }
+ return $entity_labels;
+ }
+}
diff --git a/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/widget/DefaultAutocompleteWidget.php b/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/widget/DefaultAutocompleteWidget.php
deleted file mode 100644
index ff1ed6d..0000000
--- a/core/modules/entityreference/lib/Drupal/entityreference/Plugin/field/widget/DefaultAutocompleteWidget.php
+++ /dev/null
@@ -1,140 +0,0 @@
-settings;
-
- $form['match_operator'] = array(
- '#type' => 'select',
- '#title' => t('Autocomplete matching'),
- '#default_value' => $settings['match_operator'],
- '#options' => array(
- 'STARTS_WITH' => t('Starts with'),
- 'CONTAINS' => t('Contains'),
- ),
- '#description' => t('Select the method used to collect autocomplete suggestions. Note that Contains can cause performance issues on sites with thousands of nodes.'),
- );
- $form['size'] = array(
- '#type' => 'textfield',
- '#title' => t('Size of textfield'),
- '#default_value' => $settings['size'],
- '#element_validate' => array('form_validate_number'),
- // Minimum value for form_validate_number().
- '#min' => 1,
- '#required' => TRUE,
- );
-
- return $form;
- }
-
- /**
- * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().
- */
- public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
- // We let the Field API handles multiple values for us, only take
- // care of the one matching our delta.
- if (isset($items[$delta])) {
- $items = array($items[$delta]);
- }
- else {
- $items = array();
- }
-
- $element = $this->prepareElement($items, $delta, $element, $langcode, $form, $form_state, 'entityreference/autocomplete/single');
- return array('target_id' => $element);
- }
-
- /**
- * Prepapre the element.
- *
- * @default_path
- * The menu item to be used in the autocomplete path.
- */
- protected function prepareElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state, $default_path) {
- $instance = $this->instance;
- $field = $this->field;
- $settings = $this->settings;
- $entity = isset($element['#entity']) ? $element['#entity'] : NULL;
-
- // Prepare the autocomplete path.
- $autocomplete_path = !empty($settings['path']) ? $settings['path'] : $default_path;
- $autocomplete_path .= '/' . $field['field_name'] . '/' . $instance['entity_type'] . '/' . $instance['bundle'] . '/';
-
- // Use as a placeholder in the URL when we don't have an entity.
- // Most webservers collapse two consecutive slashes.
- $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' => $settings['size'],
- '#element_validate' => array(array($this, 'elementValidate')),
- );
- return $element;
- }
-
- /**
- * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::errorElement().
- */
- public function errorElement(array $element, array $error, array $form, array &$form_state) {
- return $element['target_id'];
- }
-
- /**
- * Element validate.
- */
- public function elementValidate($element, &$form_state) {
- }
-
- /**
- * Get the entity labels.
- */
- protected function getLabels(array $items) {
- $field = $this->field;
- $entity_ids = array();
- $entity_labels = array();
-
- // Build an array of entities ID.
- foreach ($items as $item) {
- $entity_ids[] = $item['target_id'];
- }
-
- // Load those entities and loop through them to extract their labels.
- $entities = entity_load_multiple($field['settings']['target_type'], $entity_ids);
-
- foreach ($entities as $entity_id => $entity) {
- $label = $entity->label();
- $key = "$label ($entity_id)";
- // Labels containing commas or quotes must be wrapped in quotes.
- if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
- $key = '"' . str_replace('"', '""', $key) . '"';
- }
- $entity_labels[] = $key;
- }
- return $entity_labels;
- }
-}