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'),
    );
CommentFileSizeAuthor
#91 2706431-91.patch10.45 KBdahousecat
#80 2706431-80.patch12.45 KBszeidler
#75 interdiff_72-75.txt690 bytesravi.shankar
#75 2706431-75.patch12.46 KBravi.shankar
#72 provide-views-reverse-relationships-automatically-for-entity-base-fields-2706431-72.patch12.9 KBrymcveigh
#65 interdiff-2706431-64-65.txt577 bytesyogeshmpawar
#65 2706431-65.patch14.08 KByogeshmpawar
#64 reroll_diff_59-64.txt9.09 KBravi.shankar
#64 2706431-64.patch14.02 KBravi.shankar
#62 2706431-62.patch14.16 KBravi.shankar
#59 provide-views-reverse-relationships-automatically-for-entity-base-fields-2706431-59.patch14 KBsch4lly
#54 provide-views-reverse-relationships-automatically-for-entity-base-fields-2706431-54.patch14 KBlouis-cuny
#48 provide-views-reverse-relationships-automatically-for-entity-base-fields-2706431-48.patch14.29 KBabdhomsi
#42 2706431-42.drupal.provide-Views-reverse-relationships-automatically-for-entity-base-fields.patch15.08 KBvacho
#36 EntityReferenceViewsData.php_.txt3.77 KBnadavoid
#36 EntityReferenceViewsData.php_.txt3.77 KBnadavoid
#34 2706431-34.drupal.provide-Views-reverse-relationships-automatically-for-entity-base-fields.patch16.06 KBjoachim
#32 2706431-32.drupal.provide-Views-reverse-relationships-automatically-for-entity-base-fields.patch15.08 KBjoachim
#30 2706431-30.core_.provide-Views-reverse-relationships-automatically-for-entity-base-fields.patch12.74 KBjoachim
#28 interdiff.2706431.24-28.txt1.78 KBjoachim
#28 2706431-28.drupal.provide-Views-reverse-relationships-automatically-for-entity-base-fields.patch11.68 KBjoachim
#24 2706431-22-24-interdiff.txt751 bytesrosk0
#24 2706431-24.patch11.78 KBrosk0
#22 2706431-19-22-interdiff.txt1.57 KBrosk0
#22 2706431-22.patch11.75 KBrosk0
#19 2706431-15-19-interdiff.txt7.22 KBrosk0
#19 2706431-19.patch11.56 KBrosk0
#15 2706431-15.drupal.provide-Views-reverse-relationships-automatically-for-entity-base-fields.patch11.27 KBjoachim
#12 2706431-12.core_.provide-Views-reverse-relationships-automatically-for-entity-base-fields.patch10.43 KBjoachim
#10 2706431-10.core_.provide-Views-reverse-relationships-automatically-for-entity-base-fields.patch9.98 KBjoachim
#8 2706431-8.core_.provide-Views-reverse-relationships-automatically-for-entity-base-fields.patch10.01 KBjoachim

Issue fork drupal-2706431

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

joachim created an issue. See original summary.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

joachim’s picture

Technically, this is something we could do in the base entity views handler class, EntityViewsData. Even though we would be setting data for an entity type other than the one being processed by the handler, the returned data from the handler is deep-merged by views_views_data().

joachim’s picture

Status: Active » Needs work

Here's some code I wrote for a specific base field's reverse relationship.

