Entity label must be at leat 55 long to support placeholder

Created on 19 November 2024, 2 days ago

Problem/Motivation

Since https://www.drupal.org/project/auto_entitylabel/issues/3076302 🐛 Set Label runs two times on node creation Needs review ,

      $placeholder = strtr('%AutoEntityLabel: @entityId%', ['@entityId' => $entity->uuid()]);

has been introduced.

https://git.drupalcode.org/project/auto_entitylabel/-/commit/131ce06e628...

In case, when you have custom entity with label limited to less than 55 characters, saving such entity will cause WSOD, because label field (in our case "name") in {your_entity}_field_data and {your_entity}_field_reference table are shorter than 55 charactes, and placeholder can not be temporary saved (Example placeholder value "%AutoEntityLabel: 9935f91e-aca2-44b4-9f03-c205449efcaf% is 55 characters long)

Example:


    $fields['name'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Name'))
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE)
      ->setSettings([
        'max_length' => 50,
        'text_processing' => 0,
      ])
      ->setDefaultValue('')
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'string',
        'weight' => -4,
      ])
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
        'weight' => -4,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE)
      ->setRequired(TRUE);

If you update the lenght of name field table like below, everything start working just fine, but maybe it would be nice to show some error in interface or in the reports, that given entity is not eligable for auto entity label.

function {your_module}_update_10001() {
  $schema = \Drupal::service('database')->schema();

  // Define the tables and field.
  $tables = [
    '{your_entity}_field_data',
    '{your_entity}_field_data_revision',
  ];
  $field = 'name';

  foreach ($tables as $table) {
    // Check if the table and field exist.
    if ($schema->fieldExists($table, $field)) {
      $schema->changeField($table, $field, $field, [
        'type' => 'varchar',
        'length' => 55, // New length.
        'not null' => FALSE,
      ]);
      \Drupal::logger('your_module')->info("Field length updated successfully in $table.");
    }
  }
}

Steps to reproduce

Proposed resolution

N/A

Remaining tasks

N/A

User interface changes

N/A

API changes

N/A

Data model changes

N/A

🐛 Bug report
Status

Needs work

Version

3.3

Component

Code

Created by

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

Comments & Activities

Production build 0.71.5 2024