You are here

function hook_scheduler_unpublishing_allowed in Scheduler 2.x

Hook function to deny unpublishing of an entity.

This hook gives modules the ability to prevent unpublication of an entity. The entity may be scheduled, and an attempt to unpublish it will be made during the first cron run after the unpublishing time. If any implementation of this hook function returns FALSE the entity will not be unpublished. Attempts to unpublish will continue on each subsequent cron run, and the entity will be unpublished when no hook prevents it.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The scheduled entity that is about to be unpublished.

Return value

bool|null FALSE if the entity should not be unpublished. TRUE or NULL will not affect the outcome.

1 function implements hook_scheduler_unpublishing_allowed()

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_unpublishing_allowed in tests/modules/scheduler_api_test/scheduler_api_test.module
Implements hook_scheduler_unpublishing_allowed().

File

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

Code

function hook_scheduler_unpublishing_allowed(EntityInterface $entity) {
  $allowed = TRUE;

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

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