Version 1
[yaffs-website] / web / core / modules / contact / src / Plugin / views / field / ContactLink.php
1 <?php
2
3 namespace Drupal\contact\Plugin\views\field;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Url;
7 use Drupal\views\Plugin\views\field\LinkBase;
8 use Drupal\views\ResultRow;
9
10 /**
11  * Defines a field that links to the user contact page, if access is permitted.
12  *
13  * @ingroup views_field_handlers
14  *
15  * @ViewsField("contact_link")
16  */
17 class ContactLink extends LinkBase {
18
19   /**
20    * {@inheritdoc}
21    */
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'];
27   }
28
29   /**
30    * {@inheritdoc}
31    */
32   protected function getUrlInfo(ResultRow $row) {
33     return Url::fromRoute('entity.user.contact_form', ['user' => $this->getEntity($row)->id()]);
34   }
35
36   /**
37    * {@inheritdoc}
38    */
39   protected function renderLink(ResultRow $row) {
40     $entity = $this->getEntity($row);
41
42     $this->options['alter']['make_link'] = TRUE;
43     $this->options['alter']['url'] = $this->getUrlInfo($row);
44
45     $title = $this->t('Contact %user', ['%user' => $entity->label()]);
46     $this->options['alter']['attributes'] = ['title' => $title];
47
48     if (!empty($this->options['text'])) {
49       return $this->options['text'];
50     }
51     else {
52       return $title;
53     }
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   protected function getDefaultLabel() {
60     return $this->t('contact');
61   }
62
63 }