Refactor alias and title increment logic in StringHelper#53
Refactor alias and title increment logic in StringHelper#53charvimehradu wants to merge 2 commits intojoomla-framework:3.x-devfrom
Conversation
|
Please have a look at the unit tests in Drone. This change makes them fail. That means your change wouldn't be backwards compatible and thus can't be accepted into 3.x-dev. |
|
|
||
| if (preg_match($rxSearch, $string, $matches)) { | ||
| $n = empty($n) ? ($matches[1] + 1) : $n; | ||
| $string = preg_replace($rxReplace, sprintf($rxReplace, $n), $string); |
There was a problem hiding this comment.
You'll get an error here, you are replacing the n (value number) in the replacement string "-%d" which is not a valid replacement string for the preg_replace function, because it doesn't have the required delimiter -.
Your code will work, and replace report-2025 with report-2025-2, how it works?
Because, when it gets the error I mentioned, it will result in an empty string
Then when the empty alias reaches Content.php it will enter here
if (trim($this->alias) == '') { $this->alias = $this->title; }
Now the alias is typical as the title, lets say the title is report 2025 (2)
This following method will make behave the alias to be a safe URL, i.e. it will remove spaces and brackets and replace them with hyphen so report 2025 (2) will be report-2025-2
$this->alias = ApplicationHelper::stringURLSafe($this->alias, $this->language);
There was a problem hiding this comment.
Thanks for pointing that out! Looking for some better solution. Will revert back soon!
Pull Request for Issue #45011
Summary of Changes
Modified the increment function in the StringHelper file with the same logic @chmst suggested. First, the function checks the format of the string by determining the style. If the default style is used, it checks if the string contains a number inside parentheses. If a number is present, it increments the number inside the parentheses. If no number is found, it appends (2) to the title. If the dash style is used, it checks if the string ends with a number preceded by a hyphen. If a number is present, it increments it. If no number is found, it appends -2 to the alias. I would appreciate any help or suggestions to ensure this solution works as expected across various use cases.
Testing Instructions
When merged with joomla-cms,
Example Test: Create an article that has
Title: August 2024
Alias: august-2024
Save as copy
Title: August 2024 (2)
Expected Alias: august-2024-2
This method supports two formats for string incrementing:
Title Format (Default Style): For titles, the increment will be handled inside parentheses.
"dog"becomes"dog (2)", and"dog (2)"becomes"dog (3)".(2)by default.Alias Format (Dash Style): For aliases, the increment will be handled with a hyphen (
-)."dog-2"becomes"dog-3".-2by default.alias_issue.2.mp4
Documentation Changes Required