Drupal articles

Drupal 8 Views: How to formulate the route name

Jo Fitzgerald
Jo Fitzgerald
4th Oct 2016

This article will explain how to formulate the route name for a view because there are very few sources for the information online.

Read more

Question: When should I define a service?

22nd Sep 2016

We've been getting stuck into Drupal 8 development and have been grappling with the concepts of services, dependency injection and containers.

One question that's come up has been:

When should I define a service?

Sometimes it's really obvious, suppose you are defining a new way to connect to some 'resource' a bit like a database, yeah that should be a service so that I can swap it out etc. Maybe you are providing a way to optimize images, yeah, that should be a service that accepts an image and returns an optimized version of that image.

But, should all...

Read more

Dependency Injection - how to access services in controllers

Jo Fitzgerald
Jo Fitzgerald
16th Aug 2016

If you are trying to get to grips with Dependency Injection in Drupal 8 then here is a walk-through of how I applied it in one of my Drupal 8 test projects.

I have a project I have been using to investigate Drupal 8 since alpha10 which has been invaluable in my learning process. But as a result, some of my code is over 2 years old and written when I barely had a grasp of Drupal 8 concepts.

In the past week a very old @todo jumped out to me:


// @todo Find a way to get a...
Read more

Drupal 8 Config Management - how should I add config to a D8 site?

Jo Fitzgerald
Jo Fitzgerald
9th Aug 2016

For me this is the biggest unanswered question hanging over my development of Drupal 8 websites: How should I add config to a Drupal 8 site?

This article will provide plenty of options, but unfortunately no definitive answer.

Read more

Drupal 8 Namespaces - class aliasing

Jo Fitzgerald
Jo Fitzgerald
2nd Aug 2016

Class Aliasing is the simple, but very useful solution to the problem of needing to use two classes (from different namespaces) with the same name.

Read more

How to write a PHPUnit functional test for Drupal 8

Jo Fitzgerald
Jo Fitzgerald
21st Jun 2016

This article will talk you through the steps to follow to write a simple PHPUnit functional (Kernel) test for Drupal 8.

I have been doing a lot of work on Drupal 8 migrations for the past few months so that will be the focus of the test.

Read more

Drupal 8 Event Subscribers - the successor to alter hooks

Jo Fitzgerald
Jo Fitzgerald
14th Jun 2016

In Drupal 7 if you wanted to tweak the functionality of an existing function then an alter hook was your best friend, but in Drupal 8 it's "all change!"

With the introduction of Symfony, your new BFF is an Event Subscriber. Alter hooks still exist, but it is recommended that we all move towards Events to fall in line with Symfony.

If you are interested in the comparison between alter hooks and events then I recommend this article from PreviousNext.

Introduction

In just 4 simple steps, this article will talk you through the process of writing a custom Event...

Read more

Checking if the current page is the homepage in Drupal 8 - not using drupal_is_front_page

10th May 2016

We will keep this one short and sweet. There is quite a bit of documentation pointing to the fact that drupal_is_front_page() still exists in Drupal 8 ... it doesn't!

Panic not. The solution is nice and easy, the following snippet will act as a drop in replacement for drupal_is_front_page() - simply returning TRUE or FALSE.


\Drupal::service('path.matcher')->isFrontPage();

Read more

How to write a (more useful) Drush command

3rd May 2016

Welcome to part two of our miniseries on Drush commands! Do check out part one if you haven't already. There you'll see what we've been up to with our pretend module, my_awesome_module. If you're writing a custom Drush command, it's pretty much guaranteed that you're needing to do something more complicated than write 'Hello World' to your database 5,000 times. Let's take a look at some Drush essentials.

Arguments and Options

You guessed it - just pass in an array. Simply give 'arguments' or 'options', and, for each one, a helpful description.


/**
 * Implements hook_drush_command().
 */
function my_awesome_module_drush_command() {...
Read more

Responsive images with srcset and sizes

Chris Didcote
Chris Didcote
12th Apr 2016

Responsive design is now the norm when it comes to developing modern websites, and most of us will be all too familiar with how we use media queries to get our sites looking different on a whole range of different devices. When it comes to getting our images to resize depending on the viewport / device, we now have two options: we can again rely on media queries, or we can use the srcset and sizes attributes that were defined in the element specification. It is the second option that we’ll be looking at in this article to see how...

Read more