Problem/Motivation

Unless a user already exists in a site import, which precludes the use case of installing content into an empty database, the UID is unknown at the time of content definition. Support needs to be added to reference users for author assignment using identifiers known or assignable at the time of defining content in the import files.

Proposed resolution

Add an event subscriber to process Node imports and assign authors with additional processing to expand support of identifiers to the following:

  • UID
  • UUID
  • User Name

API changes

Nodes in import content may be assigned authors by defining a UID, UUID, or user name in the Node's uid attribute using the following format:

- entity: node
  type: article
  uid: <user_id>|<uuid>|<username>
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jasonawant created an issue. See original summary.

slucero’s picture

Version: 8.x-2.x-dev » 8.x-1.x-dev

Changing this to target v1.x since that's the currently supported version.

slucero’s picture

Currently this cannot be achieved since processing within the entity is only triggered at the field level after properties have been assigned. These properties are filtered out within the ContentLoader::buildEntity() method where each content key starting with field is filtered out.

This feature could be implemented through a couple of methods:

Option A: Special UID Handling

During the entity build process, the uid property is specifically checked for whether it contains a string or a numeric value. If it contains a string value we assume it's the username and use that to lookup and replace the UID value.

Option B: Extend Processing Support to Properties

Rather than only checking content keys matching field* for preprocessing, this could be extended to all properties. This adds some complexity in the preprocessing options since most properties have to be assigned a raw value instead of an array containing a value or target_id like other field values, but the added flexibility could be valuable since not all fields are guaranteed to follow the field_* naming scheme if they weren't created through the Field UI.

Moving Forward

Overall, Option A is definitely the quicker route to this functionality, and it accounts for a common enough use case to likely make it worthwhile. Option B, however, offers more flexibility to the processing logic that should probably be added in at some point in the future either way.

bjlewis2’s picture

Following this because I was just trying to figure out how to do this.

My use case:
I'm creating a group using the "group" module (which works fine), but with that module, the creator has the "Admin" role within the group. So, since it's created by "Anonymous", as user1 I don't have access/permission in the group. I can join the group, and make it work that way, but that adds manual steps to an otherwise automated process.

My 2 cents:
I'd like to see Option A implemented personally (since it covers the basic use-case, and sounds like it'd be quicker/simpler). Maybe Option B would be a part of the 2.x version of the module.

Thanks again for the great module!

slucero’s picture

Issue summary: View changes
Status: Active » Needs review
Issue tags: +Needs tests
FileSize
6.42 KB

In the attached patch I've added a new event subscriber to process the uid property of Nodes being imported if the user is not successfully, such as in the case that a numeric user Id is not provided. This event subscriber then adds processing to identity a user for assignment as the Node's author by first checking if a valid UUID was provided and then searching for the user name if not.

/**
 * Authors for content being imported may be defined using the account username
 * following this format:
 *
 * @code
 * - entity: node
 *   type: article
 *   uid: <user_id>|<uuid>|<username>
 * @code
 */
slucero’s picture

Work in this is directly related to and could benefit from the refactoring proposed in #2949625: Move Entity Loading and Lookup into Helper Service.

marvil07’s picture

Quite useful extension, thanks!

It is not applying anymore, here a re-roll for current 8.x-1.x, at 8.x-1.0-alpha9.

marvil07’s picture

Here an alternative patch, generalizing to apply it not only to Node entities, but any EntityOwnerInterface.
This is useful on entities like profile module entities, that are not nodes but have ownership.
Additionally, an interdiff, via git for easier read on the rename.