• Development

    Display-only Pseudo Fields in Drupal 8

    In the past I’ve been happy to rely on Display Suite to create what I call “Frontend Only” (DSField) fields (which I created via php). These fields appeared in the Manage Displays screens so that site admins can easily drag them around in the display as needed. As it turns out, you can achieve nearly the same result using a few built-in hooks in Drupal 8. The Display Suite DSField plugin has a few handy helpers like the ability to only show the field on specific display mode forms (e.g., show the field on Full content but not on Teaser). You can still control whether or not the display-only field renders if you use the core hooks, so we’ll be okay.

    [UPDATE January 30, 2020] After some discussions with a colleague I decided the world needs a Plugin-based approach to the solution below. We talked through how it’d work before going about our day. After writing the info file, .module file, and prepping the folder structure I realized I never checked to see if this already existed.

    I was pleasantly surprised to see that Sutharsan had already created a module that fits the bill! Extra Field is nearly identical in structure and implementation as what I’d planned to build. I just finished writing my first ExtraField\Display plugin and it works beautifully! I will leave my post below in tact, but I highly recommend stopping here and simply using Extra Field. Note that the README.txt file is worth reading, and there is an extra_field_example sub-module that explains things quite well.

  • Uncategorized

    Twig Caching Issues in Acquia Cloud Enterprise

    I’ve recently run into an issue where my site (on Acquia Cloud Enterprise) has node displays that were flip-flopping between an older version of a Twig template file, and the most recent version. I tried all combinations of drush cr, varnish cache clearing, and clearing the cache through the Drupal UI.

    After reading through No reliable method exists for clearing the Twig cache, I landed on this page at Acquia: https://support.acquia.com/hc/en-us/articles/360005167754-Drupal-8-Twig-cache

    I SSH’d into the production server I always SSH into, and I ran the command as shown. I did this repeatedly. I did a drush cr after. I did a drush cr before. Nothing was working. My pages were showing up with the old template, or the new template, and it seemed to be at random.

    Ultimately I re-read the documentation page, more thoroughly this time, and discovered this: “connect to each web server instance and run a command like this…

    Ahah! EACH web server.

    So, I logged into the Acquia Cloud interface, found the other production server’s connection string (myuser@someserver.prod.hosting.acquia.com), connected, and ran the same command. After another drush cr all of the pages were using the new template.

    That’ll teach me to jump straight to executing commands without reading the instructions carefully.

  • Development

    Migrating Into Existing Nodes in Drupal 8, Including Rollback Missing from Source

    Please read this entire post (including the disclaimer at the bottom) before you put any of it to use.

    The site I’m basing this off had an existing set of nodes and a new migration that had the same nodes (and many more) in the data source. I wanted to map the existing nodes to their migration-based counterparts and treat every node as if it originated from the migration.

    As of today, using a few patches makes it easy (thanks contributors!) to rollback items that no longer exist in a D8 migration’s source data. See Migrate support for deleting items no longer in the incoming data and Implement rollback of items no longer in source data

    With these patches you can import and rollback in two commands:

    What if the data that you’re migrating (and rolling back if removed) already existed in Drupal before you started using the migration? If you attempt to migrate content, then rollback removed items, you will find that the items that no longer exist in the source will not be deleted if they existed before the migration.

    First, I should describe how I’m pulling data into existing nodes.

    This is as simple as populating the node ID in the process section of your migration YML, like this:

  • Development

    Combining Steps in Behat for Drupal

    Lately I have found myself repeating several lines of behat steps over and over again. Here is a sample of the behat code I use to choose a specific checkbox from an entity browser popup:

    There are a few steps in here that are custom, but explaining them would be beyond the scope of this post; just assume they do what they describe (switching focus to an iframe, and clicking a checkbox).

    The issue I was having is that every time I wanted to do that one action (pick an image from an entity browser) I was repeating all 8 lines of code.

    I began looking for ways of making this set of steps repeatable. After some failed attempts at extending MinkContext and MinkAwareContext I came across scenario hooks via this StackOverflow post. I discovered the @BeforeScenario, which is executed before every scenario in each feature (where it’s used). Using this hook to access contexts from other contexts was documented on behat.org but required a few tweaks for the Drupal implementation. Here is what I ended up doing (based on the default FeatureContext.php you get when you install behat for the project:

  • Development

    Adding Level Number Class to Menu Items in Drupal 8

    This is a quick post showing how to add level classes to menu items in Drupal 8.

    Here’s the result, showing the additional menu--level-N  and menu-item--level-N  classes:

    Result

    Step 1: Create a New Twig template File

    Determine which Twig template you need to override. I recommend reading the official docs on this. In my case, for a menu called infofor, I copied docroot/core/themes/classy/templates/navigation/menu.html.twig to docroot/themes/custom/mytheme/templates/navigation/menu--infofor.html.twig .

    Step 2: Update the Twig Code

  • 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

    Access Entity Properties in a Field Twig Template

    This doesn’t need much of an explanation. You can use any entity methods as far as I can tell.

    Here’s an example showing how to get the node ID in a field template:

     

  • 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.