What's been on my mind

Custom validation on a views field plugin options form

Submitted by charles on Tue, 11/14/2023 - 12:47

When validating an options form for a views field plugin class that extends FieldPluginBase, you need to add a `validateOptionsForm()` function and use an array for getting the value although when setting the error you don't need to include 'options':

 * {@inheritdoc}
public function validateOptionsForm(&$form, FormStateInterface $form_state): void {

Get the full message from your Guzzle Response exception object

Submitted by charles on Thu, 09/28/2023 - 10:40

I was trying to debug a Guzzle response error but the error message was fairly long and some crucial information about which specific API fields were unauthorized was missing. The Guzzle Client Exception object has some handy methods to allow reading the full message contents:


The drush "entity:save" command in Drupal

Submitted by charles on Sun, 06/11/2023 - 15:26

In Drupal, there's a drush entity:save (alias esav) command which is useful when you want to save entities in bulk.

I needed to use this command the other day to save some entities that update a field value on the entity during a presave hook. Instead of running a database update to change the data, I just triggered the update by saving the entities, e.g

drush esav your_entity_type_id 12167,12168,12169,12170,12171,12172

To save all entities of a type, you can use:

Use Twig's default filter in place of the ternary operator

Submitted by charles on Tue, 03/09/2021 - 16:29

Instead of using the ternary operator to set a value or a default if that value doesn't exist, Twig's "default" filter provides a more compact and readable way of doing so. For example:

{% set alignment = alignment ? 'pagination--' ~ alignment : 'pagination--center' %}


{% set alignment = 'pagination--' ~ alignment|default('center') %}

Selectively disabling Bootstrap tooltips for form field elements

Submitted by charles on Sun, 04/05/2020 - 13:45

On a Drupal 8, Bootstrap based site that I’m currently working on we have a couple of forms that have a boolean ‘Acknowledgement’ field that is displayed at the bottom.

Because we’ve enabled “Smart form descriptions (via Tooltips)” in the theme settings, the form field descriptions are not visible, but in this case we wanted to display it below the field, instead of as a tooltip.