Hiding the node title makes node form submission crash

Created on 30 October 2023, about 1 year ago

Problem/Motivation

To reproduce:

1. Go to form display management for a node type
2. Hide the title field
3. Try to save a node of this type

Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null: INSERT INTO "node_field_data" ("nid", "vid", "type", "langcode", "status", "uid", "title", "created", "changed", "promote", "sticky", "default_langcode", "revision_translation_affected") VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9, :db_insert_placeholder_10, :db_insert_placeholder_11, :db_insert_placeholder_12); Array ( [:db_insert_placeholder_0] => 2 [:db_insert_placeholder_1] => 2 [:db_insert_placeholder_2] => page [:db_insert_placeholder_3] => en [:db_insert_placeholder_4] => 1 [:db_insert_placeholder_5] => 1 [:db_insert_placeholder_6] => [:db_insert_placeholder_7] => 1698684363 [:db_insert_placeholder_8] => 1698684366 [:db_insert_placeholder_9] => 0 [:db_insert_placeholder_10] => 0 [:db_insert_placeholder_11] => 1 [:db_insert_placeholder_12] => 1 ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 817 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component
Field  β†’

Last updated about 5 hours ago

Created by

πŸ‡¬πŸ‡§United Kingdom joachim

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

Comments & Activities

  • Issue created by @joachim
  • πŸ‡¬πŸ‡§United Kingdom joachim

    What I don't understand is how the same procedue on the Custom Block entity type does NOT crash, when both fields look very similar in their definition:

        $fields['title'] = BaseFieldDefinition::create('string')
          ->setLabel(t('Title'))
          ->setRequired(TRUE)
          ->setTranslatable(TRUE)
          ->setRevisionable(TRUE)
          ->setSetting('max_length', 255)
          ->setDisplayOptions('view', [
            'label' => 'hidden',
            'type' => 'string',
            'weight' => -5,
          ])
          ->setDisplayOptions('form', [
            'type' => 'string_textfield',
            'weight' => -5,
          ])
          ->setDisplayConfigurable('form', TRUE);
    
        $fields['info'] = BaseFieldDefinition::create('string')
          ->setLabel(t('Block description'))
          ->setDescription(t('A brief description of your block.'))
          ->setRevisionable(TRUE)
          ->setTranslatable(TRUE)
          ->setRequired(TRUE)
          ->setDisplayOptions('form', [
            'type' => 'string_textfield',
            'weight' => -5,
          ])
          ->setDisplayConfigurable('form', TRUE)
          ->addConstraint('UniqueField', []);
    

    What is different?

    The DB field in {node_field_data} does not have ALLOW NULL, but for {block_content_field_data} it does.

  • Understood. But with ✨ Do not require a 'title' field Needs work and its duplicates over the years, this seems the same issue.

  • πŸ‡ΊπŸ‡ΈUnited States TolstoyDotCom L.A.

    It seems to me that everything should have a title, even in the case of a "Person" object. The title could be random, or constructed from something else, but you should always be able to count on an existing entity having a title of some kind just as you can count on it having an ID.

    Also, I don't think handling this at the form level would be a good idea since someone might hide the title and then have custom code or a contrib module that provides the title. Preventing them from hiding the title would cause problems for them. If you let them do it but instead show a warning that would annoy people who know what they're doing and confuse others.

    I suggest changing SqlContentEntityStorage::mapToStorageRecord to throw an exception if a field is required but empty. It already throws an exception in one case throw new EntityStorageException("Table mapping contains invalid field $field_name."); so adding a check in the same area doesn't seem out of place. You're going to get an exception in any case, it might as well give the user a clue what the problem is.

  • Hi,
    Already the issue is noticed and we have patch in the ticket https://www.drupal.org/project/drupal/issues/2358537 ✨ Do not require a 'title' field Needs work . Rerolled the patch also for the latest version in that ticket.

    So instead duplicate we can close this ticket.

  • πŸ‡·πŸ‡΄Romania amateescu

    The DB field in {node_field_data} does not have ALLOW NULL, but for {block_content_field_data} it does.

    That's because the node entity schema explicitly marks the title field as NOT NULL in \Drupal\node\NodeStorageSchema::getSharedTableFieldSchema().

    Closing as a duplicate of ✨ Do not require a 'title' field Needs work .

Production build 0.71.5 2024