• Development

    Rendering a Drupal 8 Link with Anchor and Destination

    I’m working on a site that contains several complicated views (many exposed filters). The views render entities using a couple different view modes. These view modes use Display Suite. Within these view modes I’ve leveraged DS custom fields for several needs.
    I needed to render a node “Edit” link within some of these view modes. The user would click the edit link, edit the node, then click “Save”. The system would then bring the user to the exact position of the page they were on before, filters and settings intact.
    This type of solution would work without Display Suite (leveraging other methods). You’re getting a DS-based example because that’s what the site called for.
    In my particular case I needed to separate the anchor from the edit link. If you’re okay having them in the same place you could skip Step 1 and just include the anchor within the markup for the edit link.
  • Development

    Getting a Field from an Entity in Drupal 8

     

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

    Before I show the code, let me give some background.

  • Development

    Tideways and Xhgui using Lando

    While I prefer using Valet+ for my Drupal development, I have been asked a few times to share my Lando + Tideways setup. I can’t go into too many details at this point, because it’s been a while since I’ve used this, but here’s my setup. I’ve included some documentation within some of the files (especially at the bottom of .lando.yml).

    You can see some explanation and screenshots of Tideways and Xhgui in my Tideways and Xhgui using Dev Desktop post if you need a bit of an introduction. Also, you may be interested in seeing how I got Tideways running with Valet+.

    This example is for Drupal 7. You can rework it easily to work with Drupal 8. The .conf may not be required in either case.

  • Development

    Setting Drupal 8 Node Properties with Behat

    The title of this post could also have been “Opening and Closing a Details Element with Behat” or “Clicking Any Element with Behat”.

    The Drupal 8 node add/edit screen has a number properties on the right side of the screen. I wanted to use Behat to click the “Provide a menu link” checkbox. On page load this MENU SETTINGS pane is closed like the others.

    Here’s an example:

    Node add/edit

    The “URL SETTINGS”, “MENU SETTINGS”, actually mask a <details>  element.

    The “MENU SETTINGS” markup looks like this:

    Menu settings markup

    I expected to be able to write a few lines of Behat like this:

    Unfortunately this results in the failure: element not interactable.

    So, I thought I may need to first click to open the MENU SETTINGS pane so that I can see the checkbox before I try to click it. I modified my code to look like this:

    Unfortunately this results in the failure: Link with id|title|alt|text “Menu settings” not found.

    I tried using the edit-menu  id, “MENU SETTINGS” in all caps, etc. but it always resulted in the same “Link with …”

    I realized the click step is only looking for links.

    Solution

    I added a new step definition to my features/bootstrap/FeatureContext.php file:

    I updated my Behat code and found success!
  • 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):