Problem/Motivation

Hi, I've used feeds to import users for a while, so I feel like I've got the process down pretty well, but today I tried to import users with roles in a CSV file and the feed is returning the error:
The role role_machine_name cannot be assigned because it does not exist.

Except, the role does exist. I copied the machine name of the role (I also tried the user-friendly name) and pasted it into the CSV column directly to make sure I wasn't misspelling it or anything. Under Mapping, I made sure the role I want is selected for the import process.
I'm not dealing with multiple roles per user, so I didn't setup a tamper.

Steps to reproduce

Create a CSV with username, email, roles, and other fields you want
Create a feed
- Processor: Import new users, Update existing users
Map the fields to the user roles
Attempt import
Note warning errors that the role doesn't exist.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork feeds-3441669

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

rex.barkdoll created an issue. See original summary.

ptmkenny’s picture

I am having the same issue. I will attempt to find the bad commit, but when I edit the mappings for Role, I can no longer select "id", only "UUID" or "User role weight".

ptmkenny’s picture

Version: 8.x-3.0-beta4 » 8.x-3.x-dev

In my exported config:

     map:
       target_id: roles
     settings:
-      reference_by: id
+      reference_by: uuid

So it looks like the id reference got removed, but when? I also confirmed this behavior in the latest dev; it is no longer possible to reference roles by machine_name, but that is the most convenient way to do so.

ptmkenny’s picture

Status: Active » Needs review

Ok, I found the cause of the bug. ConfigEntityReference.php is not checking for id if there are mapping definitions, only UUID.

I think everything in Drupal has an id and a UUID, so I modified the code to assume everything does and simplify the logic. But, I don't know if this is actually a safe assumption. Let's see what the tests say, and please review.

megachriz’s picture

The issue is actually that Drupal 10 apparently added some new data types for config schema. Data types 'machine_name' and 'required_label' are new.

Config schema for user role in Drupal 9:

user.role.*:
  type: config_entity
  label: 'User role settings'
  mapping:
    id:
      type: string
      label: 'ID'
    label:
      type: label
      label: 'Label'
    weight:
      type: integer
      label: 'User role weight'
    is_admin:
      type: boolean
      label: 'User is admin'
    permissions:
      type: sequence
      label: 'Permissions'
      orderby: value
      sequence:
        type: string
        label: 'Permission'

And in Drupal 10:

user.role.*:
  type: config_entity
  label: 'User role settings'
  mapping:
    id:
      type: machine_name
      label: 'ID'
    label:
      type: required_label
      label: 'Label'
    weight:
      type: integer
      label: 'User role weight'
    is_admin:
      type: boolean
      label: 'User is admin'
    permissions:
      type: sequence
      label: 'Permissions'
      orderby: value
      sequence:
        type: string
        label: 'Permission'

So changes in Drupal core broke this in Feeds.

megachriz’s picture

From https://www.drupal.org/node/3349638:

Note: There's no disruption for config schemas switching from type: label to type: required_label, because config schema validation is not yet being used.

Hm, that's not true for Feeds!

Besides machine_name, other newly introduced types don't seem to be significant for Feeds: https://www.drupal.org/list-changes/drupal/published?keywords_descriptio...

megachriz’s picture

@ptmkenny
A config entity doesn't always have an 'id' property, but it does have an 'uuid' property:

config_entity:
  type: mapping
  mapping:
    uuid:
      type: uuid
      label: 'UUID'
    langcode:
      type: langcode
    status:
      type: boolean
      label: 'Status'
    dependencies:
      type: config_dependencies
      label: 'Dependencies'
    third_party_settings:
      type: sequence
      label: 'Third party settings'
      sequence:
        type: '[%parent.%parent.%type].third_party.[%key]'
    _core:
      type: _core_config_info

So I reverted your changes in the MR.

ptmkenny’s picture

Status: Needs review » Reviewed & tested by the community

Thank you so much for the explanation and better solution.

I have a site that can be initialized and all of the content is imported from feeds, so I tested the updated MR there. All the roles were imported correctly, so marking RTBC.

  • MegaChriz committed 8b154da4 on 8.x-3.x authored by ptmkenny
    Issue #3441669 by MegaChriz, ptmkenny: Role is missing on user import
    
megachriz’s picture

Status: Reviewed & tested by the community » Fixed

Cool. Thanks for testing! I merged the code.

Status: Fixed » Closed (fixed)

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

ronraney’s picture

Hello - I think I have the same issue. I updated the module. I tried using the machine name and mapping to ID. In Devel, the id is the same as the machine name. That didn't work. I am now going to try mapping the UUID to the UUID. Is that the proposed solution once the module is updated?

megachriz’s picture

@rraney
When mapping to user roles, it makes the most sense to reference roles by ID or by label. But you could use UUID as well. When on Drupal 10, you need Feeds 8.x-3.0-beta5 or later to configure the role target in the UI.