openeuropa / oe_theme

Drupal 10 theme based on the Europa Component Library (ECL).
European Union Public License 1.2
30 stars 31 forks source link

Conflict with admin_toolbar in _oe_theme_preprocess_search_input_text() #1176

Open donquixote opened 1 year ago

donquixote commented 1 year ago

Background

Admin Toolbar has an optional search field in the admin menu. This is themed using the front-end theme, if visible on front-end pages.

I think most projects do not have admin_toolbar installed, but some developers might install it locally, or some projects might choose to use it.

oe_theme has a preprocess function and a template for 'input__search' theme hook variant. The preprocess function targets all search fields without distinction, including the one from admin_toolbar_search.

Problem

The preprocess function and the template are incompatible with the search field in admin toolbar, causing a notice.

Steps to reproduce

Install oe_theme as default theme, and install admin_toolbar and admin_toolbar_search. Enable error logging and/or reporting to the page.

Visit any front-end page as user with access to the toolbar. If you don't see the notice yet, clear the cache and try again.

Technical info, backtrace

Error message

Notice: Undefined index: name in _oe_theme_preprocess_search_input_text() (line 1206 of themes/contrib/oe_theme/oe_theme.theme).

_oe_theme_preprocess_search_input_text(Array) (Line: 1178)
oe_theme_preprocess_input__search(Array, 'input__search', Array)
call_user_func_array('oe_theme_preprocess_input__search', Array) (Line: 287)
Drupal\Core\Theme\ThemeManager->render('input__search', Array) (Line: 422)
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: 94)
template_preprocess_toolbar(Array, 'toolbar', Array)
call_user_func_array('template_preprocess_toolbar', Array) (Line: 287)
Drupal\Core\Theme\ThemeManager->render('toolbar', Array) (Line: 422)
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: 79)
__TwigTemplate_3481b47f31b0c0146a19839ff46b37122636f8c286dcc4febb12af0bd058d6ad->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/oe_theme/templates/html/html.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('html', Array) (Line: 422)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 201)
Drupal\Core\Render\Renderer->render(Array) (Line: 162)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 564)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 163)
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: 163)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 80)
Symfony\Component\HttpKernel\HttpKernel->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: 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: 709)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
donquixote commented 1 year ago

Superficially this could be fixed by adding ?? NULL after the ['name'] like this:

    'name' => $element['#attributes']['name'] ?? NULL,

However, the real solution is to not use the text-input from ecl-twig at all for the toolbar search field. So, to skip entirely the input--search.html.twig from oe_theme.

donquixote commented 1 year ago

we can do this:

diff --git a/oe_theme.theme b/oe_theme.theme
index e803089a6..a32b03066 100644
--- a/oe_theme.theme
+++ b/oe_theme.theme
@@ -155,6 +155,14 @@ function oe_theme_theme_suggestions_input_alter(array &$suggestions, array $vari
   if ($element['#type'] === 'date' && $element['#attributes']['type'] === 'time') {
     $suggestions[] = 'input__time';
   }
+
+  if (isset($element['#id']) && $element['#id'] === 'admin-toolbar-search-input') {
+    // Don't use the `input--search.html.twig` because it does not work here.
+    $suggestions = array_diff($suggestions, ['input__search']);
+    // Add the base theme hook as a suggestion.
+    // Don't remove other suggestions that might be added by modules.
+    array_unshift($suggestions, 'input');
+  }
 }

 /**

But now I see another problem: The search field has margin top and bottom which looks wrong in the admin toolbar.