Allow Mapped Fields to be Required (e.g. for title field)

Created on 12 March 2024, 10 months ago
Updated 11 June 2024, 7 months ago

Problem/Motivation

Right now, we have mapped the Drupal core's title field to a text field:

e.g. ["drupal/simple-text", {"placeholder": "Enter page title*","tag":"h1", "mappingFields": [{"field": "title"}]}]

Overall, it works great.

Except when someone tries saving a node with this field being empty.

Then we get the error:

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] => 4050 [:db_insert_placeholder_1] => 20319 [:db_insert_placeholder_2] => events [:db_insert_placeholder_3] => en [:db_insert_placeholder_4] => 1 [:db_insert_placeholder_5] => 877 [:db_insert_placeholder_6] => [:db_insert_placeholder_7] => 1710192355 [:db_insert_placeholder_8] => 1710269271 [:db_insert_placeholder_9] => 1 [:db_insert_placeholder_10] => 0 [:db_insert_placeholder_11] => 1 [:db_insert_placeholder_12] => 1 ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 815 of /home/chamber/public_html/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

Steps to reproduce

  1. Enable Gutenberg
  2. Set the node template to {["drupal/simple-text", {"placeholder": "Enter page title*","tag":"h1", "mappingFields": [{"field": "title"}]}]}
  3. Create a node and try saving (without a title)
  4. See the error

Proposed resolution

Allow mapped fields to be required (and prevent saving of the node unless they are filled).

Remaining tasks

TBD.

User interface changes

Enable a new key called "required" in the node templates:

{[ "drupal/simple-text", {
    "placeholder": "Enter page title*",
     "tag":"h1",
     "mappingFields": [{"field": "title"}],
     "required": true
}]}

API changes

N/A, as far as I know.

Data model changes

N/A, as far as I know.

πŸ› Bug report
Status

Fixed

Version

3.0

Component

Code

Created by

πŸ‡¨πŸ‡¦Canada Nathan Tsai

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

Merge Requests

Comments & Activities

  • Issue created by @Nathan Tsai
  • πŸ‡¨πŸ‡¦Canada Nathan Tsai

    Note: I set this category to "Bug Report" because I assume that it should be impossible to configure Gutenberg to lead to a 500 error.

  • πŸ‡΅πŸ‡ΉPortugal marcofernandes

    Actually kind of waiting for this 🌱 Make pre-save entity validation more commonplace (e.g., the default) Active
    But I guess we can do some validation when "pushing" the block value into the node entity (on the existing entity_presave hook).
    I don't think it would be necessary to have a "required" property to the template since we can get that from the field that is mapped.
    It would be nice to have some kind of visual indication that the block is required and I think we can do that by getting the info from the field.

  • First commit to issue fork.
  • Merge request !146Issue #3427432 - validate required mapped fields β†’ (Merged) created by loze
  • Status changed to Needs review 7 months ago
  • πŸ‡ΊπŸ‡ΈUnited States loze Los Angeles
  • πŸ‡ΊπŸ‡ΈUnited States loze Los Angeles

    This also does not add any sort of required indicator. I needed something quick that prevented people from trying to save nodes with no titles.

  • Pipeline finished with Success
    7 months ago
    Total: 251s
    #181984
  • Pipeline finished with Success
    7 months ago
    Total: 245s
    #182209
  • Pipeline finished with Success
    7 months ago
    Total: 369s
    #182282
  • πŸ‡΅πŸ‡ΉPortugal marcofernandes

    Not easy to fit a visual indicator for the required mapping fields. And I mean that in a styling perspective. I tried the usual (*), a red outline border and even a different background color for the block but I didn't like the results.

    I made some changes so the mapping fields filter is ready to handle the required fields but, for now, I just added at the sidebar a text telling that the field is required πŸ€·β€β™‚οΈ

    We'll revisit this with some designers/UX experts opinions πŸ˜‰

    For now, I'm happy with the MR. Good work and thank you @loze.

  • Pipeline finished with Success
    7 months ago
    Total: 285s
    #182848
  • Pipeline finished with Success
    7 months ago
    Total: 288s
    #182851
  • Pipeline finished with Success
    7 months ago
    Total: 238s
    #183897
  • Status changed to Fixed 7 months ago
  • Pipeline finished with Success
    7 months ago
    Total: 1214s
    #185251
  • Pipeline finished with Failed
    7 months ago
    Total: 1750s
    #185274
  • Pipeline finished with Canceled
    7 months ago
    Total: 748s
    #185536
  • Pipeline finished with Failed
    7 months ago
    Total: 1079s
    #185547
  • Pipeline finished with Failed
    7 months ago
    Total: 1033s
    #185813
  • Pipeline finished with Success
    7 months ago
    Total: 1174s
    #185838
  • Automatically closed - issue fixed for 2 weeks with no activity.

  • Pipeline finished with Success
    6 months ago
    Total: 311s
    #200267
  • Pipeline finished with Canceled
    6 months ago
    #205538
  • Pipeline finished with Success
    6 months ago
    Total: 337s
    #205541
  • Pipeline finished with Success
    6 months ago
    Total: 305s
    #205545
  • Pipeline finished with Failed
    6 months ago
    Total: 197s
    #217104
  • Pipeline finished with Failed
    6 months ago
    #217586
  • Pipeline finished with Success
    6 months ago
    Total: 152s
    #217588
  • Pipeline finished with Success
    6 months ago
    Total: 154s
    #217596
  • Pipeline finished with Success
    6 months ago
    #217654
  • Pipeline finished with Success
    6 months ago
    Total: 152s
    #217700
  • Pipeline finished with Failed
    5 months ago
    Total: 162s
    #229435
  • Pipeline finished with Success
    5 months ago
    Total: 209s
    #229440
  • Pipeline finished with Success
    5 months ago
    Total: 170s
    #229821
  • Pipeline finished with Success
    5 months ago
    Total: 227s
    #229873
  • Pipeline finished with Success
    5 months ago
    #245877
  • Pipeline finished with Skipped
    3 months ago
    #285744
  • Pipeline finished with Success
    3 months ago
    Total: 135s
    #287262
  • Pipeline finished with Success
    3 months ago
    Total: 142s
    #287263
  • Pipeline finished with Success
    3 months ago
    Total: 316s
    #303858
  • Pipeline finished with Success
    3 months ago
    Total: 266s
    #303868
  • Pipeline finished with Success
    about 1 month ago
    Total: 256s
    #347063
  • Pipeline finished with Success
    24 days ago
    Total: 142s
    #356734
  • Pipeline finished with Success
    24 days ago
    Total: 145s
    #356747
  • Pipeline finished with Success
    24 days ago
    Total: 149s
    #356754
  • Pipeline finished with Success
    24 days ago
    Total: 145s
    #356758
Production build 0.71.5 2024