Split from #2091399: [META] Remove menu_get_object()

Only one instance which is in taxonomy_term_is_page().

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ekes’s picture

From the original issue, this one instance:

menu_get_object() is breaking (fatal error) taxonomy_term_is_page() if the core taxonomy/term/% page view is enabled (views.view.taxonomy_term.yml).

To reproduce, add content in terms. Enable view. Clear cache. Then $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT)->getPath(); returns NULL. Hence taxonomy_term_is_page() tries to access id on non-object $page_term.

ekes’s picture

Status: Active » Needs review
FileSize
682 bytes

Something as simple as attached?

 function taxonomy_term_is_page(Term $term) {
-  $page_term = menu_get_object('taxonomy_term', 2);
-  return (!empty($page_term) ? $page_term->id() == $term->id() : FALSE);
+  $page_term = \Drupal::request()->attributes->get('taxonomy_term');
+  return (is_object($page_term) && $page_term instanceof Term ? $page_term->id() == $term->id() : FALSE);
ekes’s picture

Following suggestion in irc from timplunkett.

 function taxonomy_term_is_page(Term $term) {
-  $page_term = menu_get_object('taxonomy_term', 2);
-  return (!empty($page_term) ? $page_term->id() == $term->id() : FALSE);
+  $request = \Drupal::request();
+  if ($request->attributes->has('taxonomy_term')) {
+    $page_term = $request->attributes->get('taxonomy_term');
+    return $page_term->id() == $term->id();
+  }
+  return FALSE;
 }

Status: Needs review » Needs work

The last submitted patch, menu_get_object.2095971-03.patch, failed testing.

ekes’s picture

Status: Needs work » Needs review

#3: menu_get_object.2095971-03.patch queued for re-testing.

tim.plunkett’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me, and was the only instance of this.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 8.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.