'Render #pre_render callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. T

Created on 13 April 2023, over 1 year ago
Updated 7 May 2024, 5 months ago

Problem/Motivation

I have this error.
I am getting it on the edit page of all content where I have a select2.

Not sure what has changed.

Notice: Undefined index: callback_object in Drupal\Core\Form\FormState->getFormObject() (line 1167 of core/lib/Drupal/Core/Form/FormState.php).
Drupal\Core\Form\FormState->getFormObject() (Line: 179)
Drupal\Core\Entity\Element\EntityAutocomplete::processEntityAutocomplete(Array, Object, Array) (Line: 363)
Drupal\select2\Element\Select2::setAutocompleteRouteParameters(Array)
call_user_func_array('\Drupal\select2\Element\Select2::setAutocompleteRouteParameters', Array) (Line: 318)
Drupal\select2\Element\Select2::preRenderAutocomplete(Array)
call_user_func_array(Array, Array) (Line: 101)
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: 772)
Drupal\Core\Render\Renderer->doCallback('#pre_render', Array, Array) (Line: 363)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 435)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 435)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 435)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 201)
Drupal\Core\Render\Renderer->render(Array) (Line: 479)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 50)
__TwigTemplate_88552931e62762c5992b484ecfa692c05c4ad0f600097c721196a102c07a4025->doDisplay(Array, Array) (Line: 405)
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 378)
Twig\Template->display(Array) (Line: 390)
Twig\Template->render(Array) (Line: 55)
twig_render_template('themes/contrib/gin/templates/node/node-edit-form.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('node_edit_form', Array) (Line: 422)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 201)
Drupal\Core\Render\Renderer->render(Array, ) (Line: 241)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 564)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 242)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 132)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object) (Line: 142)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 174)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 81)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 54)
Drupal\simple_oauth\HttpMiddleware\BasicAuthSwap->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 43)
Drupal\webform_product\RedirectMiddleware->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 719)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Postponed: needs info

Version

1.14

Component

Field widgets

Created by

πŸ‡ΊπŸ‡ΈUnited States SocialNicheGuru

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

Comments & Activities

  • Issue created by @SocialNicheGuru
  • πŸ‡©πŸ‡ͺGermany chr.fritsch πŸ‡©πŸ‡ͺπŸ‡ͺπŸ‡ΊπŸŒ

    Please give some steps to reproduce this issue in a plain drupal installation

  • Status changed to Active over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States SocialNicheGuru

    I am on Drupal 9.5 now and I consistently get this on node/add/content-type or node/*/edit

    I think changes need to be made per: https://www.drupal.org/node/2966725 β†’

  • Status changed to Needs review about 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States JonMcL Brooklyn, NY

    I don't think this notice has anything to do with the message logged in trace on doTrustedCallback.

    I think the notice is because \Drupal\Core\Entity\Element\EntityAutocomplete::processEntityAutocomplete is expecting to get a form object from $form_state with this snippet from line 180:

        // Put entity into settings.
        $form_object = $form_state->getFormObject();
    

    Right below it is a check to see if $form_object has an expected value, and if not, it proceeds on.

    I don't know if this is a correct solution, but this seems to get us past the notice:

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

    Patch is attached.

  • Status changed to Postponed: needs info 5 months ago
  • πŸ‡©πŸ‡ͺGermany chr.fritsch πŸ‡©πŸ‡ͺπŸ‡ͺπŸ‡ΊπŸŒ

    Please give some steps to reproduce this issue in a plain drupal installation

Production build 0.71.5 2024