• Development

    Setting a Specific External Port for MySQL in Lando

    Lando sets an external MySQL port dynamically by default. This means every time you (re)start a lando app it gets (potentially) a different external MySQL port. This is an annoyance as you have to change your port in TablePlus, SequelPro, or whatever MySQL GUI you’re using.

    There is a simple fix: use a local lando file to override the forwarded MySQL port. These steps assume you already have a working .lando.yml file.

    Step 1: Create a local lando file: .lando.local.yml

    Step 2: Run lando rebuild if you’ve already started the site in the past

    Step 3: Verify the settings by running lando info

    Step 4: Add .lando.local.yml to your .gitignore  file

    This site will always get the port you specified if it’s available when you start the app. You can use this port in your MySQL gui, scripts, etc.

  • 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

    Adding Custom Fields to the Field Edit Screen in Drupal 8 / 9

    Introduction

    I’m building an API with Drupal 8/9. There is additional information I want to track against each field instance in each content type (or custom entity type) in the system. This information includes where the field’s data comes from, who is responsible for maintaining it, etc. This is useful for the site administrator and developers to track where things are coming from. Drupal’s Third Party Settings functionality makes this easy.

    Result

    This is what you see when you edit the First name field:

    When you click Save settings the data is saved to this specific field’s instance configuration (meaning if you reuse this field you can fill the info out differently per instance).

    Solution

    There are two ways to achieve this. In both cases, we use hook_form_FORM_ID_alter() to introduce the new fields. I am doing this within a custom module called pdms. The code lives in pdms.module.

    Method #1:

    With this method, the system automatically handles storing the value into the field configuration’s third party settings.

    This happens because we use the  third_party_settings  key.

    The issue I had with this approach is that I could not get it to work if I put the fields in a fieldset (as shown in the screenshot).

    I think it’d be a matter of getting $entity->getThirdPartySetting()  to look into the fieldset somehow. If you know how to do this, please let me know!

    Method #2:

    This method gives us more control over what happens when the form is submitted.

    Because we have more control we’re able to traverse the fieldset easily.

    You can see, too, how I unset the third party setting if the value is empty. I am not sure if I’ll keep this in place; we’ll see if it causes any issues when I attempt to build a report showing all of the fields and their info.

     

  • Uncategorized

    Actionable Task Naming – Reduce Confusion, Increase Speed

    I’ve worked on many teams across many task management systems. One practice that has consistently brought clarity to our projects is actionable task naming. That is, naming tasks in a way that makes them assignable and actionable. This will reduce the number of false assumptions made, and the amount of time it takes people to interpret a task, every time they encounter the task!

    Doing this is easy, too.

    Instructions:

    When you’re writing a task, say these words first (in your head): “I need you to…” or “I need them to…” or “John needs to” — you get the gist. It’s that simple (most of the time).

    Example 1:

    Instead of Hero image on the homepage is too large you could say Make homepage hero image smaller or Homepage: make hero image smaller or similar.

    Note that I often remove the word “the” (Make the homepage hero image smaller).

    Example 2:

    This time I’ll illustrate why this practice is useful:

    Address field is optional  — does that mean it is currently optional but needs to be required? Or does it mean that it’s presently required but needs to be optional? I don’t want to have to ask myself that question each time I pass by this task. It’d be much easier to understand the intent/issue if it was written Make address field optional or Make address field required or similar.


    On a Related Note (for Developers)

    You can use a similar practice when writing version control commit messages.

    Instead of I need you to... we use the prefix This will....

    The result is consistent messages that say what will happen when a commit is applied/reverted.

    Example:

    Update jQuery UI version to 3.5.1
  • Development

    Increasing Memory for Specific Paths in Drupal 8

    Most of the examples I see for Drupal 8 are for single-path memory limit increases.

    The examples for Drupal 7 often follow the pattern below.

    Here’s a D8 version that supports multiple paths.

    Place this in your settings.php (or site/environment-specific settings.php), then update the paths accordingly.

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