Field value should be empty for entities that were never published

Created on 8 July 2019, over 5 years ago
Updated 16 May 2023, over 1 year ago

Currently when an entity has never been published, it has a publication date set to Tuesday, January 19, 2038 at 03:14:07 UTC, which is the value of the PUBLICATION_DATE_DEFAULT constant. According to the documentation the reason for this is to make sure that unpublished nodes appear at the top of lists when sorting by newest date:

 /**
  * Define the value stored in the database when a node is unpublished and no
  * publication date has been set. We use the largest number that the database
  * field can hold so unpublished nodes will appear newer than published nodes
  * when sorted by publication date.
  *
  * @note: This is going to trigger the Year 2038 problem.
  */
define('PUBLICATION_DATE_DEFAULT', 2147483647);

I think this is not a good approach, and the sorting of unpublished content to the top is a niche use case that can be solved in other ways which do not warrant to violate basic expectations of the data model.

Because of this approach the following features are not working as expected:

  • Checking if the field is empty using FieldItemInterface::isEmpty(). If I would call $node->get('published_at')->isEmpty() for a node that has never been published, I should get TRUE, but instead I now get FALSE because there is a fake value present.
  • We can not use conditions in entity queries in the normal way. If I want to get recently published content I cannot do $query->condition('publish_at', strtotime('1 week ago'), '>')->execute() since this will unexpectedly include unpublished content.
  • When a never before published node is rendered it is expected that no publication date is shown. This doesn't work, instead the date Tuesday, January 19, 2038 is shown. This is shown both in the frontend, and in administration interfaces.
  • Sorting of nodes by publication date is broken. Unpublished items are shown always at the top, pushing all recently published content away. If the user orders by "publication date descending" it means that they want to see the most recently published nodes at the top, not several pages of random unpublished nodes. In large projects it is not uncommon to have thousands of unpublished nodes in the database.
🐛 Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

🇧🇬Bulgaria pfrenssen Sofia

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇩🇪Germany osopolar 🇩🇪 GER 🌐

    What about the update hook? See also #13 and #21

    I guess it would be fine to set the published_at to NULL where it currently is 2147483647:

    update node_field_revision set published_at=NULL where published_at = 2147483647;
    update node_field_data set published_at=NULL where published_at = 2147483647;
Production build 0.71.5 2024