• Development

    Overriding “Required Message” on Composite Fields in Webform (Drupal 8)

    The more commonly used fields in Webform (text, radios, etc.) offer the ability to set a custom Required message string. This is the text that shows in the error message when a required field is not filled.

    Here’s what that screen looks like when you’re configuring a field on the Build screen:

    Required message

    There are several element types that do not provide a UI field to set this Required message value. The composite Name and Advanced Address field are two examples I encountered. For these fields you have to dig a little deeper, but it’s easy once you get the hang of it. Visit the Advanced tab for the field, then scroll to the bottom where you’ll see a Custom Settings fieldset.

  • Development

    Using Drupal 8 Persistent Login Module on Platform.sh

    The Persistent Login module relies an extra cookie to maintain a Drupal user’s session. The configuration screen for this module lets you specify a prefix for the cookie. The default is “PL”. If you are using the site on HTTPS, the module prepends an “S” to the cookie it creates. Here’s an example cookie name: SPLfa0650d6d985433d455a3b15cc70fd9b .
    Platform.sh lets you configure cookie cache settings via routes.yaml .
    If you’re using Persistent Login you must tell the system not to ignore this cookie.
    Here’s a standard (and simple) routes.yaml  file on a Drupal 8 site:
    Here’s a modified version which allows Persistent Login to work correctly:

     

    There is a bit more discussion around how these cookies are handled here: https://www.drupal.org/node/2898065/discuss

  • Development

    Overriding a Webform Confirmation Message in Drupal 8

    There are a few reasons you may need to override a webform confirmation:

    • Out of the box webform doesn’t allow you to add some tags (e.g., <button>) to a webform confirmation message. When you save it strips them out.
    • What if you needed to alter the webform message based on the values submitted in the webform?
    • What if you needed to alter the webform message based on where the user saw the webform?
    • etc…

    Thankfully, you can use a standard preprocess hook to override the confirmation output.

    In this particular example I needed to override the output of the message to include a button tag. I needed this to only happen if specific GET params had specific values.

    First, I modified the confirmation message settings as shown here:

  • Development

    Using Lazy Builders and Auto Placeholders in Drupal 8

    Introduction

    I’ve been working on a site that features a lot of user-specific customization and output. The site offers workshops (courses) using the Opigno LMS for Drupal 8. The workshops are rendered in a few different ways throughout the site. For the most part, the rendered workshops appear the same for all users. Here’s an example of a “card” view mode for a workshop:

    If a user has successfully completed a workshop, a special badge will appear on the card view mode. Also, the card will be highlighted:

    There are two parts of the workshop card template that have to change based on the user viewing the card:

  • Development

    NightwatchJS Command to Repeat a Keystroke

    Here’s a simple custom command that lets you repeat a specific key (single or combo) N number of times.

    In the Drupal world you want to place this in your “Commands” directory (as defined in DRUPAL_NIGHTWATCH_SEARCH_DIRECTORY in web/core/.env).

    You’d want to change “sitename” to be your site’s name (so it feels namespaced).

    /tests/Nightwatch/Commands/sitenameRepeatKeystroke.js

     

    Using this in /tests/Nightwatch/Tests/misc.js with a 1000ms pause between each keystroke:

  • Development

    Faster Debugging with Lando and xdebug

    I’ve been struggling with the speed (or lack thereof) of Lando for Drupal sites. I have suspected xdebug was part of the issue. Sure enough, disabling xdebug speeds up the lando instance considerably. Re-enabling it slows it right back down again. I use xdebug often, so having to lando rebuild  to turn xdebug on and off is not an option. I wondered if there’s a way to leave the extension enabled but only use it when needed.

    While researching config directives like xdebug.remote_enable  and xdebug.remote_autostart, I came across this issue thread on the github Lando project: Allow faster xdebug on/off toggling

    The title sounds promising, right?

  • Development

    Quickly Protecting a Few Nodes from Deletion in Drupal 8

    I’m working on a Drupal 8 site with some critical pages for which we do not want to allow deletion. The homepage, for example, is a basic page; we do not want to allow anyone (even UID #1) to delete this page. If there comes a time where UID #1 decides they need to, they’ll have to update this simple code to support that. We need not make it any more complicated than that for this particular project.

    Here’s a solution that:

    • shows nothing but an error on the “Delete” form for specific nodes (works for all users)
    • prevents deletion by any means (works for all users except UID #1)
      • why except UID #1? because hook_node_access doesn’t run for this user
  • Development

    Testing Tokens with Entity Ref Traversal via Drush

    I am working on a project that has some complicated depth. I needed a way to quickly test token replacement. It’s pretty simple when you understand a few things.

    Example Architecture

    Here’s the Drupal architecture for this example:

    • Discussion Group (content type)
      • Title
      • Intro
      • Body
      • “Associated Experts” paragraph field (allows one “Associated Experts” paragraph item)
    • Associated Experts (paragraph type, used by many content types like Discussion Group above)
      • Heading
      • Intro
      • Experts (allows unlimited number of “Expert” nodes)
    • Expert (content type)
      • Title
      • Photo
  • 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