EntityPagerLink.php in Entity Pager 8
File
src/EntityPagerLink.php
View source
<?php
namespace Drupal\entity_pager;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\TypedData\TranslatableInterface;
class EntityPagerLink implements EntityPagerLinkInterface {
use StringTranslationTrait;
public $entity;
public $text;
public function __construct($text, EntityInterface $entity = NULL) {
$this->text = $text;
$this->entity = $entity;
}
public function getLink() {
if (empty($this->entity)) {
return $this
->noResult();
}
$langcode = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
$entity = $this->entity;
if ($entity instanceof TranslatableInterface && $entity
->hasTranslation($langcode)) {
$entity = $entity
->getTranslation($langcode);
}
return [
'#type' => 'link',
'#title' => [
'#markup' => $this->text,
],
'#url' => $entity
->toUrl('canonical'),
];
}
protected function noResult() {
return [
'#type' => 'markup',
'#markup' => '<span class="inactive">' . $this->text . '</span>',
];
}
}