You are here

function _api_unpublishing_allowed in Scheduler 2.x

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

3 calls to _api_unpublishing_allowed()
scheduler_api_test_scheduler_commerce_product_unpublishing_allowed in tests/modules/scheduler_api_test/scheduler_api_test.module
Implements hook_scheduler_commerce_product_unpublishing_allowed().
scheduler_api_test_scheduler_media_unpublishing_allowed in tests/modules/scheduler_api_test/scheduler_api_test.module
Implements hook_scheduler_media_unpublishing_allowed().
scheduler_api_test_scheduler_node_unpublishing_allowed in tests/modules/scheduler_api_test/scheduler_api_test.module
Implements hook_scheduler_node_unpublishing_allowed().

File

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

Code

function _api_unpublishing_allowed(EntityInterface $entity) {

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

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

    // If unpublishing 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 unpublishing @unpublish_time, but will not be unpublished until approved.', [
        '%title' => $entity
          ->label(),
        '@unpublish_time' => \Drupal::service('date.formatter')
          ->format($entity->unpublish_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->unpublish_on->value <= \Drupal::time()
        ->getRequestTime()) {
        \Drupal::logger('scheduler_api_test')
          ->warning('Unpublishing of "%title" is prevented until approved.', [
          '%title' => $entity
            ->label(),
          'link' => !empty($entity
            ->id()) ? $entity
            ->toLink(t('View'))
            ->toString() : '',
        ]);
      }
    }
  }
  return $allowed;
}