Verified Commit df31ad4e authored by Dave Long's avatar Dave Long
Browse files

fix: #2781815 Content authored views relationship should be provided by...

fix: #2781815 Content authored views relationship should be provided by node.module, not user.module

By: jelle_s
By: danielveza
By: dcam
parent 435960da
Loading
Loading
Loading
Loading
Loading
+33 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
use Drupal\user\Entity\Role;
use Drupal\views\ViewExecutable;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\Hook\Order\Order;

/**
 * Views hook implementations for node.
@@ -16,6 +17,38 @@ class NodeViewsHooks {

  use StringTranslationTrait;

  /**
   * Implements hook_views_data_alter().
   */
  #[Hook('views_data_alter', order: Order::First)]
  public function viewsDataAlter(array &$data): void {
    $data['users_field_data']['uid']['relationship'] = [
      'title' => $this->t('Content authored'),
      'help' => $this->t('Relate content to the user who created it. This relationship will create one record for each content item created by the user.'),
      'id' => 'standard',
      'base' => 'node_field_data',
      'base field' => 'uid',
      'field' => 'uid',
      'label' => $this->t('nodes'),
    ];

    $data['users_field_data']['uid_representative'] = [
      'relationship' => [
        'title' => $this->t('Representative node'),
        'label'  => $this->t('Representative node'),
        'help' => $this->t('Obtains a single representative node for each user, according to a chosen sort criterion.'),
        'id' => 'groupwise_max',
        'relationship field' => 'uid',
        'outer field' => 'users_field_data.uid',
        'argument table' => 'users_field_data',
        'argument field' => 'uid',
        'base' => 'node_field_data',
        'field' => 'nid',
        'relationship' => 'node_field_data:uid',
      ],
    ];
  }

  /**
   * Implements hook_views_analyze().
   */
+0 −0

File moved.

+4 −4
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Drupal\Tests\user\Kernel\Views;
namespace Drupal\Tests\node\Kernel\Views;

use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\node\Traits\NodeCreationTrait;
@@ -16,7 +16,7 @@
/**
 * Tests the representative node relationship for users.
 */
#[Group('user')]
#[Group('node')]
#[RunTestsInSeparateProcesses]
class RelationshipRepresentativeNodeTest extends KernelTestBase {

@@ -30,9 +30,9 @@ class RelationshipRepresentativeNodeTest extends KernelTestBase {
  protected static $modules = [
    'filter',
    'node',
    'node_test_views',
    'system',
    'user',
    'user_test_views',
    'views',
  ];

@@ -50,7 +50,7 @@ public function testRelationship(): void {
    $this->installEntitySchema('user');
    $this->installEntitySchema('node');
    $this->installConfig(['filter']);
    ViewTestData::createTestViews(static::class, ['user_test_views']);
    ViewTestData::createTestViews(static::class, ['node_test_views']);

    $users[] = $this->createUser([], NULL, FALSE, ['uid' => 2]);
    $users[] = $this->createUser([], NULL, FALSE, ['uid' => 1]);
+0 −25
Original line number Diff line number Diff line
@@ -29,15 +29,6 @@ public function getViewsData() {
    $data['users_field_data']['uid']['filter']['id'] = 'user_name';
    $data['users_field_data']['uid']['filter']['title'] = $this->t('Name (autocomplete)');
    $data['users_field_data']['uid']['filter']['help'] = $this->t('The user or author name. Uses an autocomplete widget to find a user name, the actual filter uses the resulting user ID.');
    $data['users_field_data']['uid']['relationship'] = [
      'title' => $this->t('Content authored'),
      'help' => $this->t('Relate content to the user who created it. This relationship will create one record for each content item created by the user.'),
      'id' => 'standard',
      'base' => 'node_field_data',
      'base field' => 'uid',
      'field' => 'uid',
      'label' => $this->t('nodes'),
    ];

    $data['users_field_data']['uid_raw'] = [
      'help' => $this->t('The raw numeric user ID.'),
@@ -48,22 +39,6 @@ public function getViewsData() {
      ],
    ];

    $data['users_field_data']['uid_representative'] = [
      'relationship' => [
        'title' => $this->t('Representative node'),
        'label'  => $this->t('Representative node'),
        'help' => $this->t('Obtains a single representative node for each user, according to a chosen sort criterion.'),
        'id' => 'groupwise_max',
        'relationship field' => 'uid',
        'outer field' => 'users_field_data.uid',
        'argument table' => 'users_field_data',
        'argument field' => 'uid',
        'base' => 'node_field_data',
        'field' => 'nid',
        'relationship' => 'node_field_data:uid',
      ],
    ];

    $data['users']['uid_current'] = [
      'real field' => 'uid',
      'title' => $this->t('Current'),