You are here

function hook_scheduler_allow_unpublishing in Scheduler 8

Same name and namespace in other branches
  1. 7 scheduler.api.php \hook_scheduler_allow_unpublishing()

Hook function to deny or allow a node to be unpublished.

This hook gives modules the ability to prevent unpblication of a node at the scheduled time. The node may be scheduled, and an attempt to unpublish it will be made during the first cron run after the unpublishing time. If this hook returns FALSE the node will not be unpublished. Attempts at unpublishing will continue on each subsequent cron run until this hook returns TRUE.

Parameters

\Drupal\node\NodeInterface $node: The scheduled node that is about to be unpublished.

Return value

bool TRUE if the node can be unpublished, FALSE if it should not be unpublished.

1 function implements hook_scheduler_allow_unpublishing()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

scheduler_api_test_scheduler_allow_unpublishing in tests/modules/scheduler_api_test/scheduler_api_test.module
Implements hook_scheduler_allow_unpublishing().

File

./scheduler.api.php, line 103
API documentation for the Scheduler module.

Code

function hook_scheduler_allow_unpublishing(NodeInterface $node) {
  $allowed = TRUE;

  // Prevent unpublication of competition entries if not all prizes have been
  // claimed.
  if ($node
    ->getType() == 'competition' && ($items = $node->field_competition_prizes
    ->getValue())) {
    $allowed = (bool) count($items);

    // If unpublication is denied then inform the user why. This message will be
    // displayed during node edit and save.
    if (!$allowed) {
      \Drupal::messenger()
        ->addMessage(t('The competition will only be unpublished after all prizes have been claimed by the winners.'));
    }
  }
  return $allowed;
}