Drupal Planet

How to use once() in Drupal

Once is a Drupal library - available on npm - that ensures your JavaScript only runs once when working on any item.

I put a snippet on a Slack channel recently suggesting that someone should use once() to make sure their code didn't run multiple times. And then I gave them this snippet to show how to use it:

const editFrequencys = once('allEditFrequencies', '.edit-frequency', context);

editFrequencys.forEach(editFrequency => {
  editFrequency.on('change', function() {
    $.ajax({
      method: "POST",
      url: "/frequency-select",
      data: { name: "John", location: "Boston" },
      dataType: "json"
    })
      .done(function( data ) {
        console.log("It's done!");
        console.log(data);
      });
  });
});

Job well done, Mark, you might say. Yes, but then the author asked me:

What are the different arguments about in the first line?
I've seen this code in searching for an answer, but nothing ever explains what/why the arguments for the "once" are for.

And that reminded me of a younger me. Me about 6 months ago, when I first tried to use once(). So here is the explanation I gave them:

const editFrequencys creates a variable for what you want to work on. It’s a plural, because once always returns an array. You could do something like const [editFrequency] to just return one item if you were sure there was only ever going to be one of the thing you are looking for on the page. But I find it better to return the array just in case in the future a second instance of the thing is on the page.

once() is the function that will ensure the code that is acting on your “thing” will only act once.

'allEditFrequencies' is a name that we assign to this item. You can assign anything to it basically, such as 'justSomeStuff'. This is then attached to the property in your DOM via a data attribute, such as data-once="allEditFrequencies" so JS knows which once function this item belongs to.

context is just the method that is going to be used to find your array. You could use document here, but if the page gets updated via ajax, then document will not be updated, so in Drupal terms, context is probably better.

If I got something wrong there, or there's something I should update, let me know.

Join the "Something nice ..." newsletter

The full title is "Something nice, something quirky, something else".

I send an email once a week with something nice, something quirky, and something else that I think is interesting (all with a web development theme, of course).

Adding Advanced Layouts to a LocalGov Drupal Website

Adding Advanced Layouts to a LocalGov Drupal Website

There's a (hidden away) module in LocalGov Drupal that's a game-changer for layout possibilities.

The LocalGov Drupal Layouts module was created as part of the LGD Microsites platform, but contributed back to the core LGD project. However, it is not turned on by default (todo: propose it gets turned on by default), so I think a lot of people are not aware of it.

Here's a short demo of how to enable it and use it. Then you can extend it to add as many layout options as you want to your LGD layouts.

If you'd like to chat about how you might use this, connect with me on the LGD slack or via the Annertech contact form.

Join the "Something nice ..." newsletter

The full title is "Something nice, something quirky, something else".

I send an email once a week with something nice, something quirky, and something else that I think is interesting (all with a web development theme, of course).

Using Drupal's Webform Module to Create a Decision Tree

Using Drupal's Webform Module to Create a Decision Tree

Ever needed to create a "decision tree" or "Smart Answers" feature and didn't know where to start? It's pretty easy if you use Drupal's webform module and add conditional handlers for the confirmation settings (all through a few clicks in the webform UI).

Creating a decision tree is quite easy using the Drupal Webform UI. Here's the outline:

  1. Create the form
  2. Add all the questions that you want
  3. Use the "Conditions" tab in each question to set the conditions for what question shows where
  4. On the settings page for the form, under "Email/Handlers" add a handler for the confirmation page settings for each potential response in the decision tree.

Here's a video outlining the steps:

Join the "Something nice ..." newsletter

The full title is "Something nice, something quirky, something else".

I send an email once a week with something nice, something quirky, and something else that I think is interesting (all with a web development theme, of course).

Setting up Access Control with Drupal's Workbench Access Module

LocalGov Drupal Content Access Control

This is a short video for how to use the LocalGov Content Access Control Module, based on Drupal's Workbench Access module.

Many (if not all) councils have been asking for a way to do "access control", whereby only certain editors can edit certain parts of the website. I put together the LocalGov Drupal Content Access Control module so we'd have a defined way of doing this and all councils can have a similar approach. 

Here's a short video explaining how it works (which might help people outside of councils who also need to set this up). In short:

  1. Create a taxonomy called "Access Control".
  2. Add terms to this.
  3. Use this taxonomy in a "Workbench Access" scheme.
  4. Add editors and/or roles to the site sections.
  5. Add a field to content types that references the "Access Control" vocabulary.
  6. Enjoy!

Join the "Something nice ..." newsletter

The full title is "Something nice, something quirky, something else".

I send an email once a week with something nice, something quirky, and something else that I think is interesting (all with a web development theme, of course).

Proposed new Theme Settings for LocalGov Drupal unpublished Content

New theme Settings for Unpublished Content in a LocalGov Drupal website.

We added Drupal's default pink background to LocalGov Drupal's unpublished pages recently. It didn't go down as well as we might have hoped!

Drupal by default sets a pink background on unpublished nodes. We added this as a feature to LocalGov Drupal recently to distinguish between published and unpublished content. 

Editors were not very impressed with this because - especially on new websites - there's lots of whole sections that are unpublished. And when trying to preview a full section to stakeholders and nearly every page has a pink background, including all the items in entity reference fields and other listings, the site starts to not look like what was designed.

To get around this, I've proposed 2 site settings that can be enabled via the theme settings page if your theme uses LocalGov Base as its base theme.

  1. Turn off the pink backgrounds (handy if you just want to turn them off temporarily while demoing your content and then back on again once the demo is over)
  2. Add the word "Draft" to the title of any node that is unpublished (handy if the pink background is turned off more permanently).

