Check text format for each item

Created on 19 March 2024, 3 months ago

Problem/Motivation

In the function ckeditor5_highlight_preprocess_field for each item, editor is loaded

function ckeditor5_highlight_preprocess_field(array &$variables): void {
  $items = $variables['element']['#items'];
  if (!$items instanceof FieldItemListInterface) {
    return;
  }

  foreach ($items as $item) {
    if (!$item instanceof TextItemBase) {
      continue;
    }

    $editor = \Drupal::entityTypeManager()
      ->getStorage('editor')
      ->load($item->format);

but, TextItemBase doesn't ensure $item has format property defined (it's not required):

abstract class TextItemBase extends FieldItemBase {

  /**
   * {@inheritdoc}
   */
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $properties['value'] = DataDefinition::create('string')
      ->setLabel(t('Text'))
      ->setRequired(TRUE);

    $properties['format'] = DataDefinition::create('filter_format')
      ->setLabel(t('Text format'));
  }
}

This can produce following error:

The website encountered an unexpected error. Please try again later.
AssertionError: Cannot load the "editor" entity with NULL ID. in assert() (line 295 of core/lib/Drupal/Core/Entity/EntityStorageBase.php).
assert(, 'Cannot load the "editor" entity with NULL ID.') (Line: 295)
Drupal\Core\Entity\EntityStorageBase->load(NULL) (Line: 29)
ckeditor5_highlight_preprocess_field(Array, 'field', Array)
call_user_func_array('ckeditor5_highlight_preprocess_field', Array) (Line: 287)

Proposed resolution

We just need to add extra check before entity storage load,

    if (empty($item->format)) {
      continue;
    }
πŸ› Bug report
Status

Active

Version

1.1

Component

Code

Created by

πŸ‡·πŸ‡ΈSerbia sakiland

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

Merge Requests

Comments & Activities

Production build 0.69.0 2024