BreadcrumbsBuilder.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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\AdminBundle\Admin;
  11. use Knp\Menu\ItemInterface;
  12. use Symfony\Component\OptionsResolver\OptionsResolver;
  13. /**
  14. * Stateless breadcrumbs builder (each method needs an Admin object).
  15. *
  16. * @author Grégoire Paris <postmaster@greg0ire.fr>
  17. */
  18. final class BreadcrumbsBuilder implements BreadcrumbsBuilderInterface
  19. {
  20. /**
  21. * @var string[]
  22. */
  23. protected $config = array();
  24. /**
  25. * @param string[] $config
  26. */
  27. public function __construct(array $config = array())
  28. {
  29. $resolver = new OptionsResolver();
  30. $this->configureOptions($resolver);
  31. $this->config = $resolver->resolve($config);
  32. }
  33. /**
  34. * @param OptionsResolver $resolver
  35. */
  36. public function configureOptions(OptionsResolver $resolver)
  37. {
  38. $resolver->setDefaults(array(
  39. 'child_admin_route' => 'edit',
  40. ));
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function getBreadcrumbs(AdminInterface $admin, $action)
  46. {
  47. $breadcrumbs = array();
  48. if ($admin->isChild()) {
  49. return $this->getBreadcrumbs($admin->getParent(), $action);
  50. }
  51. $menu = $this->buildBreadcrumbs($admin, $action);
  52. do {
  53. $breadcrumbs[] = $menu;
  54. } while ($menu = $menu->getParent());
  55. $breadcrumbs = array_reverse($breadcrumbs);
  56. array_shift($breadcrumbs);
  57. return $breadcrumbs;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. * NEXT_MAJOR : make this method private.
  62. */
  63. public function buildBreadcrumbs(AdminInterface $admin, $action, ItemInterface $menu = null)
  64. {
  65. if (!$menu) {
  66. $menu = $admin->getMenuFactory()->createItem('root');
  67. $menu = $menu->addChild(
  68. 'link_breadcrumb_dashboard',
  69. array(
  70. 'uri' => $admin->getRouteGenerator()->generate('sonata_admin_dashboard'),
  71. 'extras' => array('translation_domain' => 'SonataAdminBundle'),
  72. )
  73. );
  74. }
  75. $menu = $this->createMenuItem(
  76. $admin,
  77. $menu,
  78. sprintf('%s_list', $admin->getClassnameLabel()),
  79. $admin->getTranslationDomain(),
  80. array(
  81. 'uri' => $admin->hasRoute('list') && $admin->hasAccess('list') ?
  82. $admin->generateUrl('list') :
  83. null,
  84. )
  85. );
  86. $childAdmin = $admin->getCurrentChildAdmin();
  87. if ($childAdmin) {
  88. $id = $admin->getRequest()->get($admin->getIdParameter());
  89. $menu = $menu->addChild(
  90. $admin->toString($admin->getSubject()),
  91. array(
  92. 'uri' => $admin->hasRoute($this->config['child_admin_route']) && $admin->hasAccess($this->config['child_admin_route'], $admin->getSubject()) ?
  93. $admin->generateUrl($this->config['child_admin_route'], array('id' => $id)) :
  94. null,
  95. 'extras' => array(
  96. 'translation_domain' => false,
  97. ),
  98. )
  99. );
  100. $menu->setExtra('safe_label', false);
  101. return $this->buildBreadcrumbs($childAdmin, $action, $menu);
  102. }
  103. if ('list' === $action) {
  104. $menu->setUri(false);
  105. } elseif ('create' !== $action && $admin->hasSubject()) {
  106. $menu = $menu->addChild($admin->toString($admin->getSubject()), array(
  107. 'extras' => array(
  108. 'translation_domain' => false,
  109. ),
  110. ));
  111. } else {
  112. $menu = $this->createMenuItem(
  113. $admin,
  114. $menu,
  115. sprintf('%s_%s', $admin->getClassnameLabel(), $action),
  116. $admin->getTranslationDomain()
  117. );
  118. }
  119. return $menu;
  120. }
  121. /**
  122. * Creates a new menu item from a simple name. The name is normalized and
  123. * translated with the specified translation domain.
  124. *
  125. * @param AdminInterface $admin used for translation
  126. * @param ItemInterface $menu will be modified and returned
  127. * @param string $name the source of the final label
  128. * @param string $translationDomain for label translation
  129. * @param array $options menu item options
  130. *
  131. * @return ItemInterface
  132. */
  133. private function createMenuItem(
  134. AdminInterface $admin,
  135. ItemInterface $menu,
  136. $name,
  137. $translationDomain = null,
  138. $options = array()
  139. ) {
  140. $options = array_merge(array(
  141. 'extras' => array(
  142. 'translation_domain' => $translationDomain,
  143. ),
  144. ), $options);
  145. return $menu->addChild(
  146. $admin->getLabelTranslatorStrategy()->getLabel(
  147. $name,
  148. 'breadcrumb',
  149. 'link'
  150. ),
  151. $options
  152. );
  153. }
  154. }