Programmatically created nodes do not respect TOC config

Created on 7 February 2019, almost 6 years ago
Updated 16 August 2024, 4 months ago

Problem/Motivation

When the node is created programmatically (or as part of a migration), the TOC settings are not respected, and TOC shows up if there is text on the node that matches the selectors.

This happens because the basefield has TRUE as default value, and all settings are being defined at the form level:

/**
 * Implements hook_entity_base_field_info().
 */
function toc_js_per_node_entity_base_field_info(EntityTypeInterface $entity_type) {
  $fields = [];

  if ($entity_type->id() === 'node') {
    $fields['toc_js_active'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Toc active'))
      ->setName('toc_js_active')
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE)
      ->setDescription(t('Stores whether the Toc is displayed or not.'))
      ->setDefaultValue(TRUE);
  }

  return $fields;
}

Proposed resolution

- Step 1: Set the default basefield value to FALSE. This will at least have a less intrusive behavior on all nodes created by code (migrated nodes, etc).
- Step 2: Implement logic on node pre_save that checks what the configuration defines for the default override values for that node type, and set that on the basefield before really saving the node. This way regardless of the method used to save the node, the defaults configured would be respected.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Needs work

Version

1.0

Component

Code

Created by

🇪🇸Spain marcoscano Barcelona, Spain

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024