EntityLink.php in Drupal 10
File
core/modules/views/src/Plugin/views/field/EntityLink.php
View source
<?php
namespace Drupal\views\Plugin\views\field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\ResultRow;
class EntityLink extends LinkBase {
public function render(ResultRow $row) {
return $this
->getEntity($row) ? parent::render($row) : [];
}
protected function renderLink(ResultRow $row) {
if ($this->options['output_url_as_text']) {
return $this
->getUrlInfo($row)
->toString();
}
return parent::renderLink($row);
}
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']);
}
protected function getEntityLinkTemplate() {
return 'canonical';
}
protected function getDefaultLabel() {
return $this
->t('view');
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['output_url_as_text'] = [
'default' => FALSE,
];
$options['absolute'] = [
'default' => FALSE,
];
return $options;
}
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);
$form['text']['#states']['visible'][':input[name="options[output_url_as_text]"]'] = [
'checked' => FALSE,
];
}
}
Classes
Name |
Description |
EntityLink |
Field handler to present a link to an entity. |