3 namespace Drupal\contact\Plugin\views\field;
5 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\views\Plugin\views\field\LinkBase;
8 use Drupal\views\ResultRow;
11 * Defines a field that links to the user contact page, if access is permitted.
13 * @ingroup views_field_handlers
15 * @ViewsField("contact_link")
17 class ContactLink extends LinkBase {
22 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
23 parent::buildOptionsForm($form, $form_state);
24 $form['text']['#title'] = $this->t('Link label');
25 $form['text']['#required'] = TRUE;
26 $form['text']['#default_value'] = empty($this->options['text']) ? $this->getDefaultLabel() : $this->options['text'];
32 protected function getUrlInfo(ResultRow $row) {
33 return Url::fromRoute('entity.user.contact_form', ['user' => $this->getEntity($row)->id()]);
39 protected function renderLink(ResultRow $row) {
40 $entity = $this->getEntity($row);
42 $this->options['alter']['make_link'] = TRUE;
43 $this->options['alter']['url'] = $this->getUrlInfo($row);
45 $title = $this->t('Contact %user', ['%user' => $entity->label()]);
46 $this->options['alter']['attributes'] = ['title' => $title];
48 if (!empty($this->options['text'])) {
49 return $this->options['text'];
59 protected function getDefaultLabel() {
60 return $this->t('contact');