Change url sanitize behavior

How to configure and adapt Aimeos based shops as developer
Forum rules
Always add your Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
User avatar
niloofar
Posts: 16
Joined: 04 Apr 2021, 09:41

Change url sanitize behavior

Post by niloofar » 22 Aug 2021, 10:10

Hi!

The default language of my Aimeos shop is Persian.
The product URLs must look like this:

Code: Select all

shop/default/کت-مشکی/0
which can be achieved by setting the URL segment in the basic tab of products' edit page. If I enter this URL in the browser's address bar it works fine.

But the issue is, on the list page, the URL is converted to:

Code: Select all

 /shop/default/kt-mshky/0
Seems like the sanazite method converts non-Latin characters to Latin ones in the `url()` helper of the view .

How can I change the helper's behavior to make sure the Persian characters remain untouched?
Aimeos version: ~2021.07
Laravel version: ^8.40
PHP: 7.4.9

User avatar
aimeos
Administrator
Posts: 7881
Joined: 01 Jan 1970, 00:00

Re: Change url sanitize behavior

Post by aimeos » 22 Aug 2021, 11:00

You can overwrite the Str::slug() method with your own code:
https://github.com/aimeos/aimeos-core/b ... #L188-L196

Just add upfront:

Code: Select all

\Aimeos\MW\Str::method( 'slug', function( $str, string $lang = 'en', string $sep = '-' ) {
	// your own code
} );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
niloofar
Posts: 16
Joined: 04 Apr 2021, 09:41

Re: Change url sanitize behavior

Post by niloofar » 22 Aug 2021, 11:37

That works! Thanks a lot :)

So where should I actually add this code?
Is adding a new common decorator on the client side the best solution? I mean:

Code: Select all

 client/html/common/decorators/default

User avatar
aimeos
Administrator
Posts: 7881
Joined: 01 Jan 1970, 00:00

Re: Change url sanitize behavior

Post by aimeos » 22 Aug 2021, 11:46

No, you should do that very early in the bootstrapping process. If you are using Laravel, the AppServiceProvider is a good place:
https://github.com/aimeos/aimeos/blob/m ... hp#L25-L30
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
niloofar
Posts: 16
Joined: 04 Apr 2021, 09:41

Re: Change url sanitize behavior

Post by niloofar » 22 Aug 2021, 12:00

Thanks a million. :)

User avatar
niloofar
Posts: 16
Joined: 04 Apr 2021, 09:41

Re: Change url sanitize behavior

Post by niloofar » 22 Aug 2021, 12:37

I was thinking, we don't want the admin to become confused.
So why not save the sanitized URL part in the first place?

Is adding

Code: Select all

if (isset($data['product.url']) && $data['product.url'] != '') {
    $data['product.url'] = \Aimeos\MW\Str::slug( $data['product.url'] );
}
in admin/jqadm/src/Admin/JQAdm/Product/Standard.php:466 correct?
Aimeos version: ~2021.07
Laravel version: ^8.40
PHP: 7.4.9

User avatar
aimeos
Administrator
Posts: 7881
Joined: 01 Jan 1970, 00:00

Re: Change url sanitize behavior

Post by aimeos » 22 Aug 2021, 17:25

The sanitized URL is already stored immedately, either the provided value in the product.url field or using the sanitized label:
- https://github.com/aimeos/aimeos-core/b ... #L248-L263
- https://github.com/aimeos/aimeos-core/b ... d.php#L651
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
niloofar
Posts: 16
Joined: 04 Apr 2021, 09:41

Re: Change url sanitize behavior

Post by niloofar » 23 Aug 2021, 07:48

Suppose in the admin panel the URL segment is set to a phrase with whitespaces, for example: 'black dress'.
The phrase saved to the database is exactly 'black dress' because
\Aimeos\MW\Str::slug() is only called when there's no value in the URL segment input.

Then, on the client-side, the URL segment is passed through the slug method, which replaces whitespaces with dashes.

So we can see a product in lists, but clicking on it shows an `Unable to find product` error.

Maybe we could change the setUrl method of product item like this:

Code: Select all

public function setUrl( ?string $url ) : \Aimeos\MShop\Product\Item\Iface
{
	return $this->set( 'product.url', \Aimeos\MW\Str::slug($url));
}
Aimeos version: ~2021.07
Laravel version: ^8.40
PHP: 7.4.9

User avatar
aimeos
Administrator
Posts: 7881
Joined: 01 Jan 1970, 00:00

Re: Change url sanitize behavior

Post by aimeos » 23 Aug 2021, 08:06

Your are right, in that case the product.url is different from the used URL.
Can you create a PR with the required change?
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
niloofar
Posts: 16
Joined: 04 Apr 2021, 09:41

Re: Change url sanitize behavior

Post by niloofar » 23 Aug 2021, 12:47

Thanks for the merge.

When do you we'll get this change with composer update?
Aimeos version: ~2021.07
Laravel version: ^8.40
PHP: 7.4.9

Post Reply