Warning: Undefined array key 1 in template_preprocess_html()

Created on 15 June 2024, 10 months ago

Problem/Motivation

When visiting the index.php with extra tacked on at the end, such as index.php.test, a warning is produced:

Warning: Undefined array key 1 in template_preprocess_html() (line 1312 of core/includes/theme.inc).
template_preprocess_html(Array, 'html', Array)
call_user_func_array('template_preprocess_html', Array) (Line: 261)
Drupal\Core\Theme\ThemeManager->render('html', Array) (Line: 491)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 248)
Drupal\Core\Render\Renderer->render(Array) (Line: 158)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 638)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 159)
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: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 186)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 2) (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 2, 1) (Line: 53)
Drupal\Core\StackMiddleware\Session->handle(Object, 2, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 2, 1) (Line: 28)
Drupal\Core\StackMiddleware\ContentLength->handle(Object, 2, 1) (Line: 32)
Drupal\big_pipe\StackMiddleware\ContentLength->handle(Object, 2, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 2, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 2, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 2, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 2, 1) (Line: 36)
Drupal\Core\StackMiddleware\AjaxPageState->handle(Object, 2, 1) (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 2) (Line: 166)
Drupal\Core\EventSubscriber\DefaultExceptionHtmlSubscriber->makeSubrequest(Object, '/system/404', 404) (Line: 122)
Drupal\Core\EventSubscriber\DefaultExceptionHtmlSubscriber->on404(Object) (Line: 109)
Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase->onException(Object, 'kernel.exception', Object)
call_user_func(Array, Object, 'kernel.exception', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.exception') (Line: 239)
Symfony\Component\HttpKernel\HttpKernel->handleThrowable(Object, Object, 1) (Line: 91)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 53)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 28)
Drupal\Core\StackMiddleware\ContentLength->handle(Object, 1, 1) (Line: 32)
Drupal\big_pipe\StackMiddleware\ContentLength->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: 36)
Drupal\Core\StackMiddleware\AjaxPageState->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 741)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

In logs for a production server, I see bots trying paths like this daily:

index.php.old
index.php.copy
index.php.tmp
index.php.1
index.php.~1~
index.phpsearch-results.dot
index.phpsearch_results.dot

Steps to reproduce

  1. Install Drupal
  2. Visit index.php.test to produce warning

I tested this Drupal 10. Not sure if it's fixed yet for 11.

๐Ÿ› Bug report
Status

Active

Version

10.3 โœจ

Component
Themeย  โ†’

Last updated 1 day ago

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States j_s

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

Merge Requests

