Drupal
Using filter_var for boolean environment variables
Someone on my team added the following setting to check whether an environment variable was true or false:$settings['x_menu_admin_enable'] = strtolower(getenv('X_MENU_ADMIN_ENABLE')) === 'true' ? getenv('X_MENU_ADMIN_ENABLE') : FALSE;
I find that using PHPs filter_var makes this code a bit easier to parse and only calls getenv once:
Custom validation on a views field plugin options form
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 {
Drush 12 removes the "--no-post-updates" option from the drush updb command
Drush 12 no longer uses the "--no-post-updates" option in the drush updb command. This means that you won't be able to run database updates from drush without triggering the post update hooks.
The drush "entity:save" command in Drupal
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,12172To save all entities of a type, you can use:
Detect whether a form (or any request) is loading in a modal in Drupal 9
You can check the request to detect whether you're in a Drupal modal:
$request->query->get('_wrapper_format') === 'drupal_modal';
I needed to use this when embedding a block plugin within a modal form, and wanted to display the block slightly differently than it normally gets displayed.
Use Twig's default filter in place of the ternary operator
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' %}
Becomes:
{% set alignment = 'pagination--' ~ alignment|default('center') %}
Selectively disabling Bootstrap tooltips for form field elements
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.
How to add the entity type id to the body tag classes in Drupal 8
This snippet adds the entity type id to the html body tag classes when on the canonical entity page.
Using Bootstrap 4 spacing utilities in Bootstrap 3
I've been working on a Drupal 8 Bootstrap 3 site and was wanting to use the spacing utilities that come with Bootstrap 4.
These utilities provide a range of shorthand responsive padding and margin classes: