Is it possible to add fields from the Host Entity to the User Registrations block?

I thought I could achieve this with a relationship, but it does not seem possible. I can only get a link to the Host Entity or the Host Entity ID. I can't use this field as a reference for a relationship to then pull in fields from the Host.

Specifically, I have an Event content type with a Date field, and I want this Date field to show up in the User Registrations block.

Thanks for making a great module btw!

Comments

nicoleannevella created an issue. See original summary.

john.oltman’s picture

The Host entity field in Views is a pseudo-field because the entity type of the target is unknown to the module, and thus cannot be used in a relationship. However, app builders know what the entity types are in their application, so you can extend your registration type with an "Event" entity reference field and use a presave hook in your custom code to set that extra field to the referencing Event when the registration is created. That field will then be available in views and you can use relationships to access fields on the Event and the Registration in the same listing. To add fields to your registration type, go to /admin/structure/registration-types/{machine_name_of_your_registration_type}/edit/fields. The code in the hook can use the host entity type ID and host entity ID on the registration to load the Event entity and update the registration entity with that reference. The hook_registration_presave() function in a .module would be a place you can do this.

john.oltman’s picture

Category: Feature request » Support request
ericgsmith’s picture

You can also define a relationship to the host entity without having to modify any existing data inside hook_views_data_alter.

For example, to relate a registration to a node you can use the following code:

function my_module_views_data_alter(&$data) {

$data['registration']['host_entity_node'] = [
    'relationship' => [
      'title' => t('Host entity: Node'),
      'label' => t('Host entity: Node'),
      'group' => t('Registration'),
      'help' => t('The node entity that is associated with the Registration'),
      'id' => 'standard',
      'base' => 'node_field_data',
      'entity type' => 'node',
      'base field' => 'nid',
      'relationship field' => 'entity_id',
    ],
  ];

}

There was a sub module in Drupal 7 that provided this and other functionality - @john.oltman would you be open to a patch that could determine what entity types have registrations and loop through them to add this information? Either as a sub module or as part of the existing views data alter.

Similar to https://git.drupalcode.org/project/registration/-/blob/7.x-2.x/modules/r...

john.oltman’s picture

Yes @eric, I would take a look at a patch. My only concern is registration fields are attached to specific bundles and not an entire entity type (unless someone adds one as a base field, which would be rare probably). I'd be interested to see how a patch would handle that. Maybe it's not a problem though. If you post a patch I would definitely try it out.

ericgsmith’s picture

Attached is a patch to enable the relation for any enabled entity type.

To test (assuming a registration type is setup)

From the view user_registrations add a new relationship. Each entity type with a registration bundle should be available, e.g for nodes there will be "Host Entity: Content"

As long as you leave the relationship optional, it shouldn't care that the registrations can be associated to multiple entity types.

ericgsmith’s picture

Status: Active » Needs review
john.oltman’s picture

Thanks @eric looks good, I will try this out locally within the next day or two and then most likely commit.

john.oltman’s picture

Status: Needs review » Fixed
john.oltman’s picture

Committed and will be in next beta release. That should be within the next week.

Status: Fixed » Closed (fixed)

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