You are here

function _api_publishing_allowed in Scheduler 2.x

Generic function to check if the entity is allowed to be published.

3 calls to _api_publishing_allowed()
scheduler_api_test_scheduler_commerce_product_publishing_allowed in tests/modules/scheduler_api_test/scheduler_api_test.module
Implements hook_scheduler_commerce_product_publishing_allowed().
scheduler_api_test_scheduler_media_publishing_allowed in tests/modules/scheduler_api_test/scheduler_api_test.module
Implements hook_scheduler_media_publishing_allowed().
scheduler_api_test_scheduler_node_publishing_allowed in tests/modules/scheduler_api_test/scheduler_api_test.module
Implements hook_scheduler_node_publishing_allowed().

File

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

Code

function _api_publishing_allowed(EntityInterface $entity) {

  // If there is no 'Approved for Publishing' field or we are not dealing with
  // an entity designed for this test then allow publishing.
  if (!isset($entity->field_approved_publishing) || !stristr($entity
    ->label(), "blue {$entity->getEntityTypeId()}")) {
    $allowed = TRUE;
  }
  else {

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

    // If publishing is denied then inform the user why.
    if (!$allowed) {

      // Show a message when the entity is saved.
      \Drupal::messenger()
        ->addMessage(t('%title is scheduled for publishing @publish_time, but will not be published until approved.', [
        '%title' => $entity
          ->label(),
        '@publish_time' => \Drupal::service('date.formatter')
          ->format($entity->publish_on->value, 'long'),
      ]), '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 entity but
      // cater for no id as the entity may be new and not yet saved.
      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' => !empty($entity
            ->id()) ? $entity
            ->toLink(t('View'))
            ->toString() : '',
        ]);
      }
    }
  }
  return $allowed;
}