I stuck as closely as possible to the code in core_field_views_data() so it could be generalized:

  // The entity type the field is on.
  $entity_type = \Drupal::entityTypeManager()->getDefinition('commerce_subscription');
  // The entity type the field points to.
  $target_entity_type = \Drupal::entityTypeManager()->getDefinition('commerce_order');

  $target_base_table = $target_entity_type->getDataTable() ?: $target_entity_type->getBaseTable();

  $field_name = 'orders';

  $field_storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions('commerce_subscription');
  $field_storage = $field_storage_definitions[$field_name];

  $table_mapping = \Drupal::service('entity_type.manager')->getStorage('commerce_subscription')->getTableMapping();
  //$orders_field_table = $table_mapping->getDedicatedDataTableName($field_storage_definitions['orders']);

  $args = [
    '@label' => $target_entity_type->getLowercaseLabel(),
    '@field_name' => $field_name,
    '@entity' => $entity_type->getLabel(),
  ];

  // Form the field name the same as core_field_views_data(), for when
  // https://www.drupal.org/project/drupal/issues/2706431 eventually is fixed.
  $data[$target_base_table]['reverse__commerce_subscription__orders'] = [
    'title' => t('Recurring order subscription'),
    'help' => t('TODO users to apply an action to one or more items.'),
    'relationship' => [
      'title' => t('@entity using @field_name', $args),
      'label' => t('@field_name', ['@field_name' => $field_name]),
      'help' => t('Relate each @entity with a @field_name set to the @label.', $args),
      'group' => $target_entity_type->getLabel(),
      'id' => 'entity_reverse',
      'base' => $entity_type->getDataTable() ?: $entity_type->getBaseTable(),
      'entity_type' => $entity_type->id(),
      'base field' => $entity_type->getKey('id'),
      'field_name' => $field_name,
      'field table' => $table_mapping->getDedicatedDataTableName($field_storage),
      'field field' => $field_name . '_target_id',
      'join_extra' => [
        [
          'field' => 'deleted',
          'value' => 0,
          'numeric' => TRUE,
        ],
      ],
    ],
  ];

All that's needed now is to wrap that in a loop like this:

  $efm = \Drupal::service('entity_field.manager');
  $fsd = $efm->getFieldStorageDefinitions('commerce_subscription');
  foreach ($fsd as ... ) {
    // Skip any field that is not a base field entity ref 
 }
joachim’s picture

Status: Needs work » Needs review
StatusFileSize
new10.01 KB

Here's a patch.

Status: Needs review » Needs work
joachim’s picture

Reroll & fixed a stray hardcoded entity type.

Status: Needs review » Needs work
joachim’s picture

Status: Needs work » Needs review
StatusFileSize
new10.43 KB

Status: Needs review » Needs work

The last submitted patch, 12: 2706431-12.core_.provide-Views-reverse-relationships-automatically-for-entity-base-fields.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

joachim’s picture

Should be:

        $pseudo_field_name = 'reverse__' . $this->entityType->id() . '__' . $field_definition->getName();

The idea is to match this pattern for config fields:

    $pseudo_field_name = 'reverse__' . $entity_type_id . '__' . $field_name;
joachim’s picture

Found another problem -- the entity_reverse relationship plugin expects there to be a bridge table between the two entity base tables. That's because it's written for config fields, where the field always has a dedicated table.

With base fields though, the field is on the base table, unless the field is multi-valued. So for most base fields, we want the normal relationship handler.

joachim’s picture

              'label' => t('@field_name', ['@field_name' => $field_name]),

Not a brilliant label, as you often end up with a relationship shown in your UI as:

'(TARGET_TYPE) TARGET_TYPE'

if the field is named for the target type, as is often the case.

Status: Needs review » Needs work

The last submitted patch, 15: 2706431-15.drupal.provide-Views-reverse-relationships-automatically-for-entity-base-fields.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

rosk0’s picture

Status: Needs work » Needs review
StatusFileSize
new11.56 KB
new7.22 KB

Not sure how to handle the situation when referenced table is not described to views yet, so just added isset check.

Lets see what testbot thinks.

Status: Needs review » Needs work

The last submitted patch, 19: 2706431-19.patch, failed testing. View results

joachim’s picture

> Not sure how to handle the situation when referenced table is not described to views yet, so just added isset check.

Ah, you mean because we're within the entity views data handler, and so working on a particular entity, so this situation could happen:

- handler for entity type A is running
- entity type A has a reference to entity type Z, and handler for Z has not run yet to declare the Z base tables to Views.

