ProfileMenuBlockService.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\UserBundle\Block;
  11. use Knp\Menu\Provider\MenuProviderInterface;
  12. use Sonata\BlockBundle\Block\BlockContextInterface;
  13. use Sonata\BlockBundle\Block\Service\MenuBlockService;
  14. use Sonata\UserBundle\Menu\ProfileMenuBuilder;
  15. use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
  16. use Symfony\Component\OptionsResolver\OptionsResolver;
  17. /**
  18. * @author Hugo Briand <briand@ekino.com>
  19. */
  20. class ProfileMenuBlockService extends MenuBlockService
  21. {
  22. /**
  23. * @var ProfileMenuBuilder
  24. */
  25. private $menuBuilder;
  26. /**
  27. * @param string $name
  28. * @param EngineInterface $templating
  29. * @param MenuProviderInterface $menuProvider
  30. * @param ProfileMenuBuilder $menuBuilder
  31. */
  32. public function __construct($name, EngineInterface $templating, MenuProviderInterface $menuProvider, ProfileMenuBuilder $menuBuilder)
  33. {
  34. parent::__construct($name, $templating, $menuProvider, array());
  35. $this->menuBuilder = $menuBuilder;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function getName()
  41. {
  42. return 'User Profile Menu';
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function configureSettings(OptionsResolver $resolver)
  48. {
  49. parent::configureSettings($resolver);
  50. $resolver->setDefaults(array(
  51. 'cache_policy' => 'private',
  52. 'menu_template' => 'SonataBlockBundle:Block:block_side_menu_template.html.twig',
  53. ));
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. protected function getMenu(BlockContextInterface $blockContext)
  59. {
  60. $settings = $blockContext->getSettings();
  61. $menu = parent::getMenu($blockContext);
  62. if (null === $menu || '' === $menu) {
  63. $menu = $this->menuBuilder->createProfileMenu(
  64. array(
  65. 'childrenAttributes' => array('class' => $settings['menu_class']),
  66. 'attributes' => array('class' => $settings['children_class']),
  67. )
  68. );
  69. if (method_exists($menu, 'setCurrentUri')) {
  70. $menu->setCurrentUri($settings['current_uri']);
  71. }
  72. }
  73. return $menu;
  74. }
  75. }