Comments & Activities

  • Issue created by @j_s
  • Pipeline finished with Success
    10 months ago
    Total: 7871s
    #201336
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia onkararun

    Arun.k โ†’ changed the visibility of the branch 11.x to hidden.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia onkararun

    Arun.k โ†’ changed the visibility of the branch 11.x to active.

  • Status changed to Needs review 10 months ago
  • Status changed to Needs work 10 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Tagging for steps as just install drupal isn't triggering it.

    Will need to research why the value isn't there vs a check. Don't want to mask a larger issue.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States j_s

    The steps to reproduce are accurate from my original post for 10.3.0-rc1. I have installed it on simplytest.me and locally on ddev and can get the warning right away with the steps already outlined.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States j_s
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    11.x is the current development branch so fixes need to land there first and be proven there also, just FYI

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States j_s

    Ah, is there a reason all the past dev versions can still be selected for new issues? It's confusing when creating a new issue to have them available to select when you say issues and fixes need to be based in the latest dev version. The others should not be available to select. Perhaps I'll open an issue about that for improving Drupal issue reporting user experience.

    For this issue, I'll take a look at 11 and report back if I can reproduce or if this issue can be closed.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    If fixed in 11.x but not 10.3 then should find out if something was committed that maybe didn't get backported.

  • Ah, is there a reason all the past dev versions can still be selected for new issues?

    I am sure there is an issue about that.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Will say occasionally there are issues that target older branches. But know almost all have to land in 11.x first.

    Also 11. is just the name because "main" couldn't be used.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States j_s

    Confirmed on Drupal 11.0-dev. I installed on DDEV v1.23.2 with PHP 8.3.8 using:
    ddev composer create drupal/recommended-project:11.x-dev@dev

    Using the same steps to reproduce in the original post, I get this warning:

    Warning: Undefined array key 1 in template_preprocess_html() (line 1176 of core/includes/theme.inc).
    template_preprocess_html(Array, 'html', Array)
    call_user_func_array('template_preprocess_html', Array) (Line: 261)
    Drupal\Core\Theme\ThemeManager->render('html', Array) (Line: 446)
    Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 203)
    Drupal\Core\Render\Renderer->render(Array) (Line: 158)
    Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 593)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 153)
    Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
    Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object) (Line: 246)
    Symfony\Component\EventDispatcher\EventDispatcher::Symfony\Component\EventDispatcher\{closure}(Object, 'kernel.view', Object) (Line: 206)
    Symfony\Component\EventDispatcher\EventDispatcher->callListeners(Array, 'kernel.view', Object) (Line: 56)
    Symfony\Component\EventDispatcher\EventDispatcher->dispatch(Object, 'kernel.view') (Line: 188)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 2) (Line: 76)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 2, 1) (Line: 53)
    Drupal\Core\StackMiddleware\Session->handle(Object, 2, 1) (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 2, 1) (Line: 28)
    Drupal\Core\StackMiddleware\ContentLength->handle(Object, 2, 1) (Line: 32)
    Drupal\big_pipe\StackMiddleware\ContentLength->handle(Object, 2, 1) (Line: 106)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 2, 1) (Line: 85)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 2, 1) (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 2, 1) (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 2, 1) (Line: 36)
    Drupal\Core\StackMiddleware\AjaxPageState->handle(Object, 2, 1) (Line: 51)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 2) (Line: 166)
    Drupal\Core\EventSubscriber\DefaultExceptionHtmlSubscriber->makeSubrequest(Object, '/system/404', 404) (Line: 122)
    Drupal\Core\EventSubscriber\DefaultExceptionHtmlSubscriber->on404(Object) (Line: 109)
    Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase->onException(Object, 'kernel.exception', Object) (Line: 246)
    Symfony\Component\EventDispatcher\EventDispatcher::Symfony\Component\EventDispatcher\{closure}(Object, 'kernel.exception', Object) (Line: 206)
    Symfony\Component\EventDispatcher\EventDispatcher->callListeners(Array, 'kernel.exception', Object) (Line: 56)
    Symfony\Component\EventDispatcher\EventDispatcher->dispatch(Object, 'kernel.exception') (Line: 241)
    Symfony\Component\HttpKernel\HttpKernel->handleThrowable(Object, Object, 1) (Line: 91)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 53)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 28)
    Drupal\Core\StackMiddleware\ContentLength->handle(Object, 1, 1) (Line: 32)
    Drupal\big_pipe\StackMiddleware\ContentLength->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: 36)
    Drupal\Core\StackMiddleware\AjaxPageState->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 709)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States j_s

    Will say occasionally there are issues that target older branches. But know almost all have to land in 11.x first.

    Are there really issues permitted that target Drupal 4.6.x-dev?

    It should be made more obvious in initial issue creation that even if someone finds an issue in the latest stable release, it actually needs to be replicated in the latest unreleased major version up. You didn't even consider it a potential backport issue initially and just switched it to 11 from the get-go, which is not what I was reporting. But now at least this issue can be seen across the board.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia onkararun

    @joshuasosa when i installed drupal 11.x on DDEV v1.23.2 and PHP 8.3.8 version then i found no warning in this version of drupal. But yes i found warning on Drupal 11.0.x with installation on DDEV v1.23.2 with PHP 8.3.8 version. But we can remove this warning by doing the following changes in the file core/includes/theme.inc

    diff --git a/core/includes/theme.inc b/core/includes/theme.inc
    index 1603c5c65d..80182a1f59 100644
    --- a/core/includes/theme.inc
    +++ b/core/includes/theme.inc
    @@ -1139,85 +1139,88 @@ function _template_preprocess_default_variables() {
    * - page: A render element representing the page.
    */
    function template_preprocess_html(&$variables) {
    - $variables['page'] = $variables['html']['page'];
    - unset($variables['html']['page']);
    - $variables['page_top'] = NULL;
    - if (isset($variables['html']['page_top'])) {
    - $variables['page_top'] = $variables['html']['page_top'];
    - unset($variables['html']['page_top']);
    - }
    - $variables['page_bottom'] = NULL;
    - if (isset($variables['html']['page_bottom'])) {
    - $variables['page_bottom'] = $variables['html']['page_bottom'];
    - unset($variables['html']['page_bottom']);
    - }
    + // Check if the 'html' array key exists.
    + if (isset($variables['html'])) {
    + $variables['page'] = $variables['html']['page'] ?? null;
    + unset($variables['html']['page']);
    + $variables['page_top'] = NULL;
    + if (isset($variables['html']['page_top'])) {
    + $variables['page_top'] = $variables['html']['page_top'];
    + unset($variables['html']['page_top']);
    + }
    + $variables['page_bottom'] = NULL;
    + if (isset($variables['html']['page_bottom'])) {
    + $variables['page_bottom'] = $variables['html']['page_bottom'];
    + unset($variables['html']['page_bottom']);
    + }

    - $variables['html_attributes'] = new Attribute();
    + $variables['html_attributes'] = new Attribute();

    - // element attributes.
    - $language_interface = \Drupal::languageManager()->getCurrentLanguage();
    - $variables['html_attributes']['lang'] = $language_interface->getId();
    - $variables['html_attributes']['dir'] = $language_interface->getDirection();
    + // element attributes.
    + $language_interface = \Drupal::languageManager()->getCurrentLanguage();
    + $variables['html_attributes']['lang'] = $language_interface->getId();
    + $variables['html_attributes']['dir'] = $language_interface->getDirection();

    - if (isset($variables['db_is_active']) && !$variables['db_is_active']) {
    - $variables['db_offline'] = TRUE;
    - }
    + if (isset($variables['db_is_active']) && !$variables['db_is_active']) {
    + $variables['db_offline'] = TRUE;
    + }

    - // Add a variable for the root path. This can be used to create a class and
    - // theme the page depending on the current path (e.g. node, admin, user) as
    - // well as more specific data like path-frontpage.
    - $is_front_page = \Drupal::service('path.matcher')->isFrontPage();
    + // Add a variable for the root path. This can be used to create a class and
    + // theme the page depending on the current path (e.g. node, admin, user) as
    + // well as more specific data like path-frontpage.
    + $is_front_page = \Drupal::service('path.matcher')->isFrontPage();

    - if ($is_front_page) {
    - $variables['root_path'] = FALSE;
    - }
    - else {
    - $system_path = \Drupal::service('path.current')->getPath();
    - $variables['root_path'] = explode('/', $system_path)[1];
    - }
    + if ($is_front_page) {
    + $variables['root_path'] = FALSE;
    + }
    + else {
    + $system_path = \Drupal::service('path.current')->getPath();
    + $variables['root_path'] = explode('/', $system_path)[1] ?? null;
    + }

    - $site_config = \Drupal::config('system.site');
    - // Construct page title.
    - if (isset($variables['page']['#title']) && is_array($variables['page']['#title'])) {
    - // Do an early render if the title is a render array.
    - $variables['page']['#title'] = (string) \Drupal::service('renderer')->render($variables['page']['#title']);
    - }
    - if (!empty($variables['page']['#title'])) {
    - $head_title = [
    - // Marking the title as safe since it has had the tags stripped.
    - 'title' => Markup::create(trim(strip_tags($variables['page']['#title']))),
    - 'name' => $site_config->get('name'),
    - ];
    - }
    - // @todo Remove once views is not bypassing the view subscriber anymore.
    - // @see https://www.drupal.org/node/2068471 โ†’
    - elseif ($is_front_page) {
    - $head_title = [
    - 'title' => t('Home'),
    - 'name' => $site_config->get('name'),
    - ];
    - }
    - else {
    - $head_title = ['name' => $site_config->get('name')];
    - if ($site_config->get('slogan')) {
    - $head_title['slogan'] = strip_tags($site_config->get('slogan'));
    + $site_config = \Drupal::config('system.site');
    + // Construct page title.
    + if (isset($variables['page']['#title']) && is_array($variables['page']['#title'])) {
    + // Do an early render if the title is a render array.
    + $variables['page']['#title'] = (string) \Drupal::service('renderer')->render($variables['page']['#title']);
    + }
    + if (!empty($variables['page']['#title'])) {
    + $head_title = [
    + // Marking the title as safe since it has had the tags stripped.
    + 'title' => Markup::create(trim(strip_tags($variables['page']['#title']))),
    + 'name' => $site_config->get('name'),
    + ];
    + }
    + // @todo Remove once views is not bypassing the view subscriber anymore.
    + // @see https://www.drupal.org/node/2068471 โ†’
    + elseif ($is_front_page) {
    + $head_title = [
    + 'title' => t('Home'),
    + 'name' => $site_config->get('name'),
    + ];
    + }
    + else {
    + $head_title = ['name' => $site_config->get('name')];
    + if ($site_config->get('slogan')) {
    + $head_title['slogan'] = strip_tags($site_config->get('slogan'));
    + }
    }
    - }

    - $variables['head_title'] = $head_title;
    + $variables['head_title'] = $head_title;

    - // Create placeholder strings for these keys.
    - // @see \Drupal\Core\Render\HtmlResponseSubscriber
    - $types = [
    - 'styles' => 'css',
    - 'scripts' => 'js',
    - 'scripts_bottom' => 'js-bottom',
    - 'head' => 'head',
    - ];
    - $variables['placeholder_token'] = Crypt::randomBytesBase64(55);
    - foreach ($types as $type => $placeholder_name) {
    - $placeholder = '<' . $placeholder_name . '-placeholder token="' . $variables['placeholder_token'] . '">';
    - $variables['#attached']['html_response_attachment_placeholders'][$type] = $placeholder;
    + // Create placeholder strings for these keys.
    + // @see \Drupal\Core\Render\HtmlResponseSubscriber
    + $types = [
    + 'styles' => 'css',
    + 'scripts' => 'js',
    + 'scripts_bottom' => 'js-bottom',
    + 'head' => 'head',
    + ];
    + $variables['placeholder_token'] = Crypt::randomBytesBase64(55);
    + foreach ($types as $type => $placeholder_name) {
    + $placeholder = '<' . $placeholder_name . '-placeholder token="' . $variables['placeholder_token'] . '">';
    + $variables['#attached']['html_response_attachment_placeholders'][$type] = $placeholder;
    + }
    }
    }

  • Are there really issues permitted that target Drupal 4.6.x-dev?

    This is off-topic, truly. Please letโ€™s discuss it elsewhere and I am sure there already are issues.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States j_s
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States j_s

    @cilefen Then please do not change issue information when the report is not meant for 11.x and there is no information in the issue creation workflow to indicate that new issues must only be against 11.x.

  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand quietone

    Fixes are made on on 11.x (our main development branch) first, and are then back ported as needed according to our policies.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States j_s

    j_s โ†’ changed the visibility of the branch 3454769-undefined-array-key to hidden.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States j_s

    j_s โ†’ changed the visibility of the branch 10.4.x to hidden.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States j_s

    j_s โ†’ changed the visibility of the branch 11.x to hidden.

  • Merge request !1115011.x fix theme template error โ†’ (Open) created by j_s
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States j_s

    11.x MR added. Attached a patch for 10.3.

  • Pipeline finished with Success
    3 months ago
    Total: 1077s
    #418249
Production build 0.71.5 2024