Continuing on from #1627210: Handling when a host entity is unavailable.

Related entities should not be automatically loaded so we can save those precious milliseconds loading unneeded entities. If a related entity is required, code should make use of entity metadata wrappers, an awesome feature of Entity module. In addition, entities will not go stale.

This is an API change for registration, and any code depending on registration.module.

Before:

  $registration = entity_load_single('registration', 42);
  $host_entity = $registration->entity;
  $author = $registration->author;
  $state = $registration->state;
  $user = $registration->user;

After:

  $registration = entity_load_single('registration', 42);
  $wrapper = entity_metadata_wrapper('registration', $registration);
  $host_entity = $wrapper->entity->value();
  $author = $wrapper->author->value();
  $state = $wrapper->state->value();
  $user = $wrapper->user->value();

Comments

dpi’s picture

Title: Deprecate autoloaded entities in for Entity Metadata Wrappers » Deprecate autoloaded entities for Entity Metadata Wrappers
Status: Active » Needs review
StatusFileSize
new18.22 KB
mradcliffe’s picture

Status: Needs review » Needs work

I also added setters in #1571086: Match user accounts to existing registrations specifically for the anon_mail property. This should not use the default setter because entity_property_verbatim_set will not let you set the value to NULL for a user or e-mail property type. This is required for switching registrations from anonymous users.

Also, why did you create duplicate functions for essentially the same property? Author and user are both user objects except user can be empty. I know it's kind of standard practice in Java to do individual getter/setters, but it just seems like a waste of space to me. I tried to match what Registration was already doing with a registration_set_properties function, but that may need work.

I will merge what I have done with your style.

dpi’s picture

Author is always a Drupal user.

User is usually a Drupal user. Staying flexible for #1653458: Register an entity different than a user

mradcliffe’s picture

StatusFileSize
new18.96 KB

Here's a patch with some changes to get rid of the undefined variable issues from the previous patch.

mradcliffe’s picture

Status: Needs work » Needs review
StatusFileSize
new18.59 KB

Rebased and reformatted patch.

mradcliffe’s picture

Status: Needs review » Needs work

The mail / user properties do not seem to work properly currently. I see anon_mail and user_uid still referenced too.

mradcliffe’s picture

StatusFileSize
new18.62 KB

Found the issue with user_set. I was not using EntityDrupalWrapper::value.

dpi’s picture

Status: Needs work » Needs review
andyh69uk’s picture

Patched applied but now I can't access the user, therefore can't access the user email address, I am doing something wrong?

dpi’s picture

From memory: $wrapper->user->mail->value();

andyh69uk’s picture

Sorry, I mean from within Rules, I can't access user

andyh69uk’s picture

Ignore that, I can fetch the user using the userid

andyh69uk’s picture

I have used this patch to help with an issue I posted here http://drupal.org/node/1719192 but this seems to have altered what you can and can't do in Views. I now can't add a relationship between the registration and the user, which is a show stopper because that means you can't access the email or username to generate lists of registrations

mradcliffe’s picture

Status: Needs review » Needs work
StatusFileSize
new19.38 KB

The registration state relationship is also broken.

I also found some more issues with registration state as a registration_state type. You can't use Views Bulk Operations "Modify entity values" because it's outside of its allowed types.

I'm not sure what other views handlers need overriding from Entity Metadata than state and user_uid, but I included those in an updated patch.

(whine) I'm so close with this patch and getting it to work with commerce registration in order to create user accounts and update the registration with the proper values, but the registration state setter or saving or whatever is getting in the way of it working. I'm probably going to have to assume that all registrations are "complete" and pray that nothing goes wrong. :(

andyh69uk’s picture

Status: Needs work » Needs review

Patch applied but adding User relationship in Views throws SQL errors thewalkdown.user (thewalkdown is the name of my site) table doesn't exist. I got around this problem by using Author, which isn't ideal but as I am changing the author in Rules if the registration is submitted by a different user I am pretty certain the Author & User should be the same.

I also had the same problem with state, in the end I added a custom field to store the state and protected it using Field Permissions then so that the user could see the state of the registration I have a hidden text field using Field Hidden which I set using Rules and then make it visible on Manage Display. Very long winded but it works, just means the Manage Registrations display isn't much use, I just created another View.

mradcliffe’s picture

StatusFileSize
new19.38 KB

Oh, that's a typo. It should be the users table. Sorry.

bschilt’s picture

UPDATE: Patch in #16 works fine.

After installing patch in #16 I get the following error when viewing a node that is register-able:

EntityMetadataWrapperException: Unknown data property user. in EntityStructureWrapper->getPropertyInfo() (line 339 of <drupal-root>/sites/all/modules/contrib/entity/includes/entity.wrapper.inc).

I'm not sure which property its trying to access.

Also the updated views integration to make the relationship to the user table is missing the title and help properties. So in the Views UI its displaying "missing title" and "missing help". The relationship works fine though.

bschilt’s picture

The property that is generating the error from #17 is the 'user' property.

dpi’s picture

Waiting on levelos et al before this is forward ported.

bschilt’s picture

Status: Needs review » Reviewed & tested by the community

I have applied the patch in #16 and it works great.

I did however run into a bug with the UUID module where it would cause the entities to throw the error in #17. I disabled the UUID module and it works well.

The latest dev branch of UUID will fix the issue: #1736780: EntityMetadataWrapperException Error - Conflict with Drupal commerce

levelos’s picture

Status: Reviewed & tested by the community » Needs work

Hey gang - Finally getting around to this, sorry for the holdup. I did find a couple of initial issues in testing:

  • The relationship to registration states does not work. There's no way in Views to display the state label, which leaves us with the serial ID which we already had access to.
  • The mail getter only returns the anonymous email, unlike the Registration::Mail() method which returns the canonical email for the registration, taking into account whether it's for an anon or authenticated registrant. I think the property getter should do the same so you could, for example, use VBO to send an email to all registrants, etc.

@dpi, you mention a forward port. The patch in #16 does apply cleanly. What were you referring to?

Finally, thanks for all the help pushing this forward!

levelos’s picture

Status: Needs work » Fixed

Eh, correct that. Point 1, the state relationship works fine, my bad on that one. Everything else runs clean and I committed the change.

The second issue still stands and needs to be addressed by an additional views data handler for the mail property.

dpi’s picture

Awesome stuff.

you mention a forward port.

I mean update patch to head, and fix any outstanding bugs. But waiting on activity before other issues get ahead. Its fine now.

kevinquillen’s picture

milliseconds

I can confirm that doing a entity load of a registration while migrating 23k or so registrations into Commerce Order / (Commerce) Registrations is more than mere milliseconds. If we can chop that time that would be awesome.

Status: Fixed » Closed (fixed)

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

  • levelos committed b090e33 on 7.x-1.x, panels, any-entity, slots, integrations, hold_state authored by dpi
    #1664218 by dpi and mradcliffe: Deprecate autoloaded entities for Entity...