0

Good morning,

I'm running some tests with hook_cron in Drupal 8. My goal is to unpublish nodes of a specific content type ('event'), and once I enabled my module, I was able to accomplish this. However, after setting $node->setPublished to 'true,' I was unable to accomplish publishing said nodes again after running cron, running Drush CR, and restarting Apache.

Anyone know of any good hooks to resolve this?

function scheduler_publish_cron() {
  $nids = \Drupal::entityQuery("node")
    ->condition('type', 'event')
    ->execute();
$storage_handler = \Drupal::entityTypeManager()->getStorage("node");
$nodes = $storage_handler->loadMultiple($nids);
foreach ($nodes as $node) {
// Unpublish nodes
// $node->setPublished(false);
 // Publish nodes
$node->setPublished(true);
    $node->save();
  }
}
1

Try to disable access checking in the second run, because after unpublishing the node you might no longer have access to them (depending on the user account the code runs with):

$nids = \Drupal::entityQuery('node')
  ->condition('type', 'event')
  ->accessCheck(FALSE)
  ->execute();
| improve this answer | |
  • Thanks, I think that did the trick. – yogurtexpress Mar 11 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.