Skip to content

Commit 27acd72

Browse files
author
Florian Merle
committed
feature: add bitbag_cms_render_link_for_code and bitbag_cms_get_link_for_code twig function
1 parent b8bb133 commit 27acd72

4 files changed

Lines changed: 132 additions & 0 deletions

File tree

doc/pages.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,37 @@ Let's assume you associated pages to specific products. You can render them grou
2929
Twig function. This is where `nameWhenLinked` and `descriptionWhenLinked` fields are used. If you associate pages to
3030
specific sections, they will be displayed in columns titled with section name.
3131

32+
### Render link to page from its code
33+
34+
If you want to create a link to a page from its code, you can do either with `bitbag_cms_render_link_for_code` or `bitbag_cms_get_link_for_code` twig functions. These functions will automatically generate a link with the correct locale.
35+
36+
You can define attributes to customize the tag. You can also customize the displayed name (by default the function will try to display the name when linked, if it is not defined, it will fallback to the page name).
37+
38+
If you only need the link, you can use `bitbag_cms_get_link_for_code`.
39+
40+
You can display a message if the page wasn't found with the `notFoundMessage` option.
41+
42+
```twig
43+
{{ bitbag_cms_render_link_for_code('code') }}
44+
{{ bitbag_cms_render_link_for_code('code', { attr: { class: 'ui button' }, name: 'Custom name' }) }}
45+
{{ bitbag_cms_render_link_for_code('code', {}, 'custom/template.html.twig') }}
46+
{{ bitbag_cms_get_link_for_code('code') }}
47+
{{ bitbag_cms_render_link_for_code('wrong-code', { notFoundMessage: 'Page not found' }) }}
48+
{{ bitbag_cms_get_link_for_code('wrong-code', { notFoundMessage: 'Page not found' }) }}
49+
50+
```
51+
52+
Will render:
53+
54+
```html
55+
<a href="/{_locale}/pages/{slug}">Name when linked</a>
56+
<a href="/{_locale}/pages/{slug}" class="ui button">Custom name</a>
57+
<!-- depends on custom/template.html.twig -->
58+
/{_locale}/pages/{slug}
59+
Page not found
60+
Page not found
61+
```
62+
3263
## Customization
3364

3465
If you don't know how to override templates yet,

src/Resources/config/services/twig.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ parameters:
22
bitbag_cms.twig.admin_functions:
33
- bitbag_cms_render_block
44
- bitbag_cms_render_media
5+
bitbag_cms.twig.link_template: "@@BitBagSyliusCmsPlugin/Page/link.html.twig"
56

67
services:
78
bitbag_sylius_cms_plugin.twig.extension.block:
@@ -42,3 +43,13 @@ services:
4243
arguments:
4344
- "@twig"
4445
- "%bitbag_cms.twig.admin_functions%"
46+
47+
bitbag_sylius_cms_plugin.twig.extension.render_link:
48+
class: BitBag\SyliusCmsPlugin\Twig\Extension\RenderLinkExtension
49+
arguments:
50+
- "@sylius.context.locale"
51+
- "@bitbag_sylius_cms_plugin.repository.page"
52+
- "@router.default"
53+
- "%bitbag_cms.twig.link_template%"
54+
tags:
55+
- { name: twig.extension }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% if page != null %}
2+
<a href="{{ path('bitbag_sylius_cms_plugin_shop_page_show', { slug: page.slug }) }}"
3+
{% if options.attr is defined %}
4+
{% for attrname, attrvalue in options.attr %}
5+
{{ attrname }}="{{ attrvalue }}"
6+
{% endfor %}
7+
{% endif %}
8+
>
9+
{{ options.name|default(page.nameWhenLinked)|default(page.name) }}
10+
</a>
11+
{% else %}
12+
{{ options.notFoundMessage|default('') }}
13+
{% endif %}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/*
4+
* Created by Florian Merle - Dedi Agency <florian.merle@dedi-agency.com> <florian.david.merle@gmail.com>
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace BitBag\SyliusCmsPlugin\Twig\Extension;
10+
11+
use BitBag\SyliusCmsPlugin\Repository\PageRepositoryInterface;
12+
use Sylius\Component\Locale\Context\LocaleContextInterface;
13+
use Symfony\Component\Routing\RouterInterface;
14+
15+
class RenderLinkExtension extends \Twig_Extension
16+
{
17+
/** @var LocaleContextInterface */
18+
private $localeContext;
19+
20+
/** @var PageRepositoryInterface */
21+
private $pageRepository;
22+
23+
/** @var RouterInterface */
24+
private $router;
25+
26+
/** @var string */
27+
private $defaultTemplate;
28+
29+
public function __construct(
30+
LocaleContextInterface $localeContext,
31+
PageRepositoryInterface $pageRepository,
32+
RouterInterface $router,
33+
string $defaultTemplate
34+
) {
35+
$this->localeContext = $localeContext;
36+
$this->pageRepository = $pageRepository;
37+
$this->router = $router;
38+
$this->defaultTemplate = $defaultTemplate;
39+
}
40+
41+
public function getFunctions()
42+
{
43+
return [
44+
new \Twig_Function('bitbag_cms_render_link_for_code', [$this, 'renderLinkForCode'], [
45+
'needs_environment' => true,
46+
'is_safe' => ['html']
47+
]),
48+
new \Twig_Function('bitbag_cms_get_link_for_code', [$this, 'getLinkForCode']),
49+
];
50+
}
51+
52+
public function renderLinkForCode(
53+
\Twig_Environment $environment,
54+
string $code,
55+
array $options = [],
56+
?string $template = null
57+
): string {
58+
$page = $this->pageRepository->findOneEnabledByCode($code, $this->localeContext->getLocaleCode());
59+
60+
return $environment->render($template ?? $this->defaultTemplate, [
61+
'page' => $page,
62+
'options' => $options,
63+
]);
64+
}
65+
66+
public function getLinkForCode(
67+
string $code,
68+
array $options = []
69+
): string {
70+
$page = $this->pageRepository->findOneEnabledByCode($code, $this->localeContext->getLocaleCode());
71+
if (!$page) {
72+
return isset($options['notFoundMessage']) ? $options['notFoundMessage'] : '';
73+
}
74+
75+
return $this->router->generate('bitbag_sylius_cms_plugin_shop_page_show', ['slug' => $page->getSlug()]);
76+
}
77+
}

0 commit comments

Comments
 (0)