You are here

function scheduler_api_test_scheduler_allow_publishing in Scheduler 8

Implements hook_scheduler_allow_publishing().

File

tests/modules/scheduler_api_test/scheduler_api_test.module, line 71
Hook implementations of the Scheduler API Test module.

Code

function scheduler_api_test_scheduler_allow_publishing(NodeInterface $node) {

  // If there is no 'Approved for Publishing' field then allow publishing.
  if (!isset($node->field_approved_publishing)) {
    $allowed = TRUE;
  }
  else {

    // Only publish nodes that have 'Approved for Publishing' set.
    $allowed = $node->field_approved_publishing->value;

    // If publication is denied then inform the user why.
    if (!$allowed) {
      \Drupal::messenger()
        ->addMessage(t('%title is scheduled for publishing, but will not be published until approved.', [
        '%title' => $node->title->value,
      ]), 'status', FALSE);

      // If the time is in the past it means that the action has been prevented.
      // Write a dblog message to show this. Give a link to view the node but
      // cater for no nid as the node may be new and not yet saved.
      if ($node->publish_on->value <= \Drupal::time()
        ->getRequestTime()) {
        \Drupal::logger('scheduler_api_test')
          ->warning('Publishing of "%title" is prevented until approved.', [
          '%title' => $node->title->value,
          'link' => $node
            ->id() ? $node
            ->toLink(t('View node'))
            ->toString() : '',
        ]);
      }
    }
  }
  return $allowed;
}