'#autocomplete_route_name' does not work properly

Created on 17 May 2023, over 1 year ago
Updated 22 July 2023, over 1 year ago

Problem/Motivation

Code example that is not working as expected:
#autocomplete_route_name value does not override.

$form['select2'] = [
      '#type' => 'select2',
      '#title' => t('My select2 form element'),
      '#options' => [ 1 =>'foo', 2 => 'bar'],
      '#autocomplete' => TRUE,
      '#target_type'=> 'node',
      '#autocomplete_route_name' => 'app.custom_autocomplete',
      '#multiple' => TRUE,
      '#select2' => [
        'allowClear' => FALSE,
        'multiple' => TRUE,
      ],
    ];

Steps to reproduce

adding #autocomplete_route_name in $form.

API changes

current code error

protected static function setAutocompleteRouteParameters(array &$element): array {
    $complete_form = [];
    $element = EntityAutocomplete::processEntityAutocomplete($element, new FormState(), $complete_form);
    $element['#autocomplete_route_name'] = 'select2.entity_autocomplete';
    return $element;
}

fixed code that handles this issue.

protected static function setAutocompleteRouteParameters(array &$element): array {
    $complete_form = [];
    $autocomplete_route_name = $element['#autocomplete_route_name'] ?: 'select2.entity_autocomplete';
    $element = EntityAutocomplete::processEntityAutocomplete($element, new FormState(), $complete_form);
    $element['#autocomplete_route_name'] = $autocomplete_route_name;
    return $element;
 }
🐛 Bug report
Status

Needs work

Version

1.15

Component

Render element

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Mohamed Alaa
  • @mohamed-alaa opened merge request.
  • @mohamed-alaa opened merge request.
  • Status changed to Needs review over 1 year ago
  • 🇩🇪Germany chr.fritsch 🇩🇪🇪🇺🌍

    chr.fritsch made their first commit to this issue’s fork.

  • 🇺🇸United States SocialNicheGuru

    Drupal 9.5.9
    php 8.1

    I am getting this issue which is not caused by the patch.

    Warning: Undefined array key "#autocomplete_route_name" in Drupal\select2\Element\Select2::setAutocompleteRouteParameters() (line 368 of modules/contrib/select2/src/Element/Select2.php).
    Drupal\select2\Element\Select2::setAutocompleteRouteParameters(Array)
    call_user_func_array('\Drupal\select2\Element\Select2::setAutocompleteRouteParameters', Array) (Line: 318)

    Array:49 [▼
      "#title" => Drupal\Core\StringTranslation\TranslatableMarkup {#5709 ▶}
      "#description" => Drupal\Core\StringTranslation\TranslatableMarkup {#10687 ▶}
      "#field_parents" => []
      "#required" => false
      "#delta" => 0
      "#weight" => 0
      "#element_validate" => array:1 [▶]
      "#key_column" => "target_id"
      "#type" => "select"
      "#options" => array:1 [▶]
      "#default_value" => []
      "#multiple" => false
      "#cardinality" => -1
      "#select2" => array:2 [▶]
      "#after_build" => array:2 [▶]
      "#target_type" => "group"
      "#selection_handler" => "social"
      "#selection_settings" => array:3 [▶]
      "#autocomplete" => "1"
      "#suffix" => "<div id="group-selection-result"></div>"
      "#ajax" => array:3 [▶]
      "#field_name" => "groups"
      "#parents" => array:1 [▶]
      "#tree" => true
      "#input" => true
      "#sort_options" => false
      "#sort_start" => null
      "#process" => array:3 [▶]
      "#pre_render" => array:3 [▶]
      "#theme" => "select"
      "#theme_wrappers" => array:1 [▶]
      "#autocreate" => []
      "#empty_value" => ""
      "#value_callback" => array:2 [▶]
      "#defaults_loaded" => true
      "#array_parents" => array:2 [▶]
      "#processed" => true
      "#attributes" => array:6 [▶]
      "#title_display" => "before"
      "#description_display" => "after"
      "#errors" => null
      "#id" => "edit-groups"
      "#name" => "groups"
      "#value" => []
      "#ajax_processed" => true
      "#attached" => array:2 [▶]
      "#cache" => array:3 [▶]
      "#sorted" => true
      "#after_build_done" => true
    ]
  • 🇺🇸United States SocialNicheGuru

    i also get this issue, 🐛 'Render #pre_render callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. T Needs review

    Drupal\Core\Render\Renderer->doTrustedCallback(Array, Array, 'Render #pre_render callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was %s. See https://www.drupal.org/node/2966725 ', 'exception', 'Drupal\Core\Render\Element\RenderCallbackInterface') (Line: 788)
    Drupal\Core\Render\Renderer->doCallback('#pre_render', Array, Array) (Line: 374)

    I wonder if this might be the root cause

  • Status changed to Needs work over 1 year ago
Production build 0.71.5 2024