You are here

function scheduler_api_test_scheduler_allow_unpublishing in Scheduler 8

Implements hook_scheduler_allow_unpublishing().

File

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

Code

function scheduler_api_test_scheduler_allow_unpublishing(NodeInterface $node) {

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

    // Only unpublish nodes that have 'Approved for Unpublishing' set.
    $allowed = $node->field_approved_unpublishing->value;

    // If unpublication is denied then inform the user why.
    if (!$allowed) {
      \Drupal::messenger()
        ->addMessage(t('%title is scheduled for unpublishing, but will not be unpublished 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->unpublish_on->value <= \Drupal::time()
        ->getRequestTime()) {
        \Drupal::logger('scheduler_api_test')
          ->warning('Unpublishing of "%title" is prevented until approved.', [
          '%title' => $node->title->value,
          'link' => $node
            ->id() ? $node
            ->toLink(t('View node'))
            ->toString() : '',
        ]);
      }
    }
  }
  return $allowed;
}