Say Goodbye To menu_get_object() @NYC Camp
Say Goodbye To menu_get_object() @NYC Camp
Fredric Mitchell | Senior Developer
April 8, 2014
Drupal 8 is bringing some great new features in addition to some fun DX changes. One of the ways I like to learn about these changes is to deconstruct the API. The best way to deconstruct the API is to dive into code that has a certain purpose, like looking at the Breadcrumb API. Since we know we're focusing on Drupal 7 to Drupal 8 changes, we can also use the excellent documentation in the change records to help us. In my upcoming NYCCamp presentation, I'll review some of the common API functions we used in Drupal 7 and how they've changed in Drupal 8.
What Node Am I On?
A lot of custom blocks that show related content, connected taxonomy, or any other relationship to currently viewed page typically depend on menu_get_object(). I'm sad to say that our old friend is gone. In Drupal 8, the way to get details about nodes are through the attributes of the request object in the global Drupal
namespace. While the DX of this implementation is currently being discussed, as of this writing, to get details about the current node:
attributes->get('node');
drupal_render() is EVERYTHING!
Consistency is a big theme (no pun intended) in Drupal 8. Render arrays are the main driver to staging content to be passed to the theme layer. As such, the theme() function is now gone. Instead, a new #theme array key is passed to build a piece of content programmatically. For old core theme functions, like theme_table() or theme_link(), you can pass in the 'table' or 'link' keyword, respectively, to the #type array key. As noted in the change record, to create a table of data with a pager, set the various keys, then pass it to drupal_render():
'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => array(
'id' => 'my-module-table',
),
);
$markup = drupal_render($table);
// Pager is not an element type, use #theme directly.
$pager = array('#theme' => 'pager');
$markup .= drupal_render($pager);
Want More?
If you can't make it out to NYC, definitely look for me at either the upcoming Chicago Meetup or Drupalcon Austin! I hope to you see in you in NYC this weekend!