Recent Blog Posts

How to attach a CSS or JS library to a View in Drupal 8

File this one under the 'it's obvious, but only after you've done it' category—I needed to attach a CSS library to a view in Drupal 8 via a custom module so that, wherever the view displayed on the site, the custom CSS file from my module was attached. The process for CSS and JS libraries is pretty much identical, but here's how I added a CSS file as a library, and made sure it was attached to my view:

Add the CSS file as a library

In Drupal 8, drupal_add_css(), drupal_add_js(), and drupal_add_library() were removed (for various reasons), and now, to attach CSS or JS assets to views, nodes, etc., you need to use Drupal's #attached functionality to 'attach' assets (like CSS and JS) to rendered elements on the page.

In my custom module (custom.module), I added the CSS file css/custom_view.css:

Hide the page title depending on a checkbox field in a particular content type

In Drupal 8, many small things have changed, but my willingness to quickly hack something out in a few lines of code/config instead of installing a relatively large module to do the same thing hasn't :-)

I needed to add a checkbox to control whether the page title should be visible in the rendered page for a certain content type on a Drupal 8 site, and there are a few different ways you can do this (please suggest alternatives—especially if they're more elegant!), but I chose to do the following:

  1. Add a 'Display Title' boolean field (checkbox, using the field label as the title, and setting off to 0 and on to 1 in the field settings) to the content type (page in this example).

    Drupal 8 Basic Page 'Display Title' checkbox

Reviving an old dresser by rebuilding the wooden drawer rails

My wife and I needed an extra dresser to keep up with the growing family (third baby is on the way in a few months!), and since we would rather buy things that last—but not buy new if we can save a bundle of money—we bought a used wood dresser on Craigslist:

Craigslist wood dresser

First lesson: always inspect every last bit of furniture before loading it up and hauling it away! Most of the rails were in not-great condition, and the guides on the drawers weren't in great shape either:

Craigslist dresser with old broken wood drawer rail guide

We decided to make the best of the situation and make as good a repair as possible, resulting in much improved (like new!) rails:

The Crêpe I made in honor of Juno's mission to Jupiter

I forgot to post here, after posting to different social networks—when NASA's Juno orbiter was placed in Jupiter's orbit, it just so happened to coincide with my family's Sunday night crêpe tradition (see how to make gluten/dairy/egg-free crepes, or how to make normal crepes).

So what did I do? I used some peanut butter, cream cheese, and nutella to try to depict a tasty food version of Jupiter:

A Jupiter crepe in honor of Juno's mission milestone today

Migrating Link fields with multiple properties with Drupal 8

Today I needed to migrate a URL/Link into a Drupal 8 site, and I was scratching my head over how to migrate it so there were distinct values for the URL (the actual link) and the Label (the 'title' that displays to end users and is clickable). Drupal 8's Link field type allows you to set a URL in addition to an optional (or required) label, but by default, if you just migrate the URL, the label will be blank.

I first set up the migration config like so:

  field_url: source_url

And source_url was defined in the migration's source.fields configuration.

In my case, the source data didn't have a label, but I wanted to set a default label so the Drupal 8 site could display that as the clickable link (instead of an ugly long URL). To do that, it's similar to migrating a formatted text field, where you can migrate individual components of the field using the syntax [field_name]/[component]. In a Link field's case, it looks like:


Subscribe to Jeff Geerling's Blog