Creating a service

I created a custom page using a controller within a module, but my controller class has too much code in it. I want to take some code out and have a service handle this functionality. I will then have the controller use that service. The result will be a thin controller and more organised code.

Thin Controllers

So we know a controller in Drupal 8 is a class placed in a specific directory structure that is used as a page callback, as in it is a function that reads from the request object and returns a response - it displays a custom page. But what are thin controllers? I thought it was all about the base these days!

Services and Dependency Injection

While learning Drupal 8, I usually use Google to find tips and snippets of code to help my brain work out what the hell it's doing. The problem is, a lot of code posted around the interweb is usually out-of-date, depreciated, or just the wrong way of doing things. I keep hearing things like 'services', 'service container', and 'dependency injection' and you apparently you need to have those in mind when coding or you're just doing it wrong. So I explore what those are and what are the best Drupal coding practices when writing coding within classes.

Drupal Console

Drupal Console is a new shiny thing in Drupal - a Command Line Interface (CLI) built for Drupal 8, similar to Drush, used to do tasks like rebuild caches, install modules, and generate boilerplate code for module development. It's the latter that I'm focusing on at the moment while I learn Drupal 8.

Create a custom module

So to create my first generated module I need to do the following command:

Creating an installation profile

When I start developing a new website from scratch, I don't spend hours creating a new Drupal site, install the module and themes I need, and then troll through all the configurations to have the setup I want. No, I usually start from a previous, basic Drupal site build that has all the modules, themes and configurations I need as a foundation to build on - therefore I can replicate that in some way so I don't need to keep building it when I want to start work on a new site.

Quick module tips

Remove title block for a certain page

 * Implements hook_block_view_alter().
function MODULE_block_view_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block) {
  if ($block->getBaseId() === 'page_title_block') {
    // Remove the title block from the front page.
    $is_front = \Drupal::service('path.matcher')->isFrontPage();
    if ($is_front) {
      $build['#access'] = FALSE;

Hook Preprocess examples


 * Implements hook_preprocess_block().
function THEME_preprocess_block(&$variables) {
  switch ($variables['plugin_id']) {
    // Page title block.
    case 'page_title_block':
    // System Branding block.
    case 'system_branding_block':
      // Change site logo url.
      if ($variables['content']['site_logo']['#access']) {
        $variables['site_logo'] = drupal_get_path('theme', 'THEME') . '/logo.png';
      // Change site name.
      if ($variables['content']['site_name']['#access']) {
        $variables['site_name'] = 'The Carney Effect';
      // Change site slogan.
      if ($variables['content']['site_slogan']['#access']) {
        $variables['site_slogan']['#markup'] = 'ramblings of a wannabe Drupal developer';