Problem/Motivation

I cannot get a list of users that are registered to a node when my Entity Reference view is of type "User".

I would like to use this Entity Reference view to populate a dropdown on my content type to show a list of all users that have registered for that specific content ID. I'd like that field to be USER based, not REGISTRATION based.

Steps to reproduce

  1. Create a new view named Registered Users
  2. Under View Settings select "Show Users" sorted by unsorted. Save
  3. Click "Add" and select Entity Reference
  4. Under Format, Format: Entity Reference List, Settings, select the option for User: Name. Click Apply
  5. Under Advanced, Relationships, click Add
  6. Select User Registration (relate users to their registrations), Add and configure relationships, click "Require this relationship" and Apply
  7. No user names show. I believe this should however show all usernames that have registered for something

I am able to produce a Entity Reference view of type "Registration" and it does show a dropdown of usernames but that is when using "Show Registration" in step 2 above. I want this to work where step 2 above is "Show Users" so the actual user ID is the stored value on a custom field on my content type. Using "Show Registration" the registration ID is what is stored in my custom field.

I was able to create this type of view in Drupal 7, but cannot seem to recreate it with Drupal 9.

Comments

ladybug_3777 created an issue. See original summary.

john.oltman’s picture

Category: Bug report » Support request

Hi, I tried creating a similar view on D7 and saw a relationship named "Associated registration via the registration's author entity." Was that the one you are using? On D9/10 there is a core issue because the similar relationship is not created. To solve that until the issue is resolved, you need to install the Entity API contrib module and then add the following to a custom module:

/**
 * Implements hook_entity_type_alter().
 */
function my_module_entity_type_alter(array &$entity_types) {
  if (isset($entity_types['registration'])) {
    $entity_types['registration']->setHandlerClass('views_data', 'Drupal\entity\EntityViewsData');
  }
}

Then clear cache and it will create a relationship "Relate each Registration with a user_uid field set to the user" that you can then use in your Entity Reference listing. I was able to get a list of the users who had registered for a given event by using that relationship and adding Contextual Filters for Registration:Entity Type ID and Registration:Entity ID and using the "Provide a default value" option. For the default values I used "Host entity type ID from URL" and "Host Entity ID from URL" respectively.

Try that and see if that works for you, or post with more details so I can try and figure out what the D7 version of the view is doing. The relationship you tried to use for D9/10 is for host entities, if you had enabled the User content type to be a host entity (highly unlikely); normally it is used in a context in which the relationship is for a Node or Commerce Product Variation.

john.oltman’s picture

Status: Active » Postponed (maintainer needs more info)

  • john.oltman committed f9523075 on 3.0.x
    Issue #3361113: Add reverse relationships when Entity module is...
john.oltman’s picture

Version: 3.0.0-rc2 » 3.0.x-dev
Status: Postponed (maintainer needs more info) » Fixed

Since this is going to be a common need, I have added the suggested code to the Registration module. So all people need to do to get the extra relationships is install the Entity API module. https://www.drupal.org/project/entity

I'll document this in the README

john.oltman’s picture

Title: Entity Reference User View Issue » Entity Reference from Users to their Registrations
john.oltman’s picture

john.oltman’s picture

john.oltman’s picture

john.oltman’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.