ContactLink.php 3.54 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\contact\Plugin\views\field\ContactLink.
 */

namespace Drupal\contact\Plugin\views\field;

10
use Drupal\Core\Access\AccessManagerInterface;
11
use Drupal\Core\Entity\EntityInterface;
12
use Drupal\Core\Form\FormStateInterface;
13
use Drupal\Core\Session\AccountInterface;
14
use Drupal\Core\Url;
15
use Drupal\user\Plugin\views\field\Link;
16
use Drupal\views\ResultRow;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18 19 20 21 22 23

/**
 * Defines a field that links to the user contact page, if access is permitted.
 *
 * @ingroup views_field_handlers
 *
24
 * @ViewsField("contact_link")
25 26 27
 */
class ContactLink extends Link {

28 29 30
  /**
   * The access manager.
   *
31
   * @var \Drupal\Core\Access\AccessManagerInterface
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
   */
  protected $accessManager;

  /**
   * Current user object.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * Gets the current active user.
   *
   * @todo: https://drupal.org/node/2105123 put this method in
   *   \Drupal\Core\Plugin\PluginBase instead.
   *
   * @return \Drupal\Core\Session\AccountInterface
   */
  protected function currentUser() {
    if (!$this->currentUser) {
      $this->currentUser = \Drupal::currentUser();
    }
    return $this->currentUser;
  }

  /**
   * Constructs a ContactLink object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
64
   * @param mixed $plugin_definition
65
   *   The plugin implementation definition.
66
   * @param \Drupal\Core\Access\AccessManagerInterface $access_manager
67 68
   *   The access manager.
   */
69
  public function __construct(array $configuration, $plugin_id, $plugin_definition, AccessManagerInterface $access_manager) {
70 71 72 73 74 75 76
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->accessManager = $access_manager;
  }

  /**
   * {@inheritdoc}
   */
77
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
78 79 80 81 82 83 84 85
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('access_manager')
    );
  }

86 87 88
  /**
   * {@inheritdoc}
   */
89
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
90
    parent::buildOptionsForm($form, $form_state);
91
    $form['text']['#title'] = $this->t('Link label');
92
    $form['text']['#required'] = TRUE;
93
    $form['text']['#default_value'] = empty($this->options['text']) ? $this->t('contact') : $this->options['text'];
94 95 96 97 98
  }

  /**
   * {@inheritdoc}
   */
99
  public function access(AccountInterface $account) {
100 101 102 103 104 105 106 107
    // The access logic is implemented per row.
    return TRUE;
  }


  /**
   * {@inheritdoc}
   */
108
  protected function renderLink(EntityInterface $entity, ResultRow $values) {
109 110 111 112 113 114 115

    if (empty($entity)) {
      return;
    }

    // Check access when we pull up the user account so we know
    // if the user has made the contact page available.
116
    if (!$this->accessManager->checkNamedRoute('entity.user.contact_form', array('user' => $entity->id()), $this->currentUser())) {
117 118 119 120
      return;
    }

    $this->options['alter']['make_link'] = TRUE;
121
    $this->options['alter']['url'] =  Url::fromRoute('entity.user.contact_form', ['user' => $entity->id()]);
122

123
    $title = $this->t('Contact %user', array('%user' => $entity->name->value));
124 125 126 127 128 129 130 131 132 133 134
    $this->options['alter']['attributes'] = array('title' => $title);

    if (!empty($this->options['text'])) {
      return $this->options['text'];
    }
    else {
      return $title;
    }
  }

}