Hi and thanks for this module,

I have a special use case where I must reproduce aliases from an existing site exactly.

Among other problems, I have to keep the number of spaces, so the number of separators, equal from one site to the other.

For example, the string "This is a special string with two spaces here." should translate to :

this-is-a-special-string-with-two-spaces--here (note the two dashes)

... and not to :

this-is-a-special-string-with-two-spaces-here (one dash only)

I'm using the transliteration module, because french accentuated characters must be managed.

A simple hint to a line I must change in the code would do.

Thanks in advance.

Issue fork pathauto-2218457

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

Countzero’s picture

I tried to change line 209 in pathauto.inc from

$output = preg_replace('/\s+/', $cache['separator'], $output);

to various things like

$output = preg_replace('/\s/', $cache['separator'], $output);

or

$output = str_replace(' ', $cache['separator'], $output);

... but it didn't change anything. I guess spaces are replaced in some other place, but can't find where as of now.

dave reid’s picture

I have a special use case where I must reproduce aliases from an existing site exactly.

This isn't a very good fit for using Pathauto - why not just manually set aliases for your content if you need an exact match?

Countzero’s picture

Because I have tenths of thousands of pages which will turn into nodes when migrated.

I think Pathauto is a blessing in this case. It's just the spaces to space thing which bugs me.

Countzero’s picture

Let me try to explain better what I'm trying to do :

- pathauto is perfect for my use case, because of the bulk update feature which lets me update my nodes' paths in minutes.
- The only thing I want is changing the default behaviour, which replaces multiple spaces with only one.
- For my purpose, I'll use a modified version of pathauto, just for the time of the migration.
- I don't understand why the modification I mentioned above doesn't change the behaviour of the module.

So, if someone could just point me to the correct lines of code in the module which I should modify to achieve this (or explain the basic workflow used, or what I miss), it would be a perfect solution for me.

Thanks in advance

kksandr’s picture

Version: 7.x-1.2 » 8.x-1.x-dev
Assigned: Unassigned » kksandr
Category: Support request » Feature request
Issue tags: -spaces

kksandr’s picture

Assigned: kksandr » Unassigned
Status: Active » Needs work
Issue tags: +Needs tests

I came across this request several times, maybe someone else needs it, so I suggest introducing an option for this.

mably’s picture

mably’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests

Pathauto: Preserve consecutive spaces feature

These two commits introduce and refine a Pathauto setting that controls whether
consecutive whitespace characters are collapsed into a single separator or each
individually replaced, preserving the spacing in the resulting alias.

Commit 1 — Added a setting to keep number of spaces

Introduces the core feature (by contributor kksandr): a new setting that, when
enabled, replaces each whitespace character individually with the separator instead
of collapsing all consecutive whitespace into one. For example, with -
as separator, "foo bar" becomes "foo--bar"
instead of the default "foo-bar".

  • AliasCleaner.php: adds a preserve_consecutive_spaces
    cache key read from config; when enabled, uses /\s/ (single whitespace)
    instead of /\s+/ (one or more) for replacement.
  • PathautoSettingsForm.php: adds a checkbox to the admin settings form.
  • pathauto.schema.yml: declares the config key as boolean.
  • pathauto.settings.yml: sets the default to FALSE
    (existing behavior preserved).

Commit 2 — Rename and add test coverage

Renames keep_number_of_spaces to preserve_consecutive_spaces
for consistency with preserve_consecutive_separators, improves the form
label and description, adds an update hook, and provides missing test coverage.

  • All 4 source files: rename the config key throughout (schema,
    install defaults, AliasCleaner, settings form).
  • PathautoSettingsForm.php: title changed to
    "Preserve consecutive spaces"; description now explains the concrete effect
    ("foo bar" → "foo--bar").
  • pathauto.install: adds pathauto_update_10001()
    to initialize the setting to FALSE for existing sites.
  • PathautoSettingsFormWebTest: adds
    preserve_consecutive_spaces to $defaultFormValues and
    asserts the checkbox is unchecked by default.
  • PathautoKernelTest: adds
    testCleanStringWithPreserveConsecutiveSpaces() verifying:
    • "foo bar""foo--bar" (two spaces → two separators)
    • "foo bar""foo---bar" (three spaces → three separators)
    • "foo bar""foo-bar" (single space unchanged)
    • Mixed whitespace (space + tab + space) → three separators
mably’s picture

Assigned: Unassigned » berdir