• Development

    Automatic Redirect by Path in Drupal 8

    This example shows how to intercept a Drupal page request, determine if the desired path matches the one we wish to redirect, find the “latest” node of a particular content type, and redirect to that node instead of the original page.

    I have a page on my site called Updates. This landing page exists only so that there is a menu item and proper breadcrumb trail; users never get to this landing page. Rather, they’re redirected to the latest update when they attempt to visit the landing page. Updates are nodes of the type update. Each update node (when viewed as a page) shows a Recent Updates block in the sidebar with links to of all of the most recent updates.

    The landing page is a Views-based page (so it doesn’t get accidentally deleted). There is also an All Views-based page that lists links to all updates. If the system cannot find the latest update the user will be redirected to the All page.

  • Development

    Drupal 8 – Formatting a Date Field in Twig

    Here are the four easiest ways I’ve found to show dates via Twig inside of a node template.

    They all result in the same output.

    Use the field’s formatter settings; include wrappers

    This is the only example that includes wrappers. The rest output just the date.

     

    Use the field’s formatter settings

    This will use the format defined in Content type » Manage Displays » Your View Mode.

     

    Use Twig date filter + a defined Drupal date format

    I have defined, via the Drupal UI, a date format. Here’s the config for it:

    uuid: 1a50c79f-96df-45de-9a21-32e0d8327806
    langcode: en
    status: true
    dependencies: { }
    id: short_mdyyyy
    label: 'Short - m/d/yyyy'
    locked: false
    pattern: n/j/Y

     

    Use Twig date filter

  • Development

    Quick and Dirty Debugging of Drupal 8 Migrations

    Introduction

    If you’re working on a migration in Drupal 8 and you’re finding yourself a little confused by the YAML-based configuration, I strongly recommend using a proper debugger so you can step through the code. You cannot step through the YAML, but you can step through the code that the YAML triggers. This blog post is my attempt to share a few tips/tricks I’ve learned along the way. This isn’t meant to be an exhaustive step-by-step debugging tutorial, so you may have to do some Googling to fill in the blanks.

    Disclaimer: Some of the information is PHPStorm-specific, but you should be able to adapt to whichever debugging tools you use.

  • Development

    Database Refreshing Pattern in Drupal 8 – Testing Config Changes

    I’ve been working on some Drupal 8 migration work. Given that most of the work I’m doing takes place in “config” I have to keep refreshing/reinstalling the config on my site. For a long time I would use Configuration Installer to pull in the changes I’ve made before repeatedly testing the migrations. The pattern is: Develop, overwrite config, test, develop, overwrite config, test.