0

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?

7
  • 2
    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.
    – Kevin
    Commented Mar 23, 2022 at 13:28
  • @Kevin sorry I don't know if I understand regarding the property type that may have changed?
    – mauzilla
    Commented Mar 23, 2022 at 13:39
  • 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
    – Clive
    Commented Mar 23, 2022 at 14:05
  • ->sort('changed', 'DESC')
    – leymannx
    Commented Mar 23, 2022 at 14:06
  • I have updated the question, I think i'm getting closer but not sure how to ammend the sort?
    – mauzilla
    Commented Mar 24, 2022 at 9:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.