You are here

public function EntityManager::isPublished in Acquia Content Hub 8

Detect the publishing status of a particular entity.

If the entity is translatable and has at least one published translation then the resulting publishing status will be TRUE.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The Content Entity.

Return value

bool TRUE if at least one published translation, FALSE otherwise.

2 calls to EntityManager::isPublished()
EntityManager::enqueueCandidateEntity in src/EntityManager.php
Enqueue an entity with an operation to be performed on Content Hub.
EntityManager::unexportDisqualifiedExportCandidateEntities in src/EntityManager.php
Delete entities from Content Hub that are disqualified of exporting.

File

src/EntityManager.php, line 830

Class

EntityManager
Provides a service for managing entity actions for Content Hub.

Namespace

Drupal\acquia_contenthub

Code

public function isPublished(ContentEntityInterface $entity) {

  /** @var \Drupal\Core\Extension\ModuleHandlerInterface $module_handler */
  $module_handler = \Drupal::getContainer()
    ->get("module_handler");
  if ($module_handler
    ->moduleExists('content_translation')) {

    /** @var \Drupal\content_translation\ContentTranslationManagerInterface $translation_manager */
    $translation_manager = \Drupal::getContainer()
      ->get("content_translation.manager");

    // If we are using content_translation module and this is a 'translatable'
    // entity, then check if at least one translation is published.
    if ($translation_manager
      ->isEnabled($entity
      ->getEntityTypeId(), $entity
      ->bundle())) {

      // Check whether this entity has at least one published translation.
      $languages = $entity
        ->getTranslationLanguages();
      foreach ($languages as $language) {
        $langcode = $language
          ->getId();
        $localized_entity = $entity
          ->getTranslation($langcode);

        /** @var \Drupal\content_translation\ContentTranslationMetadataWrapperInterface $translation_metadata */
        $translation_metadata = $translation_manager
          ->getTranslationMetadata($localized_entity);
        if ($translation_metadata
          ->isPublished()) {
          return TRUE;
        }
      }
      return FALSE;
    }
  }
  return $entity instanceof NodeInterface ? $entity
    ->isPublished() : TRUE;
}