• Development

    Hiding Country from an Address Field’s Output in Drupal 8

    There are two field formatters available on an Address field in Drupal 8:

    The Plain formatter uses a Twig template file but it would take some work to override with the proper markup. The Default formatter doesn’t use a Twig template file so you cannot simply override via Twig.

    If your goal is to simply hide the country value from the output you can use hook_preprocess_field to alter the country value. As it turns out, unsetting the country field doesn’t help, but setting the value to an empty string does work.

    Here’s an example (in a module; you can put in your .theme file if you’d prefer the setting to be at the theme level).

     

  • Development

    Modifying Rows During a Drupal 8 CSV Migration

    Migrate Source CSV is currently the source plugin of choice for doing a CSV-to-Drupal migration with the Migrate API in Drupal 8. In this post I will demonstrate how to manipulate the CSV data in realtime during the migrate:import operation. You can think of this as the equivalent to prepareRow() that you have seen elsewhere, like my blog post Extending the Migrate Plus JSON Parser in Drupal 8.

    Please make sure you have a working migration before you begin; it’ll make things easier to troubleshoot if you know you had a good starting point.

  • Development

    404 Error Serving a /libraries page in Drupal 8

    It took some time to figure out why I kept seeing a 404 error (page not found) on http://www.mysite.com/libraries. The issue seemed to only present itself on our Acquia environments. Ultimately I realized that there was a conflict due to having a “libraries” directory in the document root of the site (e.g., /docroot/libraries).

    It was a quick fix after realizing the cause of the problem; I added this to my .htaccess file, above the other index.php rewrite rules.

    The reason this wasn’t happening on my local machine is because I’m using nginx locally for this particular website; the .htaccess file isn’t used.

  • Development

    Joining Strings in a Drupal Views Field using Twig

    I have two optional fields on a Drupal 8 content type: City and State. Both are rendered as simple text.

    In a fields-based View I wanted to show the field output as “Portland” or “Portland, OR”, or “OR”.

    First, I added the two fields, State then City (the order is important). Next I excluded the State field from display.

    Finally, I opened the Rewrite Results pane of the City field and checked the Override the output of this field with custom text checkbox.

    After several attempts at using Twig’s joinreplacespaceless, and more, I landed on this simple solution as the rewrite text:

    Note the whitespace modifiers (hyphens). These are the key to getting  Portland, OR  instead of   Portland , OR .

    Also, we cannot just use  {{field_member_city}}, {{field_member_state}} because we could end up with  , OR if City is empty.

  • Development

    Using GROUP_CONCAT to Combine Rows in a Drupal Query

    Recently I was working on a D7 to D8 migration. I was trying to import news items and their taxonomy terms (among many other things). To make things simple I wanted the query results to have (for each node) a single field that contained a comma-separated list of taxonomy terms which I could then explode during the row processing. Using GROUP_CONCAT we can achieve this! Let’s break it down:

    The Drupal 7 site has the following structure (focusing on the important bits for this blog post):

    The migration relies on the d7_node migrate source plugin, which basically queries for nodes of a specific type. The query object looks like this (simplified for this blog post):

  • Development

    Adding Fields to Inline Entity Form Table

    Inline Entity Form is a useful module for reference entities and being able to edit them in place.

    Here’s what the edit form looks like out of the box for an unlimited value entity reference:

    Default output

    Often it’s helpful to provide additional information to your editors.

    If you have a look at inline_entity_form.module you will find a function called theme_inline_entity_form_entity_table(). Within this you will see how this table is built, and how you can manipulate the form to add additional columns of information.

  • Development

    Basic HTTP Authentication in Drupal Site Using settings.php

    Here’s a quick and painless way of preventing public access to a Drupal site using settings.php (or settings.local.php).

    I’ve been using this for development and staging sites that I want to keep private.

    If you want this to be available to all settings*.php files you should put this near the top of your settings.php file:

    Then, you can leverage it wherever you’d like. For example, on an Acquia site I might add this to the bottom of settings.php:

    For non-Acquia sites I’d call the function at the bottom of settings.local.php.

  • Development

    Valet+ Quickstart for Drupal Development

    Here’s a README.md file that I’ve developed over time. It explains how I setup and use Valet+ for quick and powerful Drupal development.

    Sorry for the formatting. I’ll get markdown support on my blog sometime…

     

  • Development

    Drupal 8 User Photo Update Form

    Recently I had to come up with a simple way for users to change their member profile photo without requiring them to visit the user edit screen. Here’s the result:

    Member Photo Output

    First, I added a new Image field called “Member Photo” to the user account fields (machine name field_user_picture). Here are the settings I used:

    • Allowed extensions: png, gif, jpg, jpeg
    • File directory: users/[date:custom:Y]-[date:custom:m]
    • Max size: 10 MB
    • Checked: Enable alt field
    • Checked: Alt field required
  • Development

    Creating a Drupal 8 Route to a User Page with Dynamic User Object

    It took me some time to figure out the right combination of properties to make this work.

    My goal was to create a form that lives at /user/UID/photo (think /user/1/edit).

    I wanted the user object to be passed into the form as an argument.

    Here’s the mymodule.routing.yml file:

    Here’s the src/Form/ProfilePhoto.php file: