David Lohmeyer's Blog

Scaling block output as Twig variables in Drupal 8

With Drupal 8's Object Oriented focus, it's just a matter of time before cooler approaches to the theme's giant template.php (now yourtheme.theme) file are found. I've written a nicer way to get block output into the $variables array in page and node preprocess functions. What this allows you to do is use simple variables like {{ my_cool_block_stuff }} in yor Drupal Twig templates.

Adding the node class to a body HTML tag in Drupal 8

Doing some hacking at DrupalCon New Orleans and my use case is to add a node-* class to the body HTML element in Drupal 8. With the preprocess hook in D8 it looks like there is access to different variables than previously. To get the node id one has to look at the cache tags. Here's how I did it:

In mytheme.theme:

Improving Drupal 8 core search when using multiple content entity searches

Drupal 8 allows you to create multiple search pages which show up as primary tabs on the search page after performing a search. This is cool, but has the limitation of making the user input a search term a second time when they navigate to the other tab.

Easily pull down a production Drupal site with drush sql-sync and drush rsync

Both of these drush commands are awesome on their own, but they really need a wrapper script to become workhorses. I built this script to refresh a local Drupal environment with 1 command. You can choose file folder sync, databases, or both. This assumes you have working Drupal installations on both ends already (minus the database). So once you're setup with your settings files and Drupal codebase, you'd run this to populate the site. Click here for the Github repository. Summary:

Quick Node Clone module for Drupal 8

I've created an interim solution to the lack of the node_clone module for Drupal 8. During a project at work, I created a custom module with very similar functionality so I ended up making it a contrib Sandbox project at Drupal.org. I don't have any community module releases yet, so it's currently up for project review. Click here to check out the project!

The state of your custom Drupal 8 projects in 2015

Drupal 8 was just released! Awesome! Unfortunately, the state of contrib is a warzone, as usually happens after a big Drupal point release. Even more so now that everything has changed. The reality is that if you're doing something custom, you're not going to be able to take a contrib module like Rules, Relation, etc and use it how it is (yet). Thankfully with the Object Oriented approach in D8, you can extend classes of contrib modules that are unfinished and implement your own custom functionality. Gone are the long, complex module files from hell with little structure.

Creating links with custom classes in Drupal 8 modules

If you're getting a little frustrated with the new Drupal::l function in D8, which is actually going to be changed to Link in Drupal 9, here's how to create a link with class attributes right now. Attributes like link classes used to be defined on the l() function in Drupal 7, but are now passed into the Url() function as extra options.

Here's the ugly truth in how you link to something within module procedural code. You'll want to put the "use Drupal\Core\Url" at the top of your file:

How to make custom Guzzle requests in Drupal 8 modules

If you're sending a request to a custom URL in Drupal 8, you might be tempted to implement a solution using cURL or another library. However, Drupal core comes with Guzzle, a "PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services." As with most things in Drupal, it's not obvious how to use something immediately, so here's a demo to show you how to take care of sending a request to an arbitrary URL inside a custom Drupal module. You might use this example class to display a status code on some page.

How to check a user role inside a custom module in Drupal 8

If you need to check for a specific role such as 'administrator' in Drupal 8, the process is changed from past versions. The following is an example of how you'd check for an administrator role when rendering the user edit form. You might do this if you were unsetting form variables based on role:

Drupal 8 Site Building Gotcha: System Blocks

For those starting to build on Drupal 8, some things can be in the same place as Drupal 7 while others can be "hidden". While the idea is to move things to more logical places, sometimes it's confusing to site builders since they have to learn the system anew in some regards.

Related Posts