That's fine. An isset() is not needed here. That's because the code that invokes hook_views_data(), and then the entity views handlers, does a deep merge of all the returned arrays. As long as we put the Z data in the right place, it'll just get merged in.

The isset() should be removed, and a comment added to explain why it's ok.

rosk0’s picture

Status: Needs work » Needs review
StatusFileSize
new11.75 KB
new1.57 KB

Right, I think this way is more obvious

Status: Needs review » Needs work

The last submitted patch, 22: 2706431-22.patch, failed testing. View results

rosk0’s picture

Status: Needs work » Needs review
StatusFileSize
new11.78 KB
new751 bytes

Silly, this is how it supposed to be...

Status: Needs review » Needs work

The last submitted patch, 24: 2706431-24.patch, failed testing. View results

joachim’s picture

$data[$target_base_table][$pseudo_field_name]['relationship']['relationship field'] = $data[$target_base_table]['table']['base']['field'];

Ah but we need that in all cases for the relationship to work!

If we don’t have the entity type yet, we will need to deduce that value.

jsst’s picture

I've tested this patch (#24) on a simple view I have. It's a view on one entity with view_bulk_operations checkboxes. When I add a reverse entity reference field to the view and select a single row for a VBO action, VBO acts as if all rows in the view were selected.

joachim’s picture

Status: Needs work » Needs review
StatusFileSize
new11.68 KB
new1.78 KB

Here's the patch with the right way to handle entity types not being there yet.

I've not had time to investigate the problem reported in #27.

Status: Needs review » Needs work
joachim’s picture

This should fix the failing test. Might also fix #27?

Status: Needs review » Needs work
joachim’s picture

This should fix all the tests, but there will still be one failure due to #3004300: EntityViewsData fails to set 'entity revision' in the table data for an entity's revision table which the changes here expose.

Status: Needs review » Needs work
joachim’s picture

Fixed the other failing test.

Will still have one test failure due to the other issue.

Status: Needs review » Needs work
nadavoid’s picture

@joachim Thank you for the great work on this. Is getting all tests passing the only thing remaining?

I was able to use the important parts of your patch in a custom class, until this patch is committed to core. Attaching it here in case anyone else finds it useful in the interim.

nadavoid’s picture

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

johnpitcairn’s picture

Thanks for your work on this @joachim.

The patch at #34 will also apply cleanly to 8.6.x, and I'm getting usable reverse relationships to an entityreference base field on a custom entity. In my case neither that entity nor the referenced entity are revisionable.

rlmumford’s picture

Re-running tests.

daffie’s picture

Issue tags: +Needs reroll
vacho’s picture

vacho’s picture

Issue tags: -Needs reroll

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

bojanz’s picture

Adapted this code into CommerceEntityViewsData, so that Commerce and its contribs get reverse relationships:
#3096916: Generate reverse relationships for base entity references
Other contribs, feel free to steal it, for until the core patch lands :)

EDIT: This is now a pat of the Entity API contrib module, from version 1.0. just use Drupal\entity\EntityViewsData in your entity type annotation.

damienmckenna’s picture

This does not appear to work for me using ECK entities, but whether that's an ECK problem or a limitation of the patch I do not know yet.

ivnish’s picture

#42 doesn't apply to 8.7.11 :(

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

rob230’s picture

Patch #42 and #48 are breaking the site for me with this error:

PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function Drupal\views\EntityViewsData::mapSingleFieldViewsData(), 7 passed in /var/core/www/modules/contrib/commerce/src/CommerceEntityViewsData.php on line 204 and exactly 9 expected in /var/core/www/core/modules/views/src/EntityViewsData.php:460

This patch breaks backwards compatibility by changing the function definition for mapSingleFieldViewsData() and removing the return parameter, so the entire file must be rewritten.

rob230’s picture

