Skip to content

Commit 67fa724

Browse files
author
frne
committed
Optimization of the frontend link button in SonataAdmin
- Added translation - Shortened caption and added title - Routes can now also be linked to the frontend - Improved exception handling
1 parent 25358a9 commit 67fa724

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

Admin/Extension/FrontendLinkExtension.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
use Knp\Menu\ItemInterface as MenuItemInterface;
1515
use Sonata\AdminBundle\Admin\AdminExtension;
1616
use Sonata\AdminBundle\Admin\AdminInterface;
17-
use Sonata\AdminBundle\Form\FormMapper;
17+
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
1818
use Symfony\Cmf\Component\Routing\RouteReferrersReadInterface;
1919
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
20+
use Symfony\Component\Routing\Exception\ExceptionInterface as RoutingExceptionInterface;
21+
use Symfony\Component\Routing\Route;
2022
use Symfony\Component\Routing\RouterInterface;
2123

2224
/**
@@ -32,12 +34,19 @@ class FrontendLinkExtension extends AdminExtension
3234
*/
3335
private $router;
3436

37+
/**
38+
* @var Translator
39+
*/
40+
private $translator;
41+
3542
/**
3643
* @param RouterInterface $router
44+
* @param Translator $translator
3745
*/
38-
public function __construct(RouterInterface $router)
46+
public function __construct(RouterInterface $router, Translator $translator)
3947
{
4048
$this->router = $router;
49+
$this->translator = $translator;
4150
}
4251

4352
public function configureTabMenu(
@@ -50,18 +59,32 @@ public function configureTabMenu(
5059
return;
5160
}
5261

53-
if (!$subject instanceof RouteReferrersReadInterface) {
62+
if (!$subject instanceof RouteReferrersReadInterface && !$subject instanceof Route) {
5463
throw new InvalidConfigurationException(
5564
sprintf(
56-
'%s can only be used on subjects which implement Symfony\Cmf\Component\Routing\RouteReferrersReadInterface!',
65+
'%s can only be used on subjects which implement Symfony\Cmf\Component\Routing\RouteReferrersReadInterface or Symfony\Component\Routing\Route!',
5766
__CLASS__
5867
)
5968
);
6069
}
6170

62-
$uri = $this->router->generate($subject);
71+
try {
72+
$uri = $this->router->generate($subject);
73+
} catch (RoutingExceptionInterface $e) {
74+
// we have no valid route
75+
return;
76+
}
6377

64-
$menu->addChild('Open in frontend', array('uri' => $uri, 'linkAttributes' => array('target' => '_blank')));
78+
$menu->addChild(
79+
$this->translator->trans('admin.menu_frontend_link_caption', array(), 'CmfRoutingBundle'),
80+
array(
81+
'uri' => $uri,
82+
'linkAttributes' => array(
83+
'target' => '_blank',
84+
'title' => $this->translator->trans('admin.menu_frontend_link_title', array(), 'CmfRoutingBundle')
85+
)
86+
)
87+
);
6588
}
6689

6790
}

Resources/config/admin-phpcr.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<service id="cmf_routing.admin_extension.frontend_link" class="%cmf_routing.admin_extension.frontend_link.class%">
6060
<tag name="sonata.admin.extension"/>
6161
<argument type="service" id="cmf_routing.router"/>
62+
<argument type="service" id="translator"/>
6263
</service>
6364

6465
</services>

Resources/translations/CmfRoutingBundle.de.xliff

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@
132132
]]>
133133
</target>
134134
</trans-unit>
135+
<trans-unit id="admin.menu_frontend_link_caption">
136+
<source>admin.menu_frontend_link_caption</source>
137+
<target>Frontend</target>
138+
</trans-unit>
139+
<trans-unit id="admin.menu_frontend_link_title">
140+
<source>admin.menu_frontend_link_title</source>
141+
<target>Frontend-Ansicht in neuem Tab öffnen</target>
142+
</trans-unit>
135143
</body>
136144
</file>
137145
</xliff>

Resources/translations/CmfRoutingBundle.en.xliff

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@
132132
]]>
133133
</target>
134134
</trans-unit>
135+
<trans-unit id="admin.menu_frontend_link_caption">
136+
<source>admin.menu_frontend_link_caption</source>
137+
<target>Frontend</target>
138+
</trans-unit>
139+
<trans-unit id="admin.menu_frontend_link_title">
140+
<source>admin.menu_frontend_link_title</source>
141+
<target>Open frontend view in new tab</target>
142+
</trans-unit>
135143
</body>
136144
</file>
137145
</xliff>

0 commit comments

Comments
 (0)