Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Commit

Permalink
Issue #3002807 by chr.fritsch, mtodor: Sort autocomplete options alph…
Browse files Browse the repository at this point in the history
…abetically
  • Loading branch information
chrfritsch committed Oct 2, 2018
1 parent 2875d0c commit fae871f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/Plugin/Field/FieldWidget/Select2EntityReferenceWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
namespace Drupal\select2\Plugin\Field\FieldWidget;

use Drupal\Core\Entity\EntityReferenceSelection\SelectionWithAutocreateInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\OptGroup;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\user\EntityOwnerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Plugin implementation of the 'select2' widget.
Expand All @@ -20,7 +24,29 @@
* multiple_values = TRUE
* )
*/
class Select2EntityReferenceWidget extends Select2Widget {
class Select2EntityReferenceWidget extends Select2Widget implements ContainerFactoryPluginInterface {

/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* {@inheritdoc}
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->entityTypeManager = $entity_type_manager;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['third_party_settings'], $container->get('entity_type.manager'));
}

/**
* {@inheritdoc}
Expand Down Expand Up @@ -80,8 +106,12 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
$element = parent::formElement($items, $delta, $element, $form, $form_state);

$element['#target_type'] = $this->getFieldSetting('target_type');
$label_field = $this->entityTypeManager->getDefinition($element['#target_type'])->getKey('label') ?: '_none';
$element['#selection_handler'] = $this->getFieldSetting('handler');
$element['#selection_settings'] = $this->getFieldSetting('handler_settings') + ['match_operator' => $this->getSetting('match_operator')];
$element['#selection_settings'] = [
'match_operator' => $this->getSetting('match_operator'),
'sort' => ['field' => $label_field],
] + $this->getFieldSetting('handler_settings');
$element['#autocomplete'] = $this->getSetting('autocomplete');

if ($this->getSelectionHandlerSetting('auto_create') && ($bundle = $this->getAutocreateBundle())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Drupal\Tests\select2\FunctionalJavascript\FieldWidget;

use Drupal\Component\Serialization\Json;
use Drupal\Core\Url;
use Drupal\entity_test\Entity\EntityTestBundle;
use Drupal\entity_test\Entity\EntityTestMulRevPub;
use Drupal\entity_test\Entity\EntityTestWithBundle;
Expand Down Expand Up @@ -331,4 +333,38 @@ public function testAjaxCallbacksInBetween() {
$assert_session->elementNotExists('css', '.messages--error');
}

/**
* Tests that the autocomplete ordering is alphabetically.
*/
public function testAutocompleteOrdering() {
$this->createField('select2', 'node', 'test', 'entity_reference', [
'target_type' => 'entity_test_mulrevpub',
], [
'handler' => 'default:entity_test_mulrevpub',
'handler_settings' => [
'target_bundles' => ['entity_test_mulrevpub' => 'entity_test_mulrevpub'],
'auto_create' => FALSE,
],
], 'select2_entity_reference', ['autocomplete' => TRUE, 'match_operator' => 'CONTAINS']);

EntityTestMulRevPub::create(['name' => 'foo'])->save();
EntityTestMulRevPub::create(['name' => 'bar'])->save();
EntityTestMulRevPub::create(['name' => 'bar foo'])->save();
EntityTestMulRevPub::create(['name' => 'gaga'])->save();

$this->drupalGet('/node/add/test');
$url = $this->getSession()->evaluateScript("drupalSettings.select2['edit-select2'].ajax.url");

$url = Url::fromUserInput($url);
$url->setAbsolute(TRUE);
$url->setRouteParameter('q', 'f');

$response = \Drupal::httpClient()->get($url->toString());

$results = Json::decode($response->getBody()->getContents())['results'];

$expected = [['id' => 3, 'text' => 'bar foo'], ['id' => 1, 'text' => 'foo']];
$this->assertSame($expected, $results);
}

}

0 comments on commit fae871f

Please sign in to comment.