provide Views reverse relationships automatically for entity base fields

Created on 14 April 2016, about 9 years ago
Updated 29 June 2023, almost 2 years ago

core_field_views_data() provides reverse relationships for entity reference fields, but this is only for config fields.

For base fields on entities, such as the node uid field, core entity modules have to implement this themselves.

Eg UserViewsData::UserViewsData():

    $data['users_field_data']['uid']['relationship'] = array(
      'title' => t('Content authored'),
      'help' => 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' => t('nodes'),
    );
✨ Feature request
Status

Needs work

Version

11.0 πŸ”₯

Component
ViewsΒ  β†’

Last updated about 9 hours ago

Created by

πŸ‡¬πŸ‡§United Kingdom joachim

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • last update almost 2 years ago
    Patch Failed to Apply
  • Status changed to Needs review almost 2 years ago
  • last update almost 2 years ago
    29,468 pass, 2 fail
  • πŸ‡©πŸ‡ͺGermany szeidler Berlin

    Rerolling patch #75 for Drupal 10.1.x.

  • πŸ‡¬πŸ‡§United Kingdom jonathanshaw Stroud, UK
  • Status changed to Needs work over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Did not review.

    But was previously tagged for tests which still appear to be needed

    Also issue summary should follow standard template.

  • last update over 1 year ago
    29,721 pass, 2 fail
  • πŸ‡¬πŸ‡§United Kingdom jonathanshaw Stroud, UK

    Anyone wanting to work on the missing test should look at EntityReferenceRelationshipTest (which tests the forward and reverse relationship for configured fields) and EntityViewsDataTest (which tests the forward relationship for base fields, and is where the test for the reverse should go).

    Currently EntityViewsDataTest::testBaseTableFields() has:

        $relationship = $data['entity_test']['user_id']['relationship'];
        $this->assertEquals('users_field_data', $relationship['base']);
        $this->assertEquals('uid', $relationship['base field']);

    We probably need to add to this:

        $user_data = $this->entityTypeManager->getHandler('user', 'views_data')->getViewsData();
        $this->assertEquals('entity_reverse', $views_data['reverse__entity_test__user_id']['relationship']['id']);
       ... etc
    
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Environment: PHP 8.1 & MariaDB 10.3.22
    last update over 1 year ago
    29,721 pass, 2 fail
  • πŸ‡©πŸ‡ͺGermany geek-merlin Freiburg, Germany

    Played this and it looks it needs much more work.

    After installing with the commerce module enabled, i get:
    Uncaught PHP Exception ArgumentCountError: "Too few arguments to function Drupal\views\EntityViewsData::mapSingleFieldViewsData(), 7 passed in /home/merlin/Code-Incubator/site-c4c-dev/web/modules/contrib/commerce/src/CommerceEntityViewsData.php on line 220 and exactly 8 expected" at /home/merlin/Code-Incubator/site-c4c-dev/web/core/modules/views/src/EntityViewsData.php line 483

    Which is because a $data ("all views data") arg was added to mapSingleFieldViewsData method.
    - 1) This is a BC break.
    - 2) Looking over the code, my gut feeling is that adding this arg makes complex code even more complex and should be done differently.

  • πŸ‡¬πŸ‡§United Kingdom joachim

    Agreed, the BC break needs fixing, as clearly other code is calling this.

    (We probably need our BC policy to get real and state that generic entity handlers are public API, because everyone already treats them as such.)

    > - 2) Looking over the code, my gut feeling is that adding this arg makes complex code even more complex and should be done differently.

    I'm not sure how!

    The nature of a reverse entity reference field is that we need to add data to ANOTHER table from the one for the current field -- it needs to go on the table for the reference field's target entity.

    And the way the methods in this class work is that there is a helper method for each field type.

    Therefore, the field type helper method, processViewsDataForEntityReference() in this case, needs access to the whole of the Views data, not just for the field's table.

  • Merge request !12202Draft: Reverse relationships base fields β†’ (Open) created by joachim
  • πŸ‡¬πŸ‡§United Kingdom joachim

    In the meantime, I've converted the most recent patch to an MR, rebased on 11.x.

  • Pipeline finished with Failed
    21 days ago
    Total: 381s
    #503870
  • πŸ‡¬πŸ‡§United Kingdom joachim

    The UI texts aren't clear when the host and target entity type are the same, as with taxonomy parent field:

    > β€œTaxonomy term using parent” β€” β€œRelate each Taxonomy term with a parent set to the taxonomy term.

    Needs a rewrite.

  • πŸ‡¬πŸ‡§United Kingdom joachim

    I've had an idea for a clean way to do this. We would need get πŸ“Œ Allow @FieldType to customize views data Needs work in, and then the classes that provide Views data for each field type can implement two methods:

    - one to add field data for the field table, with &$field_data as a param
    - one to allow the class to add data ANYWHERE, with &$data as a param -- most field types would not need to use this

  • Pipeline finished with Failed
    7 days ago
    Total: 566s
    #515887
Production build 0.71.5 2024