Here's a short video to show it all working:

The code for this can been seen in this pull request on GitHub.

Join the "Something nice ..." newsletter

The full title is "Something nice, something quirky, something else".

I send an email once a week with something nice, something quirky, and something else that I think is interesting (all with a web development theme, of course).

Create a LocalGov Drupal Microsite Theme

Changing the design of subsites in Localgov Drupal

A short video demo showing how I quickly created 4 LocalGov Drupal Microsites themes.

When you design a site using the UI for the LocalGov Drupal Microsites platform, all the customisations are set as CSS variables attached to the body element, like so:

<body style="--color-accent: #2F72B1; --color-accent-contrast: #ffffff; --color-accent-2: #cce6ff; --color-accent-2-contrast: #20517e; --page-background-color: #fcfcfc; --spacing: 1.5rem; --color-text: #2e2e2e; --color-link: #2F72B1; --font-secondary: 'Inter'; --heading-font-weight: 400; --font-primary: 'Inter'; --heading-2-font-weight:400; --heading-2-line-height: 1.2; --header-background-color: #2F72B1; --header-text-color: #ffffff; --header-link-color: #ffffff; --header-link-hover-color: #cce6ff; --footer-grid-column-justification: space-between;">

You can then grab those variables, and set them in your microsite theme's variables.css file. 

body {
 --color-accent: #262626;
 --color-accent-contrast: #ededed;
 --color-accent-2: #262626;
 --color-accent-2-contrast: #ddc983;
 --page-background-color: #ededed;
 --color-text: #262626;
 --color-link: #003ecc;
 --font-secondary: "Public sans";
}

Now, you can use that theme across all your microsites and/or override it on a per site basis - making it a perfect starting point for you site(s).

I created 4 base themes based on the demo sites we created:

 

Join the "Something nice ..." newsletter

The full title is "Something nice, something quirky, something else".

I send an email once a week with something nice, something quirky, and something else that I think is interesting (all with a web development theme, of course).

Live Preview of Changing the Design of your LocalGov Drupal Microsite

Live Preview of Changing the Design of your LocalGov Drupal Microsite

Update your microsite and see the changes in real time without having to publish them.

Following on from yesterday's post about a Very Basic Example of Editing Drupal Nodes in a Modal, here's a more expanded version to show how we can edit the design of a LocalGov Drupal microsite in a settings tray dialog and see the edits live without having to publish them.

The link is a bit more complex than yesterday's, as we have to set the renderer and some options.

<a href="/group/${micrositeId}/edit?destination=${currentNodePath}"
   class="use-ajax"
   data-dialog-type="dialog"
   data-dialog-renderer="off_canvas"
   data-dialog-options="{&quot;width&quot;:800}">
   Live Preview
</a>`;

I should probably change the hardcoded width and set it to a percentage-based width instead.

Once we have the link working, then we can work on the fields to create a correspondence between the field being edited and the values being updated in a live preview. This turned out to be a little tricky in the microsites context, since the fields being edited often update CSS variables rather than text. For that reason I only worked on three fields and will spend some time later thinking of a more generic solution. The code for this is available on GitHub.

Here's a short video of it in action.

Join the "Something nice ..." newsletter

The full title is "Something nice, something quirky, something else".

I send an email once a week with something nice, something quirky, and something else that I think is interesting (all with a web development theme, of course).

Very Basic Example of Editing Drupal Nodes in a Modal

Editing Drupal nodes in a modal
Editing nodes in a modal? Yes, so you can preview the content before you hit save!

I was playing with adding use-ajax class to a Drupal listing this evening, so I could open a node and edit it in a modal.

The reason I was doing this was because I was thinking about how we could preview making changes to microsites (colours, fonts, etc) in the LocalGov Drupal Microsites distribution. I think this approach is quite a nice and simple first attempt at an approach for that. 

The code to get the edit form in a modal was as simple as this:

<a href="/node/{{ node.id }}/edit" class="use-ajax" data-dialog-type="modal">Edit</a>

The JavaScript was very messy, so I won't post it. The general outline was: when the modal is open, if the value in the ckeditor changes, then update the corresponding field with the new value.

Here's a video of me editing some nodes; pretend instead of text, I'm changing the values for colours and fonts and layout, etc in a microsite.

 

Join the "Something nice ..." newsletter

The full title is "Something nice, something quirky, something else".

I send an email once a week with something nice, something quirky, and something else that I think is interesting (all with a web development theme, of course).

WTF is SDC in Drupal Core?

Unboxxing SDC Drupal core module

Single-Directory Components (SDC) is the biggest change to Drupal theming since the introduction of Twig. Here's an "unboxxing" walkthrough of how it works.

Join the "Something nice ..." newsletter

The full title is "Something nice, something quirky, something else".

I send an email once a week with something nice, something quirky, and something else that I think is interesting (all with a web development theme, of course).

Creating different styles for subsites within a Localgov Drupal website

Changing the design of subsites in Localgov Drupal

We've had lots of requests for changing the design of subsites in LGD. Here's how I'd do it.

Create a subsite, select the theme you want for it and ... hey-presto: you now have a different style for your subsite than the style of the rest of the site.

Join the "Something nice ..." newsletter

The full title is "Something nice, something quirky, something else".

I send an email once a week with something nice, something quirky, and something else that I think is interesting (all with a web development theme, of course).