Commit bfadcb6b authored by webchick's avatar webchick
Browse files

Issue #2470093 by keopx, jhodgdon, dawehner: Views plugin 'user' needs to be...

Issue #2470093 by keopx, jhodgdon, dawehner: Views plugin 'user' needs to be replaced with entity-aware 'field' plugin
parent 70890d6e
......@@ -189,7 +189,6 @@ public function getViewsData() {
$data['comment_field_data']['uid']['relationship']['title'] = t('Author');
$data['comment_field_data']['uid']['relationship']['help'] = t("The User ID of the comment's author.");
$data['comment_field_data']['uid']['relationship']['label'] = t('author');
$data['comment_field_data']['uid']['field']['id'] = 'user';
$data['comment_field_data']['pid']['title'] = t('Parent CID');
$data['comment_field_data']['pid']['relationship']['title'] = t('Parent comment');
......
......@@ -72,7 +72,7 @@ public function testCommentFields() {
$this->assertFieldAccess('comment', 'name', 'anonymous');
$this->assertFieldAccess('comment', 'mail', 'test@example.com');
$this->assertFieldAccess('comment', 'homepage', 'https://example.com');
// $this->assertFieldAccess('comment', 'uid', $comment->uid->target_id);
$this->assertFieldAccess('comment', 'uid', $user->getUsername());
// $this->assertFieldAccess('comment', 'created', \Drupal::service('date.formatter')->format(123456));
// $this->assertFieldAccess('comment', 'changed', \Drupal::service('date.formatter')->format(REQUEST_TIME));
$this->assertFieldAccess('comment', 'status', 'On');
......
......@@ -225,7 +225,6 @@ public function getViewsData() {
$data['node_field_data']['uid']['help'] = t('The user authoring the content. If you need more fields than the uid add the content: author relationship');
$data['node_field_data']['uid']['filter']['id'] = 'user_name';
$data['node_field_data']['uid']['field']['id'] = 'user';
$data['node_field_data']['uid']['relationship']['title'] = t('Content author');
$data['node_field_data']['uid']['relationship']['help'] = t('Relate content to the user who created it.');
$data['node_field_data']['uid']['relationship']['label'] = t('author');
......
......@@ -48,6 +48,7 @@ public function testNodeFields() {
$node = Node::create([
'type' => 'article',
'title' => 'Test title',
'uid' => $user->id(),
'status' => 1,
'promote' => 1,
'sticky' => 0,
......@@ -64,7 +65,7 @@ public function testNodeFields() {
$this->assertFieldAccess('node', 'type', $node->type->entity->label());
$this->assertFieldAccess('node', 'langcode', $node->language()->getName());
$this->assertFieldAccess('node', 'title', 'Test title');
// $this->assertFieldAccess('node', 'uid', $user->getUsername());
$this->assertFieldAccess('node', 'uid', $user->getUsername());
// @todo Don't we want to display Published / Unpublished by default,
// see https://www.drupal.org/node/2465623
$this->assertFieldAccess('node', 'status', 'On');
......
<?php
/**
* @file
* Definition of Drupal\user\Plugin\views\field\User.
*/
namespace Drupal\user\Plugin\views\field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
/**
* Field handler to provide simple renderer that allows linking to a user.
*
* @ingroup views_field_handlers
*
* @ViewsField("user")
*/
class User extends FieldPluginBase {
/**
* {@inheritdoc}
*/
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
if (!empty($this->options['link_to_user'])) {
$this->additional_fields['uid'] = 'uid';
}
}
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['link_to_user'] = array('default' => TRUE);
return $options;
}
/**
* Provide link to node option
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['link_to_user'] = array(
'#title' => $this->t('Link this field to its user'),
'#description' => $this->t("Enable to override this field's links."),
'#type' => 'checkbox',
'#default_value' => $this->options['link_to_user'],
);
parent::buildOptionsForm($form, $form_state);
}
/**
* Prepares a link to the user.
*
* @param string $data
* The XSS safe string for the link text.
* @param \Drupal\views\ResultRow $values
* The values retrieved from a single row of a view's query result.
*
* @return string
* Returns a string for the link text.
*/
protected function renderLink($data, ResultRow $values) {
if (!empty($this->options['link_to_user']) && $this->view->getUser()->hasPermission('access user profiles') && ($entity = $this->getEntity($values)) && $data !== NULL && $data !== '') {
$this->options['alter']['make_link'] = TRUE;
$this->options['alter']['url'] = $entity->urlInfo();
}
return $data;
}
/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
$value = $this->getValue($values);
return $this->renderLink($this->sanitizeValue($value), $values);
}
}
......@@ -56,7 +56,7 @@ public function testUserFields() {
// @todo Expand the test coverage in https://www.drupal.org/node/2464635
// $this->assertFieldAccess('user', 'uid', $user->id());
$this->assertFieldAccess('user', 'uid', $user->id());
$this->assertFieldAccess('user', 'uuid', $user->uuid());
$this->assertFieldAccess('user', 'langcode', $user->language()->getName());
$this->assertFieldAccess('user', 'preferred_langcode', 'Spanish');
......
......@@ -25,7 +25,6 @@ public function getViewsData() {
$data['users_field_data']['table']['wizard_id'] = 'user';
$data['users_field_data']['uid']['field']['id'] = 'user';
$data['users_field_data']['uid']['argument']['id'] = 'user_uid';
$data['users_field_data']['uid']['argument'] += array(
'name table' => 'users_field_data',
......
......@@ -82,7 +82,6 @@ display:
empty_zero: false
hide_alter_empty: true
plugin_id: field
type: user
entity_type: user
entity_field: uid
permission:
......
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