I see in #45 that similar work has been done in Commerce, however, this does not create the reverse relationship from an order to a subscription with that order as its initial order (an entity_reference field). And I cannot apply this patch because it breaks Commerce views integration.

I see this as a Drupal core bug rather than a Commerce bug. For a base field definition that is an entity reference, these views relationships and reverse relationships should be created automatically.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

louis-cuny’s picture

Upload a D9 compatible patch. Just updated two lines.

I faced the following error to noticed the patch was not d9 compatible :
Fatal error: Uncaught Error: Call to a member function getDefinition() on null in /app/web/core/modules/views/src/EntityViewsData.php:611

joachim’s picture

Issue tags: +Needs tests

This should wait until #3116481: Convert EntityViewsDataTest from a unit test to a kernel test is in, as it will need tests.

greggles’s picture

For anyone using a patch before 54 on a Drupal 8 site, perhaps using composer-patches, when you upgrade to Drupal 9 you will get an error stacktrace that starts like this:

The website encountered an unexpected error. Please try again later.
Error: Call to a member function getDefinition() on null in Drupal\views\EntityViewsData->processViewsDataForEntityReference() (line 611 of core/modules/views/src/EntityViewsData.php).

Putting this here in the hopes the search engines will index it and it will help other people running into this problem :)

dqd’s picture

Applies at Drupal 9.2.7 with -11 lines offset:

web$ wget https://www.drupal.org/files/issues/2021-08-24/provide-views-reverse-relationships-automatically-for-entity-base-fields-2706431-54.patch
--2021-11-01 16:19:12--  https://www.drupal.org/files/issues/2021-08-24/provide-views-reverse-relationships-automatically-for-entity-base-fields-2706431-54.patch
Resolving www.drupal.org (www.drupal.org)... 151.101.14.217
Connecting to www.drupal.org (www.drupal.org)|151.101.14.217|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 14331 (14K) [text/plain]
Saving to: 'provide-views-reverse-relationships-automatically-for-entity-base-fields-2706431-54.patch'
provide-views-reverse-relationships-automatically-for 100%[===============>]  14.00K  --.-KB/s    in 0.004s
2021-11-01 16:19:13 (3.70 MB/s) - 'provide-views-reverse-relationships-automatically-for-entity-base-fields-2706431-54.patch' saved [14331/14331]
/web$ git apply -v provide-views-reverse-relationships-automatically-for-entity-base-fields-2706431-54.patch
Checking patch core/modules/views/src/EntityViewsData.php...
Hunk #1 succeeded at 301 (offset -11 lines).
Hunk #2 succeeded at 339 (offset -11 lines).
Hunk #3 succeeded at 401 (offset -11 lines).
Hunk #4 succeeded at 418 (offset -11 lines).
Hunk #5 succeeded at 429 (offset -11 lines).
Hunk #6 succeeded at 443 (offset -11 lines).
Hunk #7 succeeded at 550 (offset -11 lines).
Hunk #8 succeeded at 569 (offset -11 lines).
Hunk #9 succeeded at 595 (offset -11 lines).
Hunk #10 succeeded at 607 (offset -11 lines).
Hunk #11 succeeded at 729 (offset -11 lines).
Hunk #12 succeeded at 751 (offset -11 lines).
Applied patch core/modules/views/src/EntityViewsData.php cleanly.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

sch4lly’s picture

Attached patch works for Drupal 9.3, there were some minor deprecation issues which I fixed.

stijndmd’s picture

Applies and works on a clean Drupal 9.3.

a.sinitsa’s picture

Does not apply to 9.4.x-dev

ravi.shankar’s picture

StatusFileSize
new14.16 KB

Added reroll of patch #59 on Drupal 9.4.x.

andregp’s picture

Quick Review:

@sch4lly you may have unintentionally uploaded the wrong patch, but the patch #59 is identical to the patch #54 even the index hash code is the same.

diff --git a/core/modules/views/src/EntityViewsData.php b/core/modules/views/src/EntityViewsData.php
index f8d8ed6301..f6f7221168 100644

