The more I work with Drupal 8, the more I realize how much has changed for developers in the Drupal community. While the transition to a modern, object-oriented system is what's best for the longevity of the platform, it certainly doesn't come without challenges. As someone who doesn't come from an OOP background, I've found the transition difficult at times. In many cases, I know exactly what I want to do, just not how to do it the "Drupal 8 way". On top of this, tutorials and blog posts on D8 are all over the map in terms of accuracy. Many posts written during D8's development cycle are no longer applicable because of API changes, etc.
Below is a list of snippets that might be helpful to site builders or developers more familiar with D7 hooks and procedural. It might also be useful to OOP folks who are new to Drupal in general. My goal below is to add to and update these snippets over time.
Routes & Links
Determine the Current Drupal Route
Need to know what the current Drupal route is or need to run some logic against the current route? You can get the current route like so:
$route = \Drupal::routeMatch()->getRouteName();
To some, the \Drupal::routeMatch()
syntax might look foreign (it did to me). Here's a rundown of what's happening here:
First, \Drupal
. This is calling the global Drupal class, which, in Drupal 8, is a bridge between procedural and OO methods of writing Drupal code. The following comes from the documentation:
This class acts as a unified global accessor to arbitrary services within the system in order to ease the transition from procedural code to injected OO code.
Right. Moving on to ::routeMatch()
. Here we're using the routeMatch()
method which "Retrieves the currently active route match object." Simple enough. But what is "::" all about? This StackOverflow answer helped me to understand what that's all about.
From there, the getRouteName() method returns the current route name as a string. Here are some example routes: entity.node.canonical
, view.frontpage
and node.type_add
.
Is this the Front Page Route?
Need to check if the current route is the front page route? There's a service and method for that:
// Is the current route/path the front page?
if ($is_front = \Drupal::service('path.matcher')->isFrontPage()) {}
Here we're calling the path.matcher
service (defined in /core/core.services.yml
) and using the isFrontPage()
method. For more on services, check out the "Services and Dependency Injection Container" documentation on api.drupal.org which helped me understand how all of these bits work together and the why of their structure.
Get the Requested Path
Need to know what the current page's requested path was, as opposed to the route? You can do this:
$current_uri = \Drupal::request()->getRequestUri();
Redirect to a Specific Route
Need to redirect to a specific page? In Drupal 7, you would likely handle this with drupal_goto()
in your page callback function. In Drupal 8, you can use RedirectResponse()
for that. Here is the relevant changelog.
Here are some examples, borrowed heavily from said changelog. First, in procedural PHP:
use Symfony\Component\HttpFoundation\RedirectResponse;
function my_redirect() {
return new RedirectResponse(\Drupal::url('user.page'));
}
Here is how you would use a Drupal 8 controller to accomplish the same thing:
use Drupal\Core\Controller\ControllerBase;
class MyControllerClass extends ControllerBase {
public function foo() {
//...
return $this->redirect('user.page');
}
}
Links on the Fly
Drupal 7 and prior relied heavily on the l() function. In fact, I would wager this was my most used function over the years. In Drupal 8, if you need to create links on the fly, utilize the Link class
$link = \Drupal\Core\Link::fromTextAndUrl($text, $url);
Working with Entities
Query Database for Entities
If you need to query the database for some nodes (or any other entity) you should use the entityQuery service. The syntax should be pretty familiar to most D7 developers who have used EntityFieldQuery:
// Query for some entities with the entity query service.
$query = \Drupal::entityQuery('node')
->condition('status', 1)
->condition('type', 'article')
->range(0, 10)
->sort('created', 'DESC');
$nids = $query->execute();
Loading Entities
If you need to load the actual entities, you can do so a number of ways:
While the following will technically work in Drupal 8:
$node = entity_load_multiple('node', $nids);
This method has been deprecated in Drupal 8 and will be removed before Drupal 9, in favor of methods overriding Entity::loadMultiple()
. To future-proof your code, you would do something like the following:
$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($nids);
Here's how you would do similar for a single node:
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
Here are a few other entity snippets that might be useful:
// Link to an entity using the entity's link method.
$author_link = $user->toLink();
// Do the same thing, but customize the link text.
$author_link = $user->toLink('Some Custom Text');
// Given a node object, here's how to determine its type:
$type = $node->getType();
// To get the full user entity of the node's author:
$author = $node->getOwner();
// To get the raw ID of the author of a node:
$author_id = $node->getOwnerId();
Image Styles
Need to whip up an image using a particular image style on the fly? This will work for that:
// Create an instance of an image using a specific image style, given a path to a file.
$style = \Drupal\image\Entity\ImageStyle::load('yourStyle_image');
$img_path = $user->field_profile_some_image->entity->getFileUri();
$img_style_url = $style->buildUrl($img_path);
That's it for now. I intend to keep this post updated as we learn more and more about the new world of Drupal 8. If you have a snippet worth sharing, drop us a line via Twitter and we’ll add it to this post (with credit of course).