Same filename and directory in other branches
- 8.9.x core/modules/contact/src/Plugin/views/field/ContactLink.php
- 9 core/modules/contact/src/Plugin/views/field/ContactLink.php
Namespace
Drupal\contact\Plugin\views\field
File
core/modules/contact/src/Plugin/views/field/ContactLink.php
View source
<?php
namespace Drupal\contact\Plugin\views\field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\views\Attribute\ViewsField;
use Drupal\views\Plugin\views\field\LinkBase;
use Drupal\views\ResultRow;
class ContactLink extends LinkBase {
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['text']['#title'] = $this
->t('Link label');
$form['text']['#required'] = TRUE;
$form['text']['#default_value'] = empty($this->options['text']) ? $this
->getDefaultLabel() : $this->options['text'];
}
protected function getUrlInfo(ResultRow $row) {
$entity = $this
->getEntity($row);
if (!$entity) {
return NULL;
}
return Url::fromRoute('entity.user.contact_form', [
'user' => $entity
->id(),
]);
}
protected function renderLink(ResultRow $row) {
$entity = $this
->getEntity($row);
if (!$entity) {
return '';
}
$this->options['alter']['make_link'] = TRUE;
$this->options['alter']['url'] = $this
->getUrlInfo($row);
$title = $this
->t('Contact %user', [
'%user' => $entity
->label(),
]);
$this->options['alter']['attributes'] = [
'title' => $title,
];
if (!empty($this->options['text'])) {
return $this->options['text'];
}
else {
return $title;
}
}
protected function getDefaultLabel() {
return $this
->t('contact');
}
}
Classes