Score:0

Preprocessor not returning news based on the correct created date

cn flag

I am returning news to my homepage in a carousel. I am using the below query to return the latest 9 articles based on the "created" date. The client however wants to back date some articles (so admin > content > edit node > authored by and change the created date). This however does not seem to work as keeps returning the articles in the actual date order they were created, even if the date was changed manually:

$query = \Drupal::entityQuery('node');
$query->accessCheck(FALSE)
    ->condition('type', 'news')
    ->condition('langcode', \Drupal::languageManager()->getCurrentLanguage()->getId(),"IN")
    ->sort('created', 'DESC')
    ->range(0, 9);

$nids = $query->execute();
$entities = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($nids);

What am I doing wrong? It seems to work fine within the actual news page view. The view itself has the sort set as "Content: Authored on (desc)" and this returns the data as expected, but the preprocessor query does not.

EDIT:

The current recommendation is to change the sort to "changed", but this unfortunately does not work. I have a feeling the issue is related to translations. The following is the 1st result in the array. I deliberately went ahead and changed the "authored" date to 2002 to ensure it goes last, yet, it's still first. If you look at the values however, I think this is where the issue happens:

[created] => Array
 (
    [da] => 1647950253
    [x-default] => 1016357523
    [es] => 1647950332
    [fi] => 1647950277
    [fr] => 1647949861
    [nb] => 1647950235
    [nl] => 1647950348
    [sv] => 1647950211
    [en-au] => 1647949797
    [en-gb] => 1647947671
    [en-us] => 1647947639
    [bel-fr] => 1647950381
    [bel-nl] => 1647950361
 )

Based on this result, it may be that the sort looks for the 1st "created" date, being DA, but in our case the language is the x-default

Can I change the sort by to ensure it sorts by the actual language?

Kevin avatar
in flag
The property type might be 'changed'. Side note, removing access check and no check for published status could surface things you don't want. Another way to do this would be to use a dedicated date field instead of a node property.
mauzilla avatar
cn flag
@Kevin sorry I don't know if I understand regarding the property type that may have changed?
cn flag
Kevin probably means you might need to use the 'changed' field on the node (last updated time) instead of 'created', not that a property has changed. But if it's working correctly in views I'd suspect a cache issue as your entity query looks fine
leymannx avatar
ne flag
`->sort('changed', 'DESC')`
mauzilla avatar
cn flag
I have updated the question, I think i'm getting closer but not sure how to ammend the sort?
mauzilla avatar
cn flag
I was able to track down the issue. As not all of the translations authored dates was updated, it seems that the created date used is the "newest" translation.
apaderno avatar
us flag
Where exactly are you using that code? The title speaks of *preprocessor*, but I am not clear if that means a preprocess hook or something else.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.