Problem/Motivation

Hi,
When a entity of recently read is inserted the field "status" is not set but entity_key for that field is defined.
This is a problem I had when I enabled the module DANSE and this module. DANSE tried to access this field and Drupal throw an error:

The website encountered an unexpected error. Please try again later.

Drupal\Core\Entity\EntityStorageException: Field status is unknown. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 810 of core\lib\Drupal\Core\Entity\Sql\SqlContentEntityStorage.php).
Drupal\Core\Entity\ContentEntityBase->get('status') (Line: 368)
Drupal\danse_content\Plugin\Danse\Content->isPublished(Object) (Line: 264)
Drupal\danse_content\Plugin\Danse\Content->topicsForEntity(Object) (Line: 128)
danse_content_entity_update(Object)
call_user_func_array('danse_content_entity_update', Array) (Line: 403)
Drupal\Core\Extension\ModuleHandler->invokeAll('entity_update', Array) (Line: 201)
Drupal\Core\Entity\EntityStorageBase->invokeHook('update', Object) (Line: 800)
Drupal\Core\Entity\ContentEntityStorageBase->invokeHook('update', Object) (Line: 530)
Drupal\Core\Entity\EntityStorageBase->doPostSave(Object, 1) (Line: 685)
Drupal\Core\Entity\ContentEntityStorageBase->doPostSave(Object, 1) (Line: 455)
Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 801)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object) (Line: 339)
Drupal\Core\Entity\EntityBase->save() (Line: 119)
Drupal\recently_read\RecentlyReadService->insertEntity(Object) (Line: 31)
recently_read_entity_view(Array, Object, Object, 'full')
call_user_func_array('recently_read_entity_view', Array) (Line: 403)
Drupal\Core\Extension\ModuleHandler->invokeAll('entity_view', Array) (Line: 289)
Drupal\Core\Entity\EntityViewBuilder->buildMultiple(Array) (Line: 239)
Drupal\Core\Entity\EntityViewBuilder->build(Array)
call_user_func_array(Array, Array) (Line: 101)
Drupal\Core\Render\Renderer->doTrustedCallback(Array, Array, 'Render #pre_render callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was %s. See https://www.drupal.org/node/2966725', 'exception', 'Drupal\Core\Render\Element\RenderCallbackInterface') (Line: 786)
Drupal\Core\Render\Renderer->doCallback('#pre_render', Array, Array) (Line: 377)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 201)
Drupal\Core\Render\Renderer->render(Array, ) (Line: 241)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 578)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 242)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 132)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object) (Line: 142)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 163)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 80)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 716)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

I tried to find a solution from the side of DANSE but they pointed that the issue is the undefined entity_key for the "status" that their module tries to access. https://www.drupal.org/project/danse/issues/3224940 is the issue I opened there.

Steps to reproduce

Install the module "Recently read".
Configure the module to track any content type (for example articles).
Install and enable the DANSE modules: Drupal Audit Notification Subscription Event, and DANSE content
View a node of the content type configured in the "Recently read" module
Reload the page if the error does not appear (this is because the "Recently read" module will create the entity in the first visit)

Proposed resolution

Definde the "status" field.

Remaining tasks

User interface changes

API changes

Data model changes

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

scastro1107 created an issue. See original summary.

dstojanovski made their first commit to this issue’s fork.

dstojanovski’s picture

Status: Active » Needs review

The "Status" field is defined and the module is tested with the DANCE module. It works fine.

lexsoft’s picture

StatusFileSize
new19.89 KB

Updating the module gives me a:
Entity/field definitions Mismatched entity and/or field definitions
The following changes were detected in the entity type and field definitions.
Recently read
The Status field needs to be installed.

recently read status field not installed

lexsoft’s picture

Status: Needs review » Needs work
vadym.tseiko’s picture

StatusFileSize
new106.3 KB

Hello guys, I have a similar problem here after the module update.

vadym.tseiko’s picture

StatusFileSize
new209 KB

I seek further and found that if try to create a view with a filter on some of the fields that are connected to the recently read module error appears that no such database table. This problem appears not after the update of the module but in compatibility with some other modules, in my case views module with relation to this module.

vadym.tseiko’s picture

Tried: drush updb -y, re-install the module, check updates, caches cleared, reconfigured view, as soon as I add a relation to recently_read I get this error. I think there needs to be added some hook_entity_update or base_field_definition.

admirlju’s picture

StatusFileSize
new768 bytes

There is a missing update hook, that adds the status field to the database table. Here's the patch file.

admirlju’s picture

Status: Needs work » Needs review

codebymikey made their first commit to this issue’s fork.

codebymikey’s picture

StatusFileSize
new2.22 KB

Added a patch updating #10 which avoids the following exception being thrown if there are already some Recently Read entities in the database:

Exception thrown while performing a schema update. SQLSTATE[01000]: Warning: 1265 Data truncated for column 'status' at row 1: ALTER TABLE "recently_read" CHANGE `status` `status` TINYINT NOT NULL;

It was missing the initial value, causing the default status value to be NULL during the initial database update. This update ensures the default value is always populated.

I also added some missing interface methods for the entity.

edit: NB: this part of the code:

+  $has_recently_read_status_field = $definition_update_manager->getFieldStorageDefinition('status', 'recently_read');
+  if ($has_recently_read_status_field) {
+    // The field already exists. Populate using the original field value.
+    $status_field->setInitialValueFromField('status', TRUE);
+  }

is somewhat redundant at the moment since the update manager doesn't attempt to do any content population if the 'status' field already exists.

DeaOm made their first commit to this issue’s fork.

deaom’s picture

It's important to note, that the issues occurs with older version where the status field was introduced (1.2 I think). So if you get the latest version, you should not have the issue (this is what was probably happening with the not managing to reproduce comments). The update hook runs with no errors, I added some CS correction to the added function in the interface and also in the service (to match the interface), so leaving this to needs review but in my POV can be merged. The failing tests already have their own issue #3382214: Drupal 10 Fix deprecations in tests, but if necessary the MR can be merged after the tests are passing, so test can be a blocker.

vadym.tseiko’s picture

Applied local patch made from interdiff #14 - worked fine for me. Thanks guys.

admirlju’s picture

Status: Needs review » Reviewed & tested by the community

From what I can see locally this works, so I'm setting the status to RTBC.

taran2l’s picture

StatusFileSize
new3 KB

Attaching a static patch from the latest changes in the MR (for composer patching purposes)