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
Branches 2.x
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:
label:
type: label
label: 'Button label'
autocomplete_maxlength:
type: integer
label: 'Autocomplete field maximum length'
......@@ -2,10 +2,10 @@
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\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* A entity reference tree widget.
......@@ -35,29 +35,30 @@ class EntityReferenceTreeWidget extends EntityReferenceAutocompleteWidget {
if (empty($arr_target)) {
$str_target = '*';
}
else
{
else {
$str_target = implode(',', $arr_target);
}
//The id of the autocomplete text field.
//To ensure unqiueness when being used within Paragraph entities
//add the ids of any parent elements as a prefix to the the
//edit id.
// The id of the autocomplete text field.
// To ensure unqiueness when being used within Paragraph entities
// add the ids of any parent elements as a prefix to the the
// edit id.
$parents = $element['#field_parents'];
$id_prefix = '';
if (!empty($parents)) {
//Empty check necessary because implode will return the
//separator when given an empty array.
// Empty check necessary because implode will return the
// separator when given an empty array.
$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';
$arr_element['target_id']['#id'] = $edit_id;
$arr_element['target_id']['#tags'] = TRUE;
$arr_element['target_id']['#default_value'] = $items->referencedEntities();
$arr_element['target_id']['#maxlength'] = (int) ($this->getSetting('autocomplete_maxlength') ?? 1024);
$label = $this->getSetting('label');
if (!$label) {
......@@ -114,14 +115,16 @@ class EntityReferenceTreeWidget extends EntityReferenceAutocompleteWidget {
*/
public static function defaultSettings() {
return [
// JsTree theme
'theme' => 'default',
// JsTree theme.
'theme' => 'default',
// Using dot line.
'dots' => 0,
'dots' => 0,
// Button label.
'label' => '',
'label' => '',
// Dialog title.
'dialog_title' => '',
'dialog_title' => '',
// Maximum length of autocomplete.
'autocomplete_maxlength' => '1024',
] + parent::defaultSettings();
}
......@@ -132,28 +135,28 @@ class EntityReferenceTreeWidget extends EntityReferenceAutocompleteWidget {
$element = [];
// JsTRee theme.
$element['theme'] = [
'#type' => 'radios',
'#title' => t('JsTree theme'),
'#default_value' => $this->getSetting('theme'),
'#required' => TRUE,
'#options' => array(
'default' => $this
->t('Default'),
'default-dark' => $this
->t('Default Dark'),
),
'#type' => 'radios',
'#title' => t('JsTree theme'),
'#default_value' => $this->getSetting('theme'),
'#required' => TRUE,
'#options' => [
'default' => $this
->t('Default'),
'default-dark' => $this
->t('Default Dark'),
],
];
// Tree dot.
$element['dots'] = [
'#type' => 'radios',
'#title' => t('Dot line'),
'#default_value' => $this->getSetting('dots'),
'#options' => array(
0 => $this
->t('No'),
1 => $this
->t('Yes'),
),
'#type' => 'radios',
'#title' => t('Dot line'),
'#default_value' => $this->getSetting('dots'),
'#options' => [
0 => $this
->t('No'),
1 => $this
->t('Yes'),
],
];
// Button label.
$element['label'] = [
......@@ -168,6 +171,15 @@ class EntityReferenceTreeWidget extends EntityReferenceAutocompleteWidget {
'#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;
}
......@@ -177,7 +189,7 @@ class EntityReferenceTreeWidget extends EntityReferenceAutocompleteWidget {
public function settingsSummary() {
$summary = [];
// JsTree theme.
$summary[] = t('JsTree theme: @theme', array('@theme' => $this->getSetting('theme')));
$summary[] = t('JsTree theme: @theme', ['@theme' => $this->getSetting('theme')]);
// Button label.
if ($label = $this->getSetting('label')) {
$summary[] = t('Button label: @label', ['@label' => $label]);
......@@ -186,7 +198,8 @@ class EntityReferenceTreeWidget extends EntityReferenceAutocompleteWidget {
if ($label = $this->getSetting('dialog_title')) {
$summary[] = t('Dialog title: @title', ['@title' => $label]);
}
return $summary;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment