Luke Leber Programmer/Analyst III

Most content editors will probably say that custom block titles are not something that are very useful most of the time.  For historical reasons these titles are toggled on by default for all new custom blocks that are added to a layout.

There is an ongoing issue regarding reversing the default value for this checkbox, but there is an easy way to accomplish this without waiting for the issue to be resolved or having to resort to a core patch.

TL;DR Version

/**
 * Implements hook_form_BASE_FORM_ID_alter() for layout_builder_add_block.
 *
 * @TODO: Keep an eye on https://www.drupal.org/project/drupal/issues/3074435
 */
function my_module_form_layout_builder_add_block_alter(&$form, FormStateInterface $form_state) {
  if (isset($form['settings']['label_display'])) {
    // Uncheck the 'display title' checkbox by default.
    $form['settings']['label_display']['#default_value'] = FALSE;
  }
}

What does the hook do?

This is a very simple form alter hook that's specifically targeting the layout_builder_add_block form. First, it does a sanity check (for paranoid folks such as myself) to ensure that the expected data structure is being passed in. As far as I know, data structures are not guaranteed to be backwards compatible, so it's always a good idea to future-proof things like this when possible. If the sanity check passes, the #default_value is simply set to FALSE so that the checkbox will be unchecked.

There's also a useful @TODO comment in there that can be checked periodically to see if the hook implementation is still required.  Eventually, the community may reach a consensus on how to approach the issue and this new default will ship with Drupal Core.

Tags