You are here

class EntityPagerLink in Entity Pager 8

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

A class representing a single Entity Pager link.

Hierarchy

Expanded class hierarchy of EntityPagerLink

1 file declares its use of EntityPagerLink
EntityPagerLinkTest.php in tests/src/Kernel/EntityPagerLinkTest.php

File

src/EntityPagerLink.php, line 12

Namespace

Drupal\entity_pager
View source
class EntityPagerLink implements EntityPagerLinkInterface {
  use StringTranslationTrait;

  /**
   * 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>',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityPagerLink::$entity public property The entity that this link is for.
EntityPagerLink::$text public property The link text.
EntityPagerLink::getLink public function Returns a render array for the link. Overrides EntityPagerLinkInterface::getLink
EntityPagerLink::noResult protected function Returns a render array for an entity pager link with no results.
EntityPagerLink::__construct public function Constructs an EntityPagerLink.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.