Drupal investigation

ContentRepositoryEnhancer.php 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /*
  3. * This file is part of the Symfony CMF package.
  4. *
  5. * (c) 2011-2015 Symfony CMF
  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 Symfony\Cmf\Component\Routing\Enhancer;
  11. use Symfony\Cmf\Component\Routing\ContentRepositoryInterface;
  12. use Symfony\Cmf\Component\Routing\RouteObjectInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. /**
  15. * This enhancer uses a ContentRepositoryInterface to load a content if $target
  16. * is empty.
  17. *
  18. * $source specifies the field that contains the ID to load, $target specifies
  19. * the field where to put the content returned by the repository.
  20. *
  21. * @author Samusev Andrey
  22. */
  23. class ContentRepositoryEnhancer implements RouteEnhancerInterface
  24. {
  25. /**
  26. * @var ContentRepositoryInterface
  27. */
  28. private $contentRepository;
  29. /**
  30. * @var string
  31. */
  32. private $target;
  33. /**
  34. * @var string
  35. */
  36. private $source;
  37. /**
  38. * @param ContentRepositoryInterface $contentRepository repository to search for the content
  39. * @param string $target the field name to set content
  40. * @param string $source the field name of the request parameter that contains the id
  41. */
  42. public function __construct(
  43. ContentRepositoryInterface $contentRepository,
  44. $target = RouteObjectInterface::CONTENT_OBJECT,
  45. $source = RouteObjectInterface::CONTENT_ID
  46. ) {
  47. $this->contentRepository = $contentRepository;
  48. $this->target = $target;
  49. $this->source = $source;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function enhance(array $defaults, Request $request)
  55. {
  56. if (!isset($defaults[$this->target]) && isset($defaults[$this->source])) {
  57. $defaults[$this->target] = $this->contentRepository->findById($defaults[$this->source]);
  58. }
  59. return $defaults;
  60. }
  61. }