You are here

class EntityLink in Drupal 10

Same name in this branch
  1. 10 core/modules/views/src/Plugin/views/field/EntityLink.php \Drupal\views\Plugin\views\field\EntityLink
  2. 10 core/modules/comment/src/Plugin/views/field/EntityLink.php \Drupal\comment\Plugin\views\field\EntityLink
Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/field/EntityLink.php \Drupal\views\Plugin\views\field\EntityLink
  2. 9 core/modules/views/src/Plugin/views/field/EntityLink.php \Drupal\views\Plugin\views\field\EntityLink

Field handler to present a link to an entity.

Plugin annotation

@ViewsField("entity_link");

Hierarchy

Expanded class hierarchy of EntityLink

1 file declares its use of EntityLink
TranslationLink.php in core/modules/content_translation/src/Plugin/views/field/TranslationLink.php

File

core/modules/views/src/Plugin/views/field/EntityLink.php, line 15

Namespace

Drupal\views\Plugin\views\field
View source
class EntityLink extends LinkBase {

  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $row) {
    return $this
      ->getEntity($row) ? parent::render($row) : [];
  }

  /**
   * {@inheritdoc}
   */
  protected function renderLink(ResultRow $row) {
    if ($this->options['output_url_as_text']) {
      return $this
        ->getUrlInfo($row)
        ->toString();
    }
    return parent::renderLink($row);
  }

  /**
   * {@inheritdoc}
   */
  protected function getUrlInfo(ResultRow $row) {
    $template = $this
      ->getEntityLinkTemplate();
    $entity = $this
      ->getEntity($row);
    if ($this->languageManager
      ->isMultilingual()) {
      $entity = $this
        ->getEntityTranslation($entity, $row);
    }
    return $entity
      ->toUrl($template)
      ->setAbsolute($this->options['absolute']);
  }

  /**
   * Returns the entity link template name identifying the link route.
   *
   * @returns string
   *   The link template name.
   */
  protected function getEntityLinkTemplate() {
    return 'canonical';
  }

  /**
   * {@inheritdoc}
   */
  protected function getDefaultLabel() {
    return $this
      ->t('view');
  }

  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['output_url_as_text'] = [
      'default' => FALSE,
    ];
    $options['absolute'] = [
      'default' => FALSE,
    ];
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    $form['output_url_as_text'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Output the URL as text'),
      '#default_value' => $this->options['output_url_as_text'],
    ];
    $form['absolute'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use absolute link (begins with "http://")'),
      '#default_value' => $this->options['absolute'],
      '#description' => $this
        ->t('Enable this option to output an absolute link. Required if you want to use the path as a link destination.'),
    ];
    parent::buildOptionsForm($form, $form_state);

    // Only show the 'text' field if we don't want to output the raw URL.
    $form['text']['#states']['visible'][':input[name="options[output_url_as_text]"]'] = [
      'checked' => FALSE,
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityLink::buildOptionsForm public function Overrides LinkBase::buildOptionsForm
EntityLink::defineOptions protected function Overrides LinkBase::defineOptions
EntityLink::getDefaultLabel protected function Returns the default label for this link. Overrides LinkBase::getDefaultLabel 3
EntityLink::getEntityLinkTemplate protected function Returns the entity link template name identifying the link route. 3
EntityLink::getUrlInfo protected function Returns the URI elements of the link. Overrides LinkBase::getUrlInfo
EntityLink::render public function Overrides LinkBase::render
EntityLink::renderLink protected function Prepares the link to view an entity. Overrides LinkBase::renderLink 2
EntityTranslationRenderTrait::$entityTranslationRenderer protected property The renderer to be used to render the entity row.
EntityTranslationRenderTrait::getEntityTranslation public function Returns the entity translation matching the configured row language.
EntityTranslationRenderTrait::getEntityTranslationRenderer protected function Returns the current renderer.
LinkBase::$accessManager protected property The access manager service.
LinkBase::$currentUser protected property Current user object.
LinkBase::$entityRepository protected property The entity repository.
LinkBase::$entityTypeManager protected property The entity type manager.
LinkBase::$languageManager protected property The language manager.
LinkBase::addLangcode protected function Adds language information to the options.
LinkBase::checkUrlAccess protected function Checks access to the link route.
LinkBase::create public static function
LinkBase::currentUser protected function Gets the current active user.
LinkBase::getEntityRepository protected function
LinkBase::getEntityTypeId protected function Returns the entity type identifier. Overrides EntityTranslationRenderTrait::getEntityTypeId
LinkBase::getEntityTypeManager protected function
LinkBase::getLanguageManager protected function Returns the language manager. Overrides EntityTranslationRenderTrait::getLanguageManager
LinkBase::getView protected function Returns the top object of a view. Overrides EntityTranslationRenderTrait::getView
LinkBase::query public function
LinkBase::usesGroupBy public function
LinkBase::__construct public function Constructs a LinkBase object.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.