You are here

function hook_scheduler_allow_publishing in Scheduler 8

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

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

This hook gives modules the ability to prevent publication of a node at the scheduled time. The node may be scheduled, and an attempt to publish it will be made during the first cron run after the publishing time. If this hook returns FALSE the node will not be published. Attempts at publishing 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 published.

Return value

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

1 function implements hook_scheduler_allow_publishing()

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

File

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

Code

function hook_scheduler_allow_publishing(NodeInterface $node) {

  // If there is no 'approved' field do nothing to change the result.
  if (!isset($node->field_approved)) {
    $allowed = TRUE;
  }
  else {

    // Prevent publication of nodes that do not have the 'Approved for
    // publication by the CEO' checkbox ticked.
    $allowed = !empty($node->field_approved->value);

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