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

Issue #3089842 by xavier.masson: Drag to re-order doesn't work withou… #98

Open
wants to merge 1 commit into
base: 8.x-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/select2.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
$(this).select2(config);

// Copied from https://github.com/woocommerce/woocommerce/blob/master/assets/js/admin/wc-enhanced-select.js#L118
if (Object.prototype.hasOwnProperty.call(config, 'ajax') && config.multiple) {
if (config.multiple) {
var $select = $(this);
var $list = $select.next('.select2-container').find('ul.select2-selection__rendered');
Sortable.create($list[0], {
Expand Down
23 changes: 11 additions & 12 deletions src/Element/Select2.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,17 @@ public static function processSelect(&$element, FormStateInterface $form_state,
$element['#options'] = $empty_option + $element['#options'];
}

if ($element['#multiple']) {
$values = isset($element['#value']) ? $element['#value'] : $element['#default_value'];
$values = is_array($values) ? $values : [$values];

// Apply field values order on the options.
if (!empty($values)) {
$flipped_keys = array_intersect_key(array_flip($values), $element['#options']);
$element['#options'] = array_replace($flipped_keys, $element['#options']);
}
}

// Set the type from select2 to select to get proper form validation.
$element['#type'] = 'select';

Expand Down Expand Up @@ -305,18 +316,6 @@ public static function preRenderAutocomplete($element) {
}
$element = call_user_func_array($value_callable, [&$element]);

// Reduce options to the preselected ones and bring them in the correct
// order.
$options = OptGroup::flattenOptions($element['#options']);
$values = isset($element['#value']) ? $element['#value'] : $element['#default_value'];
$values = is_array($values) ? $values : [$values];
$element['#options'] = [];
foreach ($values as $value) {
if (isset($options[$value])) {
$element['#options'][$value] = $options[$value];
}
}

/** @var \Drupal\Core\Access\AccessManagerInterface $access_manager */
$access_manager = \Drupal::service('access_manager');
$access = $access_manager->checkNamedRoute($element['#autocomplete_route_name'], $element['#autocomplete_route_parameters'], \Drupal::currentUser(), TRUE);
Expand Down
50 changes: 50 additions & 0 deletions src/Plugin/EntityReferenceSelection/ViewsWithAutoCreation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Drupal\select2\Plugin\EntityReferenceSelection;

use Drupal\Core\Entity\EntityReferenceSelection\SelectionWithAutocreateInterface;
use Drupal\user\EntityOwnerInterface;
use Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection;

/**
* Plugin implementation of the 'selection' entity_reference with autocreation.
*
* @EntityReferenceSelection(
* id = "views_with_autocreation",
* label = @Translation("Views: Filter by an entity reference view and allow autocreation"),
* group = "views",
* weight = 0
* )
*/
class ViewsWithAutoCreation extends ViewsSelection implements SelectionWithAutocreateInterface {

/**
* {@inheritdoc}
*/
public function createNewEntity($entity_type_id, $bundle, $label, $uid) {
$entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
$bundle_key = $entity_type->getKey('bundle');
$label_key = $entity_type->getKey('label');
$entity = $this->entityTypeManager->getStorage($entity_type_id)->create([
$bundle_key => $bundle,
$label_key => $label,
]);
if ($entity instanceof EntityOwnerInterface) {
$entity->setOwnerId($uid);
}
return $entity;
}

/**
* {@inheritdoc}
*/
public function validateReferenceableNewEntities(array $entities) {
return array_filter($entities, function ($entity) {
if (isset($this->configuration['handler_settings']['auto_create_bundle'])) {
return ($entity->bundle() === $this->configuration['handler_settings']['auto_create_bundle']);
}
return TRUE;
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
// with 'autocreate' or 'autocomplete' we want to ignore that.
$element['#multiple'] = $this->multiple && (count($this->options) > 1 || isset($element['#autocreate']) || $element['#autocomplete']);

if ($element['#autocomplete'] && $element['#multiple']) {
if ($element['#multiple']) {
$entity_definition = $this->entityTypeManager->getDefinition($element['#target_type']);
$message = $this->t("Drag to re-order @entity_types.", ['@entity_types' => $entity_definition->getPluralLabel()]);
if (!empty($element['#description'])) {
Expand Down