Skip to content
Snippets Groups Projects
Commit 720390c1 authored by Geoffrey Roberts's avatar Geoffrey Roberts Committed by Mingsong
Browse files

Issue #3456598 by mingsong, geoffreyr, sirclickalot: Extend autocomplete field...

Issue #3456598 by mingsong, geoffreyr, sirclickalot: Extend autocomplete field length, allow more terms to be selected
parent 076bcb8b
No related branches found
No related tags found
1 merge request!113456598: Allow autocomplete field to be extended to any length beyond 1024
Pipeline #397413 passed with warnings
...@@ -26,4 +26,6 @@ field.widget.settings.entity_reference_tree: ...@@ -26,4 +26,6 @@ field.widget.settings.entity_reference_tree:
label: label:
type: label type: label
label: 'Button label' label: 'Button label'
autocomplete_maxlength:
type: integer
label: 'Autocomplete field maximum length'
...@@ -2,10 +2,10 @@ ...@@ -2,10 +2,10 @@
namespace Drupal\entity_reference_tree\Plugin\Field\FieldWidget; namespace Drupal\entity_reference_tree\Plugin\Field\FieldWidget;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget; use Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/** /**
* A entity reference tree widget. * A entity reference tree widget.
...@@ -35,29 +35,30 @@ class EntityReferenceTreeWidget extends EntityReferenceAutocompleteWidget { ...@@ -35,29 +35,30 @@ class EntityReferenceTreeWidget extends EntityReferenceAutocompleteWidget {
if (empty($arr_target)) { if (empty($arr_target)) {
$str_target = '*'; $str_target = '*';
} }
else else {
{
$str_target = implode(',', $arr_target); $str_target = implode(',', $arr_target);
} }
//The id of the autocomplete text field. // The id of the autocomplete text field.
//To ensure unqiueness when being used within Paragraph entities // To ensure unqiueness when being used within Paragraph entities
//add the ids of any parent elements as a prefix to the the // add the ids of any parent elements as a prefix to the the
//edit id. // edit id.
$parents = $element['#field_parents']; $parents = $element['#field_parents'];
$id_prefix = ''; $id_prefix = '';
if (!empty($parents)) { if (!empty($parents)) {
//Empty check necessary because implode will return the // Empty check necessary because implode will return the
//separator when given an empty array. // separator when given an empty array.
$id_prefix = str_replace('_', '-', implode('-', array_merge($parents))) . '-'; $id_prefix = str_replace('_', '-', implode('-', array_merge($parents))) . '-';
} }
//Including the delta in the id to follow the Entity Reference module's convention. // Including the delta in the id
// to follow the Entity Reference module's convention.
$edit_id = 'edit-' . $id_prefix . str_replace('_', '-', $items->getName()) . '-' . $delta . '-target-id'; $edit_id = 'edit-' . $id_prefix . str_replace('_', '-', $items->getName()) . '-' . $delta . '-target-id';
$arr_element['target_id']['#id'] = $edit_id; $arr_element['target_id']['#id'] = $edit_id;
$arr_element['target_id']['#tags'] = TRUE; $arr_element['target_id']['#tags'] = TRUE;
$arr_element['target_id']['#default_value'] = $items->referencedEntities(); $arr_element['target_id']['#default_value'] = $items->referencedEntities();
$arr_element['target_id']['#maxlength'] = (int) ($this->getSetting('autocomplete_maxlength') ?? 1024);
$label = $this->getSetting('label'); $label = $this->getSetting('label');
if (!$label) { if (!$label) {
...@@ -114,14 +115,16 @@ class EntityReferenceTreeWidget extends EntityReferenceAutocompleteWidget { ...@@ -114,14 +115,16 @@ class EntityReferenceTreeWidget extends EntityReferenceAutocompleteWidget {
*/ */
public static function defaultSettings() { public static function defaultSettings() {
return [ return [
// JsTree theme // JsTree theme.
'theme' => 'default', 'theme' => 'default',
// Using dot line. // Using dot line.
'dots' => 0, 'dots' => 0,
// Button label. // Button label.
'label' => '', 'label' => '',
// Dialog title. // Dialog title.
'dialog_title' => '', 'dialog_title' => '',
// Maximum length of autocomplete.
'autocomplete_maxlength' => '1024',
] + parent::defaultSettings(); ] + parent::defaultSettings();
} }
...@@ -132,28 +135,28 @@ class EntityReferenceTreeWidget extends EntityReferenceAutocompleteWidget { ...@@ -132,28 +135,28 @@ class EntityReferenceTreeWidget extends EntityReferenceAutocompleteWidget {
$element = []; $element = [];
// JsTRee theme. // JsTRee theme.
$element['theme'] = [ $element['theme'] = [
'#type' => 'radios', '#type' => 'radios',
'#title' => t('JsTree theme'), '#title' => t('JsTree theme'),
'#default_value' => $this->getSetting('theme'), '#default_value' => $this->getSetting('theme'),
'#required' => TRUE, '#required' => TRUE,
'#options' => array( '#options' => [
'default' => $this 'default' => $this
->t('Default'), ->t('Default'),
'default-dark' => $this 'default-dark' => $this
->t('Default Dark'), ->t('Default Dark'),
), ],
]; ];
// Tree dot. // Tree dot.
$element['dots'] = [ $element['dots'] = [
'#type' => 'radios', '#type' => 'radios',
'#title' => t('Dot line'), '#title' => t('Dot line'),
'#default_value' => $this->getSetting('dots'), '#default_value' => $this->getSetting('dots'),
'#options' => array( '#options' => [
0 => $this 0 => $this
->t('No'), ->t('No'),
1 => $this 1 => $this
->t('Yes'), ->t('Yes'),
), ],
]; ];
// Button label. // Button label.
$element['label'] = [ $element['label'] = [
...@@ -168,6 +171,15 @@ class EntityReferenceTreeWidget extends EntityReferenceAutocompleteWidget { ...@@ -168,6 +171,15 @@ class EntityReferenceTreeWidget extends EntityReferenceAutocompleteWidget {
'#default_value' => $this->getSetting('dialog_title'), '#default_value' => $this->getSetting('dialog_title'),
]; ];
$element['autocomplete_maxlength'] = [
'#type' => 'number',
'#title' => $this->t('Autocomplete field maximum length'),
'#description' => $this->t('Use this for fields which can accept a larger number of taxonomy terms.'),
'#default_value' => $this->getSetting('autocomplete_maxlength'),
'#min' => 1024,
'#step' => 1,
];
return $element; return $element;
} }
...@@ -177,7 +189,7 @@ class EntityReferenceTreeWidget extends EntityReferenceAutocompleteWidget { ...@@ -177,7 +189,7 @@ class EntityReferenceTreeWidget extends EntityReferenceAutocompleteWidget {
public function settingsSummary() { public function settingsSummary() {
$summary = []; $summary = [];
// JsTree theme. // JsTree theme.
$summary[] = t('JsTree theme: @theme', array('@theme' => $this->getSetting('theme'))); $summary[] = t('JsTree theme: @theme', ['@theme' => $this->getSetting('theme')]);
// Button label. // Button label.
if ($label = $this->getSetting('label')) { if ($label = $this->getSetting('label')) {
$summary[] = t('Button label: @label', ['@label' => $label]); $summary[] = t('Button label: @label', ['@label' => $label]);
...@@ -186,7 +198,8 @@ class EntityReferenceTreeWidget extends EntityReferenceAutocompleteWidget { ...@@ -186,7 +198,8 @@ class EntityReferenceTreeWidget extends EntityReferenceAutocompleteWidget {
if ($label = $this->getSetting('dialog_title')) { if ($label = $this->getSetting('dialog_title')) {
$summary[] = t('Dialog title: @title', ['@title' => $label]); $summary[] = t('Dialog title: @title', ['@title' => $label]);
} }
return $summary; return $summary;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment