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

Tagged with: , ,
Posted in 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. Read more ›

Tagged with: , ,
Posted in 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.

Read more ›

Tagged with: ,
Posted in Development

Lathe Stand Plans

Here’s my 5-foot adaptation of the lathe stand from https://www.youtube.com/watch?v=MJZycw5NUF8 (Thanks Keith!)

Here’s what it took for supplies:

  1. (1) 4×8  3/4 birch plywood
  2. (7) 2×6-8ft
  3. (3) 50lb bags of sand (had to fill trash 4 trash bags because 50lb bags too big)
  4. (1) 4×8 hardboard
  5. Lots of Kreg pocket hole screws
  6. Wood glue
  7. Countersink bits

My legs are at 5 degrees. The only adaptation I made as I built it was that I needed more room for my feet, so I moved the entire “sand box” up several inches. The only thing this changed was to shorten the length of the two 2×6 cross boards at the bottom (the one that says 9 7/8″ in the drawing).

Tagged with: , , ,
Posted in DIY, Woodworking

Bookmark with Dynamic Components via Keyboard Maestro (with Toggl Reports example)

Here’s an example of how you can use Keyboard Maestro to create bookmarks that use dynamic components. For this example I wanted to create a bookmark that would give me the correct URL to get a report of the last 365 days of Toggl time entries. Toggl lets you pass a start date and an end date in the URL. Using KM it’s easy to replace those values with calculated date values.

Click the screenshot at the right to see the macro, which I’ve also provided here: Toggl entries for the last 365 days.kmmacros. Read more ›

Tagged with: , , , ,
Posted in Tech Tips

Adding Entity Wrapper Class from Field Value using Display Suite

NOTICE: This works fine in Drupal 7. For Drupal 8 please see https://www.drupal.org/node/2863420

Sometimes as a developer you need to provide your Drupal authors with the ability to choose different variations of a particular content object. For example, imagine you have a Callout paragraph bundle and you want the author to choose between a Dark or a Light colorscheme; if the user chooses Light, the font colors appear dark over a light background. To achieve this you need a dark or light class on the entity wrapper based on whichever the author chose; this would let you write CSS to target each color scheme. Using Display Suite this is an easy problem to solve and doesn’t require any preprocess code or other hand-coded solution.

This solution requires Token and Display Suite. You will need to enable the ui modules from these, as well as ds_extras to give you the CSS class functionality. Read more ›

Tagged with: , , , ,
Posted in Tech Tips

Friendly URL for Views Contextual Filter on a Boolean Field

I have a story content type that has a corresponding Views listing page. The stories can be flagged as Featured through a checkbox/boolean field ( field_story_featured ). The stories also have a Story Type value ( field_story_type ). The views listing page needs to have the following links at the top:

Screenshot_2017-02-08_15-05-57

The URL structure for the Views page makes the most sense as follows (purple = story type contextual filter, orange = featured contextual filter): Read more ›

Tagged with: , ,
Posted in Development

Backing up a File Before Overwriting It (in Drupal 7)

I’m communicating with a 3rd party SOAP API to pull some data into a CSV file. The CSV file will be used by Feeds and/or Migrate so it’s important to have the file named exactly the same for each run. I want the generated CSV to always be named “mdb_interests.csv” but I want to “backup” the existing “mdb_interests.csv” to “mdb_interests_N.csv” if one already exists. This will ensure that we always have the latest data in “mdb_interests.csv” but will have backups from all previous API pulls. Using some built-in Drupal functions it’s pretty simple: Read more ›

Tagged with:
Posted in Tech Tips

Adding an Inline Style Attribute to an Entity in Drupal 7

The point of this post is to illustrate adding inline styles without creating template files. Normally we add classes via $vars['classes_array']  but sometimes inline styles are required.

This code isn’t too useful as written (make all text red for all entity types, really?), but drop in a conditional to affect specific entity types and replace the styles with something useful like setting a background image URL from a field within the entity and you have a handy snippet.

 

Tagged with:
Posted in Development

Entity Reference Referrer – Getting Entity Reference’s Parent/Referrer Entity ID and Type

Out-of-the-box, the Entity Reference module doesn’t provide a way to access the referrer entity’s fields from within the referenced entity. I have two ways of doing this. The first is to patch the entityreference module (not ideal unless this patch went out to the community via proper channels; perhaps I should get on that?). The second is to mimic entityreference’s “Rendered entity” field formatter but modify it to include the referrer information. I recommend the second method for now. Read more ›

Tagged with: , ,
Posted in Development