RoleAssign with User CSV Import form alter doesn't work

Created on 31 August 2023, about 1 year ago
Updated 18 October 2023, about 1 year ago

Problem/Motivation

The RoleAssign with User CSV Import submodule should alter the User CSV Import import form to restrict the available options to the ones set by the RoleAssign module.

Currently with User CSV Import, RoleAssign and RoleAssign with User CSV Import modules enabled and configured, the User CSV Import import form is not showing any role options and the upload is failing because a role option is not set.

This is happening because:

  • The configuration settings from the RoleAssign are outputting an indexed array.
  • The role options array within the User CSV Import import form is an associative array.
  • The current code is outputting an empty array when trying to intersect these arrays.
  if ($form_id == 'user_csv_import_form') {
    // limit the visible roles to just those saved in roleassign.settings
    $form['config_options']['roles']['#options'] =
      array_intersect_key(
        $form['config_options']['roles']['#options'],
        array_filter(\Drupal::config('roleassign.settings')->get('roleassign_roles')
      )
    );
  }

Steps to reproduce

  1. Install, enable and configure:
    • "drupal/roleassign": "^2.0"
    • "drupal/user_csv_import": "^2.0"
  2. Install and configure RoleAssign with User CSV Import
  3. .

  4. Go to /admin/people/import and attempt to upload a CSV.

Proposed resolution

Fix the logic (and add some extra conditional logic) as follows:

if ($form_id == 'user_csv_import_form') {
      $assignable_roles = array_filter(\Drupal::config('roleassign.settings')->get('roleassign_roles'));
      if ((!empty($assignable_roles) && is_array($assignable_roles))) {
          $form['config_options']['roles']['#options'] =
              array_intersect_key(
                  $form['config_options']['roles']['#options'],
                  array_flip($assignable_roles)
              );
      }
}

Remaining tasks

Do it.

User interface changes

Role options shall display.

πŸ› Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States iarla Providence, RI

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024