You are here

function hook_scheduler_publishing_allowed in Scheduler 2.x

Hook function to deny publishing of an entity.

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

Parameters

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

Return value

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

1 function implements hook_scheduler_publishing_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_publishing_allowed in tests/modules/scheduler_api_test/scheduler_api_test.module
Implements hook_scheduler_publishing_allowed().

File

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

Code

function hook_scheduler_publishing_allowed(EntityInterface $entity) {

  // Do some logic here ...
  $allowed = !empty($entity->field_approved->value);

  // If publication is denied then inform the user why. This message will be
  // displayed during entity edit and save.
  if (!$allowed) {
    \Drupal::messenger()
      ->addMessage(t('The content will only be published after approval.'), 'status', FALSE);

    // If the time is in the past it means that the action has been prevented,
    // so write a dblog message to show this.
    if ($entity->publish_on->value <= \Drupal::time()
      ->getRequestTime()) {
      \Drupal::logger('scheduler_api_test')
        ->warning('Publishing of "%title" is prevented until approved.', [
        '%title' => $entity
          ->label(),
        'link' => $entity
          ->id() ? $entity
          ->toLink(t('View'))
          ->toString() : '',
      ]);
    }
  }
  return $allowed;
}