EntityPagerLink.php in Entity Pager 2.0.x
Same filename and directory in other branches
Namespace
Drupal\entity_pagerFile
src/EntityPagerLink.phpView source
<?php
namespace Drupal\entity_pager;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\TypedData\TranslatableInterface;
/**
* A class representing a single Entity Pager link.
*/
class EntityPagerLink implements EntityPagerLinkInterface {
/**
* The entity that this link is for.
*
* @var \Drupal\Core\Entity\EntityInterface|null
*/
public $entity;
/**
* The link text.
*
* @var string
*/
public $text;
/**
* Constructs an EntityPagerLink.
*
* @param string $text
* The text of the link.
* @param \Drupal\Core\Entity\EntityInterface|null $entity
* The result row in the view to link to.
*/
public function __construct($text, EntityInterface $entity = NULL) {
$this->text = $text;
$this->entity = $entity;
}
/**
* {@inheritdoc}
*/
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'),
];
}
/**
* Returns a render array for an entity pager link with no results.
*
* @return array
* The render array for the link with no results.
*/
protected function noResult() {
return [
'#type' => 'markup',
'#markup' => '<span class="inactive">' . $this->text . '</span>',
];
}
}
Classes
Name![]() |
Description |
---|---|
EntityPagerLink | A class representing a single Entity Pager link. |