My patterns (tokens) were not being honored and no matter what patterns or configuration options I tried the generated user name was always a random string. Upon troubleshooting I found that the call to load the pattern from configuration was always empty/blank.

I've created a patch that changes the way the pattern is loaded and this solves the issue as the pattern is now loaded and the names automatically generated as expected.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

quantumized created an issue. See original summary.

3CWebDev’s picture

johan.gant’s picture

Not totally sure about conventions here, but I struggled to have quantumized's patch apply alongside another, useful one: https://www.drupal.org/node/2918391 due to differences in patch formats.

If I reapply the patch above against 8.x-1.x with `git format-patch` then they both apply cleanly. I've uploaded the output to here, but it probably shouldn't be credited to me if it's committed into the next release.

superbole’s picture

Thank you very much!

This patch solved my problem!

wturrell’s picture

With either patch, the username is correctly set, but I get the following error:

Warning: Invalid argument supplied for foreach() in Drupal\auto_username\Form\AutoUsernameSettingsForm::autoUsernameCleanTokenValues() (line 436 of modules/contrib/auto_username/src/Form/AutoUsernameSettingsForm.php).
Drupal\auto_username\Form\AutoUsernameSettingsForm::autoUsernameCleanTokenValues('[user:mail]') (Line: 402)

autoUsernameCleanTokenValues() seems to be expecting an array but according to the stack trace is being passed the raw value of auto_username.setting.aun_pattern - "[user:mail]" - as a string… am I missing something obvious?

(core 8.5.5, php 7.1.18)

wturrell’s picture

[incorrect stuff removed]

wturrell’s picture

EDIT Completely misunderstood this, sorry. I misread #3 as suggesting the patches had been combined.
#2918391: Incorrect callback given to token replacement in autoUsernamePatternprocessor fixes the warning in autoUsernameCleanTokenValues() I mentioned in #5 (that error didn't show up immediately for me, hence my confusion.)

TL;DR: if you're using composer, you could do this:

        "patches": {
            "drupal/auto_username": {
                "Module does not load patern from configuration": "https://www.drupal.org/files/issues/2904513-fix-to-load-pattern-config-with-author-info.patch",
                "Incorrect callback given to token replacement in autoUsernamePatternprocessor": "https://www.drupal.org/files/issues/auto_username-2918391-2.patch"
            },

...
wturrell’s picture

Title: Module does not load patern from configuration » Module does not load pattern from configuration
Related issues: +#2918391: Incorrect callback given to token replacement in autoUsernamePatternprocessor

(Hide useless patch, add callback as related issue, fix typo in issue title)

andrezstar’s picture

#8 works fine

AaronBauman’s picture

Status: Active » Needs work
@@ -431,7 +433,7 @@ class AutoUsernameSettingsForm extends ConfigFormBase {
    *   An array of token replacements that need to be "cleaned".
    */
   public static function autoUsernameCleanTokenValues(&$replacements) {
-    foreach ($replacements as $token => $value) {
+    foreach ( (array) $replacements as $token => $value) {
       $replacements[$token] = self::autoUsernameCleanstring($value);
     }
   }

Edit: This change is unncessary and unrelated, given the fix in #2918391

AaronBauman’s picture

Status: Needs work » Needs review

Patch #3 is good to go.

AaronBauman’s picture

Zoltán Balogh’s picture

Status: Needs review » Needs work

Patch in #3 works well, but there is a warning when applying the patch:

git apply 2904513-fix-to-load-pattern-config-with-author-info.patch
2904513-fix-to-load-pattern-config-with-author-info.patch:22: trailing whitespace.

warning: 1 line adds whitespace errors.

AaronBauman’s picture

Priority: Normal » Major
Status: Needs work » Needs review
FileSize
663 bytes
Zoltán Balogh’s picture

Status: Needs review » Reviewed & tested by the community

Thank you, #14 works well.

lazyD’s picture

for me tokens were not getting loaded... did below and it worked for me... Hope this helps someone....
FileName -> AutoUsernameSettingsForm.php
Function -> autoUsernamePatternprocessor
- $pattern = \Drupal::state()->get('aun_pattern', ''); -> Remove this line
+ $pattern = \Drupal::config('auto_username.settings')->get('aun_pattern'); ->Add this line

Cheers.

dkosbob’s picture

FileSize
3.78 KB

Yeah, config wasn't getting loaded for anything. This patch expands on the patch from #14 to include the rest of the config calls.

dkosbob’s picture

Status: Reviewed & tested by the community » Needs review
GaëlG’s picture

The last patch in #2895214: Misuse the State API fixed the problem for me. Several problems, same cause?

thomasmurphy’s picture

Status: Needs review » Reviewed & tested by the community

#17 worked well for me.

thomasmurphy’s picture

Minor bug still after applying this patch: usernames are being updated on user accounts edits when this functionality is disabled in the module config.

thomasmurphy’s picture

Status: Reviewed & tested by the community » Needs work
jlscott’s picture

Status: Needs work » Fixed

Committed, with an additional fix for point made by @thomasmurphy.

Status: Fixed » Closed (fixed)

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