Spin off from #2002162: Convert form validation of users to entity validation: the definition of the roles field on user entities should use the entity_reference_field type, because it references the Role config entity.

Unfortunately this uncovers lots of bugs in entity reference handling. Now it works.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

klausi’s picture

Status: Active » Needs review
FileSize
23.64 KB

Latest patch from #2002162: Convert form validation of users to entity validation, we should split out the relevant changes for user roles.

Status: Needs review » Needs work

The last submitted patch, urser-roles-ref-2044859.patch, failed testing.

amateescu’s picture

Component: typed data system » user.module
Issue summary: View changes
Status: Needs work » Closed (won't fix)

"Roles" implies multiple values and base fields cannot have multiple values.

jibran’s picture

base fields cannot have multiple values.

Do we have to create new issue for this? Or is there any existing issue? If we can't convert roles to reference field then what is the purpose of #1757452: Support config entities in entity reference fields please explain.

amateescu’s picture

We don't have to create a new issue, this is simply not possible :)

The purpose of the issue you linked is to be able to reference bundles, for example. Roles are not bundles for the user entity type..

jibran’s picture

Okay. Thanks for the clarification.

fago’s picture

Status: Closed (won't fix) » Active

"Roles" implies multiple values and base fields cannot have multiple values.

Not necessarily.

Our general, default storage approach handles single valued base fields only, that doesn't mean it could work with multiple valued base fields though. It works, but the storage controller would have to take care of loading/saving the values as appropriate (what I guess it does already).

I think, that's best solved by extending the configurable entity reference class to a UserRolesReference class which then implements the respective insert/save logic. Loading is more problematic though, as there is no respective method to handle multiple loading on the field. One variant would be to use getValue() to lazily load + return the roles for caching, what would work fine for cached entities but single-load on cache fails.
Alternatively, we'd have to deal with loading (or evering) in the respective entity class method.

amateescu’s picture

Not to mention EntityQuery..

I mean, of course it's doable if you bend the system in every possible way, but do we really want to go there?

Damien Tournoud’s picture

Also, why do we need this one to be a base field?

fago’s picture

I figured, moving it's storage logic into the field isn't related to this at all - thus should be its own issue.

But moreover being a multi-valued base field - it already is.

I mean, of course it's doable if you bend the system in every possible way, but do we really want to go there?

Not sure why it would bend it if we use an existing configurable entity reference field type for a configurable entity reference.

EntityQuery

Yes, afaik it's not queryable right now - no changes here?

Also, why do we need this one to be a base field?

Not sure what it should be else? It's provided with the user entity, so it's a base field.

amateescu’s picture

But moreover being a multi-valued base field - it already is.

So.. what part of this definition makes it a multi-valued base field?

    $properties['roles'] = array(
      'label' => t('Roles'),
      'description' => t('The roles the user has.'),
      // @todo Convert this to entity_reference_field, see
      // https://drupal.org/node/2044859
      'type' => 'string_field',
    );

Like I said above, the fact that we split a string in the user storage controller to make it *look* like it contains actual references doesn't make it a multi-valued field. Edit: Removed snarky remark :)

Damien Tournoud’s picture

Am I missing something? This has no reason to stay a crappy semi-field. Why not making it a full blown configurable field and be done with it?

andypost’s picture

User roles are converted to ER in #2192259-29: Remove ConfigField.. Item and List classes + interfaces
So probably this one could be closed after commit

amateescu’s picture

I don't think user roles should be converted to ER in [#8557525].

msonnabaum’s picture

I'd like to see this revived, mainly because I think getRoles() should be returning proper role objects rather than strings.

This leads to issues like we have in hasPermission on User and UserSession, where they both have to convert the roles themselves to find the permission:


   $roles = \Drupal::entityManager()->getStorage('user_role')->loadMultiple($this->getRoles());

plach’s picture

Issue tags: +beta deadline

I doubt we will be allowed to do this after beta...

robcolburn’s picture

@andypost - Looks like #8557525 landed, which you explained could accomplish this issue. Are we at least closer now?

andypost’s picture

There's issue #8557525, and roles still string with some complicated logic behind.
I'm +1 to #12 - configurable field like a signature

Berdir’s picture

#2337927: SqlContentEntityStorage::onFieldStorageDefinition(Create|Update|Delete)() is broken for base fields will provide support for multi-value base fields and I already tried to convert user roles storage in #2248977: Complete support for multi-value base fields in ContentEntitySchemaHandler and use it for the user.roles field, which I will pick up when the first issue lands.

Note that converting to an entity reference field itself would be easy at this point, entity reference supports config entities, the only difference would be that ->value would now be ->target_id. The interesting part is converting custom storage to default storage, string or entity_reference.

Berdir’s picture

Status: Active » Needs review
FileSize
1.1 KB

New patch posted in #2248977: Complete support for multi-value base fields in ContentEntitySchemaHandler and use it for the user.roles field, please review.

To clarify what I meant above, "converting to entity reference field" would be this patch.

Status: Needs review » Needs work

The last submitted patch, 20: user-roles-entity-reference-2044859-20.patch, failed testing.

Berdir’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: +Entity Field API, +Entity validation
FileSize
4.93 KB

I always forget the target_type setting. Fixed the tests.

catch’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Me too. Committed e3e12db and pushed to 8.0.x. Thanks!

  • alexpott committed e3e12db on 8.0.x
    Issue #2044859 by Berdir, klausi: Convert user roles to...
Wim Leers’s picture

Nice!

yched’s picture

Awesome !

Status: Fixed » Closed (fixed)

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