Skip to content

Commit cb1460c

Browse files
committed
Document slugify
1 parent c702492 commit cb1460c

File tree

6 files changed

+116
-0
lines changed

6 files changed

+116
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<parameters>
8+
<parameter key="shopware.slug.config" type="collection">
9+
<parameter key="regexp">/([^A-Za-z0-9\.&amp;]|-)+/</parameter>
10+
<parameter key="lowercase">false</parameter>
11+
</parameter>
12+
</parameters>
13+
</container>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace SwagCustomSlugConfig;
4+
5+
use Shopware\Components\Plugin;
6+
7+
class SwagCustomSlugConfig extends Plugin
8+
{
9+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
8+
<services>
9+
<service id="SwagCustomSlugService.slug"
10+
class="SwagCustomSlugService\SlugService"
11+
decorates="shopware.slug">
12+
<argument type="service" id="SwagCustomSlugService.slug.inner" />
13+
</service>
14+
</services>
15+
</container>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace SwagCustomSlugService;
4+
5+
use Shopware\Components\Slug\SlugInterface;
6+
7+
class SlugService implements SlugInterface
8+
{
9+
/**
10+
* @var SlugInterface
11+
*/
12+
private $coreSlugService;
13+
14+
/**
15+
* @param SlugInterface $core
16+
*/
17+
public function __construct(SlugInterface $core)
18+
{
19+
$this->coreSlugService = $core;
20+
}
21+
22+
/**
23+
* Return a URL safe version of a string.
24+
*
25+
* @param string $string
26+
* @param string|null $separator
27+
*
28+
* @return string
29+
*/
30+
public function slugify($string, $separator = null)
31+
{
32+
$string = html_entity_decode($string);
33+
34+
return $this->coreSlugService->slugify($string, $separator);
35+
}
36+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace SwagCustomSlugService;
4+
5+
use Shopware\Components\Plugin;
6+
7+
class SwagCustomSlugService extends Plugin
8+
{
9+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
layout: default
3+
title: Create custom url slugger
4+
github_link: developers-guide/plugin-slugify/index.md
5+
indexed: true
6+
group: Developer Guides
7+
subgroup: Tutorials
8+
menu_title: Create custom url slugger
9+
menu_order: 35
10+
shopware_version: 5.2.5
11+
---
12+
13+
From Shopware 5.2.5 we implemented the slugify-Framework. Slugify rewrites special characters like ñ, Ñ, ¿, é or Ó to n,n,-,e or o. If you named your article "tomàtiga de ramellet" the URL will be created like this "tomatiga-de-ramellet". Please note the slash will not be rewritten. That means that the article name „tomàtiga de ramellet / ecológica“ will be rewritten to "tomatiga-de-ramellet/ecologica".
14+
15+
## Configure slugify ruleset
16+
17+
By default, the slugify framework is defined with its default settings.
18+
19+
As part of the implementation in Shopware, you can overwrite the parameters in the dependency injection container by creating a `Resources/services.xml` file in your plugin.
20+
21+
```xml
22+
<parameter key="shopware.slug.config" type="collection">
23+
<parameter key="regexp">/([^A-Za-z0-9\.]|-)+/</parameter>
24+
<parameter key="lowercase">false</parameter>
25+
</parameter>
26+
```
27+
28+
You can download an example plugin with this changes <a href="{{ site.url }}/exampleplugins/SwagCustomSlugConfig.zip">here</a>.
29+
30+
## Decorate the slugify service
31+
32+
Another approach could be to decorate the existing service and implement your own logic using the `Shopware\Components\Slug\SlugInterface`.
33+
34+
You can download an example plugin with this changes <a href="{{ site.url }}/exampleplugins/SwagCustomSlugService.zip">here</a>.

0 commit comments

Comments
 (0)