|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony CMF package. |
| 5 | + * |
| 6 | + * (c) 2011-2014 Symfony CMF |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Cmf\Bundle\RoutingBundle\Admin\Extension; |
| 13 | + |
| 14 | +use Knp\Menu\ItemInterface as MenuItemInterface; |
| 15 | +use Sonata\AdminBundle\Admin\AdminExtension; |
| 16 | +use Sonata\AdminBundle\Admin\AdminInterface; |
| 17 | +use Symfony\Bundle\FrameworkBundle\Translation\Translator; |
| 18 | +use Symfony\Cmf\Component\Routing\RouteReferrersReadInterface; |
| 19 | +use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
| 20 | +use Symfony\Component\Routing\Exception\ExceptionInterface as RoutingExceptionInterface; |
| 21 | +use Symfony\Component\Routing\Route; |
| 22 | +use Symfony\Component\Routing\RouterInterface; |
| 23 | + |
| 24 | +/** |
| 25 | + * Admin extension to add a frontend link to the edit tab implementing the |
| 26 | + * RouteReferrersReadInterface. |
| 27 | + * |
| 28 | + * @author Frank Neff <[email protected]> |
| 29 | + */ |
| 30 | +class FrontendLinkExtension extends AdminExtension |
| 31 | +{ |
| 32 | + /** |
| 33 | + * @var RouterInterface |
| 34 | + */ |
| 35 | + private $router; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var Translator |
| 39 | + */ |
| 40 | + private $translator; |
| 41 | + |
| 42 | + /** |
| 43 | + * @param RouterInterface $router |
| 44 | + * @param Translator $translator |
| 45 | + */ |
| 46 | + public function __construct(RouterInterface $router, Translator $translator) |
| 47 | + { |
| 48 | + $this->router = $router; |
| 49 | + $this->translator = $translator; |
| 50 | + } |
| 51 | + |
| 52 | + public function configureTabMenu( |
| 53 | + AdminInterface $admin, |
| 54 | + MenuItemInterface $menu, |
| 55 | + $action, |
| 56 | + AdminInterface $childAdmin = null |
| 57 | + ) { |
| 58 | + if (!$subject = $admin->getSubject()) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + if (!$subject instanceof RouteReferrersReadInterface && !$subject instanceof Route) { |
| 63 | + throw new InvalidConfigurationException( |
| 64 | + sprintf( |
| 65 | + '%s can only be used on subjects which implement Symfony\Cmf\Component\Routing\RouteReferrersReadInterface or Symfony\Component\Routing\Route!', |
| 66 | + __CLASS__ |
| 67 | + ) |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + try { |
| 72 | + $uri = $this->router->generate($subject); |
| 73 | + } catch (RoutingExceptionInterface $e) { |
| 74 | + // we have no valid route |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 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 | + ); |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments