Problem/Motivation

Steps to reproduce:

  • Install module
  • Create new site settings entity type
  • Move the Authored by field to the deactivated section in the form display configuration
  • Create new instance of the entity with the new entity type, save --> error

Error:

Auf der Website ist ein unvorhergesehener Fehler aufgetreten. Bitte versuchen Sie es später nochmal.</br></br><em class="placeholder">Drupal\Core\Entity\EntityStorageException</em>: SQLSTATE[23000]: Integrity constraint violation: 1048 Column &#039;user_id&#039; cannot be null: INSERT INTO {site_setting_entity_field_data} (id, type, langcode, user_id, name, fieldset, status, created, changed, default_langcode) 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); Array
(
    [:db_insert_placeholder_0] =&gt; 4
    [:db_insert_placeholder_1] =&gt; footer_bottom_text
    [:db_insert_placeholder_2] =&gt; de
    [:db_insert_placeholder_3] =&gt; 
    [:db_insert_placeholder_4] =&gt; Footer bottom text
    [:db_insert_placeholder_5] =&gt; Footer
    [:db_insert_placeholder_6] =&gt; 1
    [:db_insert_placeholder_7] =&gt; 1562843674
    [:db_insert_placeholder_8] =&gt; 1562843674
    [:db_insert_placeholder_9] =&gt; 1
)
 in <em class="placeholder">Drupal\Core\Entity\Sql\SqlContentEntityStorage-&gt;save()</em> (line <em class="placeholder">847</em> of <em class="placeholder">core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php</em>). <pre class="backtrace">Drupal\Core\Database\Statement-&gt;execute(Array, Array) (Line: 631)
Drupal\Core\Database\Connection-&gt;query(&#039;INSERT INTO {site_setting_entity_field_data} (id, type, langcode, user_id, name, fieldset, status, created, changed, default_langcode) 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)&#039;, Array, Array) (Line: 358)
Drupal\Core\Database\Driver\mysql\Connection-&gt;query(&#039;INSERT INTO {site_setting_entity_field_data} (id, type, langcode, user_id, name, fieldset, status, created, changed, default_langcode) 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)&#039;, Array, Array) (Line: 32)
Drupal\Core\Database\Driver\mysql\Insert-&gt;execute() (Line: 1052)
Drupal\Core\Entity\Sql\SqlContentEntityStorage-&gt;saveToSharedTables(Object) (Line: 990)
Drupal\Core\Entity\Sql\SqlContentEntityStorage-&gt;doSaveFieldItems(Object) (Line: 657)
Drupal\Core\Entity\ContentEntityStorageBase-&gt;doSave(NULL, Object) (Line: 449)
Drupal\Core\Entity\EntityStorageBase-&gt;save(Object) (Line: 838)
Drupal\Core\Entity\Sql\SqlContentEntityStorage-&gt;save(Object) (Line: 394)
Drupal\Core\Entity\EntityBase-&gt;save() (Line: 79)
Drupal\site_settings\Form\SiteSettingEntityForm-&gt;save(Array, Object)
call_user_func_array(Array, Array) (Line: 111)
Drupal\Core\Form\FormSubmitter-&gt;executeSubmitHandlers(Array, Object) (Line: 51)
Drupal\Core\Form\FormSubmitter-&gt;doSubmitForm(Array, Object) (Line: 590)
Drupal\Core\Form\FormBuilder-&gt;processForm(&#039;site_setting_entity_footer_bottom_text_form&#039;, Array, Object) (Line: 319)
Drupal\Core\Form\FormBuilder-&gt;buildForm(&#039;site_setting_entity_footer_bottom_text_form&#039;, Object) (Line: 61)
Drupal\Core\Entity\EntityFormBuilder-&gt;getForm(Object) (Line: 99)
Drupal\site_settings\Controller\SiteSettingEntityAddController-&gt;addForm(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber-&gt;Drupal\Core\EventSubscriber\{closure}() (Line: 582)
Drupal\Core\Render\Renderer-&gt;executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber-&gt;wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber-&gt;Drupal\Core\EventSubscriber\{closure}() (Line: 151)
Symfony\Component\HttpKernel\HttpKernel-&gt;handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel-&gt;handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session-&gt;handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle-&gt;handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache-&gt;pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache-&gt;handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware-&gt;handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware-&gt;handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel-&gt;handle(Object, 1, 1) (Line: 693)
Drupal\Core\DrupalKernel-&gt;handle(Object) (Line: 19)
</pre>

Some screenshots:

screen
screen

Proposed resolution

Support moving authored by field to deactivated section. Maybe just get the current user, and use it as the author, instead of using the form value, if it's always beeing hidden anyway.

Remaining tasks

Fix bug

User interface changes

None.

API changes

tbd.

Data model changes

None.

Comments

orlando.thoeny created an issue. See original summary.

orlando.thoeny’s picture

Title: Fatal error on save when "Authored by" field is disabled in form mode » Fatal error on save when "Authored by" field is deactivated in form mode
anybody’s picture

Yep, I can confirm this.

scott_euser’s picture

Status: Active » Needs review
StatusFileSize
new960 bytes

Thanks for discovering this and providing thorough details. Attached patch.

scott_euser’s picture

Title: Fatal error on save when "Authored by" field is deactivated in form mode » Fatal error on save when "Authored by" or "Fieldset" fields are deactivated in form mode

Affects fieldsets as well, updating title.

anybody’s picture

Thank you @scott_euser, is this a custom "trick" or did you have a look into core? I think nodes and other entities have to do a similar trick, because they also contain fields which may be hidden, but values like "created by", "created at" etc. are set correctly. I think we should do the same?

Setting the user id to "0" may have negative side-effects.

scott_euser’s picture

Status: Needs review » Needs work

Happy if someone is willing to investigate and propose a solution.

nagy.balint’s picture

I did some research, it seems we can play with the default value in the baseFieldDefinitions, and that we can remove the extra entity save.

With the attached patch it will work, but on setting create it will get the "None" and 0 default value, and that will only be overriden on the first edit.

However we can define default value callbacks See trait EntityOwnerTrait

So the user id can be set similarly to "getDefaultEntityOwner() {"

And the same could be done for the fieldset i suppose by loading the entity type settings.

So that way even if the user_id or the fieldset is empty, it would call those functions to set the proper value, and then it will be right even on the create.

nagy.balint’s picture

Status: Needs work » Needs review
StatusFileSize
new2.86 KB

So something like this.

Status: Needs review » Needs work

The last submitted patch, 9: site_settings-fatal-error-on-save-fields-deactivated-3067257-9.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

nagy.balint’s picture

Status: Needs work » Needs review
StatusFileSize
new4.06 KB

I think the test fails because now without that extra save the system will actually display "Created the" and not "Saved the".

scott_euser’s picture

Status: Needs review » Reviewed & tested by the community

Nice, this is way better than my original patch. Learned something new with the default value callbacks - thanks!

scott_euser’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

tlthades’s picture

Thanks, it works for me