You are here

class PreviewLinkHost in Preview Link 2.x

Same name and namespace in other branches
  1. 2.0.x src/PreviewLinkHost.php \Drupal\preview_link\PreviewLinkHost

Service for relationships between preview links and entities they unlock.

Hierarchy

Expanded class hierarchy of PreviewLinkHost

1 string reference to 'PreviewLinkHost'
preview_link.services.yml in ./preview_link.services.yml
preview_link.services.yml
1 service uses PreviewLinkHost
preview_link.host in ./preview_link.services.yml
Drupal\preview_link\PreviewLinkHost

File

src/PreviewLinkHost.php, line 13

Namespace

Drupal\preview_link
View source
class PreviewLinkHost implements PreviewLinkHostInterface {

  /**
   * Preview link storage.
   *
   * @var \Drupal\preview_link\PreviewLinkStorageInterface
   */
  protected $previewLinkStorage;

  /**
   * PreviewLinkHost constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   Entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager) {
    $this->previewLinkStorage = $entityTypeManager
      ->getStorage('preview_link');
  }

  /**
   * {@inheritdoc}
   */
  public function getPreviewLinks(EntityInterface $entity) : array {
    $ids = $this->previewLinkStorage
      ->getQuery()
      ->condition('entities.target_type', $entity
      ->getEntityTypeId())
      ->condition('entities.target_id', $entity
      ->id())
      ->execute();
    return $this->previewLinkStorage
      ->loadMultiple($ids);
  }

  /**
   * {@inheritdoc}
   */
  public function isToken(EntityInterface $entity, array $tokens) : bool {
    $count = $this->previewLinkStorage
      ->getQuery()
      ->condition('entities.target_type', $entity
      ->getEntityTypeId())
      ->condition('entities.target_id', $entity
      ->id())
      ->condition('token', $tokens, 'IN')
      ->count()
      ->execute();
    return $count > 0;
  }

  /**
   * {@inheritdoc}
   */
  public function hasPreviewLinks(EntityInterface $entity) : bool {
    $count = $this->previewLinkStorage
      ->getQuery()
      ->condition('entities.target_type', $entity
      ->getEntityTypeId())
      ->condition('entities.target_id', $entity
      ->id())
      ->count()
      ->execute();
    return $count > 0;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PreviewLinkHost::$previewLinkStorage protected property Preview link storage.
PreviewLinkHost::getPreviewLinks public function Get preview links for an entity. Overrides PreviewLinkHostInterface::getPreviewLinks
PreviewLinkHost::hasPreviewLinks public function Determines if an entity has any active preview links. Overrides PreviewLinkHostInterface::hasPreviewLinks
PreviewLinkHost::isToken public function Determines if a token unlocks an entity entity. Overrides PreviewLinkHostInterface::isToken
PreviewLinkHost::__construct public function PreviewLinkHost constructor.