Regarding #62. Thank's @ravi.shankar for the re-roll. :)

Just two notes:

  1. You forgot to remove a space between the @param tags here:
    @@ -473,10 +482,10 @@ protected function mapFieldDefinition($table, $field_name, FieldDefinitionInterf
        * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
        *   The field definition.
        *
    -   * @return array
    -   *   The modified views data field definition.
    +   * @param array $data
    +   *   A reference to the views data.
  2. I noticed a that the biggest difference between patch #54 and #62 is at the end of function EntityViewsData::mapFieldDefinition. I Don't know if the change is correct or not, (I didn't analyze deeper), but I'm pointing it out, so someone else can do a deeper review.
ravi.shankar’s picture

StatusFileSize
new14.02 KB
new9.09 KB

Thanks @andregp.

Added reroll of patch #59 for Drupal 9.4.x. and I have removed space as said in comment # 63.1.

Added reroll diff as well.

Please ignore patch #62.

yogeshmpawar’s picture

Status: Needs work » Needs review
StatusFileSize
new14.08 KB
new577 bytes

Resolved CSpell errors & added an interdiff.

yogeshmpawar’s picture

Status: Needs review » Needs work

Keeping it in NW as it requires tests.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

chi’s picture

When used with Commerce module patch #65 causes errors described in #50. Also field_name parameter in mapSingleFieldViewsData() seems unused.

joachim’s picture

> This patch breaks backwards compatibility by changing the function definition for mapSingleFieldViewsData() and removing the return parameter, so the entire file must be rewritten.

The BC policy says that specific entity handlers are internal:

> Particular entity handlers should not be considered part of the public API. The interfaces which define those entity handlers though are part of the supported API.

v.kydyba’s picture

As mentioned in #69, the patch #65 causes 2 errors many times for me in dblog:

  1. Undefined variable: table_data in Drupal\views\EntityViewsData->mapFieldDefinition() (line 457 of core/modules/views/src/EntityViewsData.php)


  2. The $table_data variable is not defined before foreach.

  3. Invalid argument supplied for foreach() in Drupal\Component\Utility\NestedArray::mergeDeepArray() (line 327 of core/lib/Drupal/Component/Utility/NestedArray.php)


  4. The method mapSingleFieldViewsData() is used as argument for NestedArray::mergeDeep() in mapFieldDefinition(), but it returns nothing.

darvanen’s picture

Just a note that "Build Successful" is not a pass. The patch is failing badly and will likely need a reroll for 9.5

rymcveigh’s picture

@lkacenja and I have attached a patch that works on Drupal 9.4 and 9.5.

johnpitcairn’s picture

Status: Needs work » Needs review
johnpitcairn’s picture

Status: Needs review » Needs work
ravi.shankar’s picture

StatusFileSize
new12.46 KB
new690 bytes

Fixed Drupal CS issue of patch #72.

rymcveigh’s picture

The test fail for patch #75 is

Drupal\Tests\aggregator\Functional\Views\Handler\HandlerAggregatorTest::testHandlers
Undefined array key "entity revision"

We probably need to make sure we are adding the entity revision key to our relationship array.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

szeidler’s picture

Status: Needs work » Needs review
StatusFileSize
new12.45 KB

Rerolling patch #75 for Drupal 10.1.x.

Status: Needs review » Needs work

The last submitted patch, 80: 2706431-80.patch, failed testing. View results

jonathanshaw’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update

Did not review.

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

Also issue summary should follow standard template.

jonathanshaw’s picture

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
geek-merlin’s picture

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.

joachim’s picture

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.

joachim’s picture

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

joachim’s picture

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.

joachim’s picture

I've had an idea for a clean way to do this. We would need get #2337515: Allow @FieldType to customize views data 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

dahousecat’s picture

StatusFileSize
new10.45 KB

Rerolling patch #80 for Drupal 11

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

fskreuz made their first commit to this issue’s fork.