Undefined variable $entity warning on non-special pages

Created on 11 July 2024, 4 months ago

Problem/Motivation

An 'Undefined variable $entity' warning is shown or logged on non-special pages:
Warning: Undefined variable $entity in Drupal\metatag\MetatagManager->getSpecialMetatags() (line 524 of modules/contrib/metatag/src/MetatagManager.php).

Special pages are those checked in MetatagManager::getSpecialMetatags - front page, 403, and 404. Any other page will prompt this warning.

Steps to reproduce

Steps are for if you have dblog turned on. If you have error reporting turned on in your settings.php/settings.local.php, you can just do step 1 and go to any other page.

  1. Go to any existing page other than the front page where your metatags get added
  2. Login
  3. Review `Warning: Undefined variable $entity in Drupal\metatag\…` message

Proposed resolution

$entity is not passed to MetatagManager::getSpecialMetatags. Passing $entity resolves this issue. I will submit a merge request.

πŸ› Bug report
Status

Closed: cannot reproduce

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States wsantell

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

Comments & Activities

  • Issue created by @wsantell
  • πŸ‡ΊπŸ‡ΈUnited States wsantell
  • Status changed to Closed: cannot reproduce 4 months ago
  • πŸ‡ΊπŸ‡ΈUnited States wsantell

    This is an invalid issue that stems from changing to the 2.x branch and applying a patch from issue 2949963 which added this code:

    elseif ($entity && ($entity instanceof ContentEntityInterface) && !$entity->isNew()) {
          $entity_url = base_path() . $entity->toUrl('canonical', ['absolute' => TRUE])->getInternalPath();
          $system_config = \Drupal::configFactory()->get('system.site');
    
          if ($system_config->get('page.front') === $entity_url) {
            $metatags = $this->metatagDefaults->load('front');
          }
          elseif ($system_config->get('page.403') === $entity_url) {
            $metatags = $this->metatagDefaults->load('403');
          }
          elseif ($system_config->get('page.404') === $entity_url) {
            $metatags = $this->metatagDefaults->load('403');
          }
        }
Production build 0.71.5 2024