• Development

    Drupal Migrate API skip_on_multiple Process Plugin

    Here’s a Migrate API process plugin I wrote in Drupal 9 that skips a property or row when more than one value exists. My use case:

    1. My source data has locations; each location has multiple associated organizations
    2. My new Drupal site has locations; each location has a parent organization
    3. I want to only populate the field_parent_org field (an entity reference) if there is a single organization value in the source data for the location
    4. I’m using a custom module called “pdms_migrate”

    I’ve stripped out all of the noise from the examples below… hopefully it’s enough to help you understand the example:

  • Development

    Setting a LIMIT on an UPDATE query in Drupal 8 / 9

    Here’s a quick tip regarding Drupal database manipulation. Specifically, I needed a way to flip a boolean value (from 1 to 0) on the first N rows in a database table that matched a specific set of conditions.

    The Problem

    It seems you cannot enforce a LIMIT on an UPDATE query using a static query in Drupal.

    Here’s what I was trying to do:

    This throws an error. I believe the issue is caused by the variable substitution wrapping the limit value in quotes. The error message starts with:

    If you run this query manually in MySQL, it works fine with LIMIT 10 but doesn’t work with LIMIT '10' (it throws the same error shown above).

    The Solution

    Given dynamic queries are favored over static queries, and that this static query doesn’t work, I ended up getting to the finish line with two dynamic queries.

    I have tested the solution on 10,000 records and it took < 6 seconds to process the entire request on my local dev lando site.

    “code” is a unique identifier column in the database table.

  • Development

    Missing Titles on Drupal 8 ECK Content Listings

    UPDATE: I discovered the solution below was only working when the Title base field was enabled for the entity type. If you enable the title field on the entity type it works, but then the title is required to save a new record. I will look for an alternative solution to avoid using the Title field altogether. Also, I’ll look into https://www.drupal.org/project/eck/issues/2785297.


    I’ve recently run into an issue with an ECK-created custom entity type. There was no reason for me to use the “Title” field on this particular entity type. Unfortunately, when you don’t have a title field several of the built-in entity listings and displays seem broken. Here’s what the Content List screen looks like, for example:

  • Development

    Conditionally Triggering Salesforce Push Operations in Drupal 8

    The user interface for the Salesforce Suite in Drupal 8 is fantastic. The suite itself is incredibly powerful. Among the many options, for each mapping you create you can specify what operations should trigger a synchronization. Here’s an example:

    We recently faced an issue with the “Drupal entity update” trigger being called too often. The salesforce_push module implements hook_entity_update() , which gets called a lot. After looking at how the functions are called in salesforce_push.module I realized there were two choices:

  • Development

    Patch for Drupal 8.7.14 SA-CORE-2020-004, SA-CORE-2020-005, and SA-CORE-2020-006

    I have a site that’s temporarily stuck on 8.7.14; it’s not worth the risk to update to 8.8.8 right now.

    I was able to diff 8.8.7 and 8.8.8 to figure out what changes were made for these security announcements:

    I could then compare the changes with 8.7.14 to create the patch below, which applies against 8.7.14 without any issues.

    DISCLAIMER: The formal recommendation is to update to 8.8.8 if at all possible. Use the patch below at your own risk.

     

  • Development

    Storing First and Last Name with Social API for Drupal 8

    I recently worked on a project that relied on Social API for Google, Facebook, and Yahoo authentication. Out of the box everything worked great. Our site stores the user’s first name and last name in two fields on the user entity: field_first_name  and field_last_name. There are a few ways to tap into the Social API authentication process (which comes from the Social Auth component).

    Originally we leveraged the SocialAuthEvents::USER_CREATED event in an event subscriber. We set the first name and last name, then re-saved the user. This, however, executes after the user is created. The issue we had was that we also have Salesforce integration and both name fields are required in Salesforce. When the user is first saved it was pushing to Salesforce without the name fields, which triggered an error.

    The solution was to implement a different event handler: SocialAuthEvents::USER_FIELDS

    This lets you manipulate the user fields before the user is saved. I poked through the Social Auth code and figured out how to get the first and last name values. Here’s the working solution:

    web/modules/custom/mymodule/mymodule.services.yml

    web/modules/custom/mymodule/src/EventSubscriber/MymoduleSocialAuthSubscriber.php

    After creating/updating the files above, just clear the caches and test.

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