Commit 707c3d2f authored by catch's avatar catch

Issue #1859884 by dawehner: Convert user link fields to use entities.

parent 08df7b0d
......@@ -9,6 +9,7 @@
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ViewExecutable;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Annotation\Plugin;
/**
......@@ -57,16 +58,29 @@ public function query() {
$this->add_additional_fields();
}
/**
* Overrides \Drupal\views\Plugin\views\field\FieldPluginBase::render().
*/
function render($values) {
$value = $this->get_value($values, 'uid');
return $this->render_link($this->sanitizeValue($value), $values);
return $this->render_link($this->get_entity($values), $values);
}
function render_link($data, $values) {
/**
* Alters the field to render a link.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* @param \stdClass $values
* The current row of the views result.
*
* @return string
* The acutal rendered text (without the link) of this field.
*/
public function render_link(EntityInterface $entity, \stdClass $values) {
$text = !empty($this->options['text']) ? $this->options['text'] : t('view');
$this->options['alter']['make_link'] = TRUE;
$this->options['alter']['path'] = "user/" . $data;
$uri = $entity->uri();
$this->options['alter']['path'] = $uri['path'];
return $text;
}
......
......@@ -7,6 +7,7 @@
namespace Drupal\user\Plugin\views\field;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Annotation\Plugin;
/**
......@@ -21,19 +22,17 @@
*/
class LinkCancel extends Link {
function render_link($data, $values) {
$uid = $values->{$this->aliases['uid']};
// Build a pseudo account object to be able to check the access.
$account = entity_create('user', array());
$account->uid = $uid;
if ($uid && user_cancel_access($account)) {
/**
* Overrides \Drupal\user\Plugin\views\field\Link::render_link().
*/
public function render_link(EntityInterface $entity, \stdClass $values) {
if ($entity && user_cancel_access($entity)) {
$this->options['alter']['make_link'] = TRUE;
$text = !empty($this->options['text']) ? $this->options['text'] : t('cancel');
$this->options['alter']['path'] = "user/$uid/cancel";
$uri = $entity->uri();
$this->options['alter']['path'] = $uri['path'] . '/cancel';
$this->options['alter']['query'] = drupal_get_destination();
return $text;
......
......@@ -7,6 +7,7 @@
namespace Drupal\user\Plugin\views\field;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Annotation\Plugin;
/**
......@@ -21,17 +22,17 @@
*/
class LinkEdit extends Link {
function render_link($data, $values) {
// Build a pseudo account object to be able to check the access.
$account = entity_create('user', array());
$account->uid = $data;
if ($data && user_edit_access($account)) {
/**
* Overrides \Drupal\user\Plugin\views\field\Link::render_link().
*/
public function render_link(EntityInterface $entity, \stdClass $values) {
if ($entity && user_edit_access($entity)) {
$this->options['alter']['make_link'] = TRUE;
$text = !empty($this->options['text']) ? $this->options['text'] : t('edit');
$this->options['alter']['path'] = "user/$data/edit";
$uri = $entity->uri();
$this->options['alter']['path'] = $uri['path'] . '/edit';
$this->options['alter']['query'] = drupal_get_destination();
return $text;
......
......@@ -53,9 +53,10 @@ public function buildOptionsForm(&$form, &$form_state) {
}
function render_link($data, $values) {
if (!empty($this->options['link_to_user']) && user_access('access user profiles') && ($uid = $this->get_value($values, 'uid')) && $data !== NULL && $data !== '') {
if (!empty($this->options['link_to_user']) && user_access('access user profiles') && ($entity = $this->get_entity($values)) && $data !== NULL && $data !== '') {
$this->options['alter']['make_link'] = TRUE;
$this->options['alter']['path'] = "user/" . $uid;
$uri = $entity->uri();
$this->options['alter']['path'] = $uri['path'];
}
return $data;
}
......
......@@ -355,12 +355,15 @@ function element_wrapper_classes($row_index = NULL) {
}
/**
* Get the entity matching the current row and relationship.
* Gets the entity matching the current row and relationship.
*
* @param $values
* @param \stdClass $values
* An object containing all retrieved values.
*
* @return \Drupal\Core\Entity\EntityInterface
* Returns the entity matching the values.
*/
function get_entity($values) {
public function get_entity(\stdClass $values) {
$relationship_id = $this->options['relationship'];
if ($relationship_id == 'none') {
return $values->_entity;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment