GetByIdViewHelper.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\Events;
  3. use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
  4. use Opentalent\OtTemplating\Domain\Repository\EventRepository;
  5. use Opentalent\OtTemplating\Exception\ApiRequestException;
  6. use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
  7. /**
  8. * This view helper returns the Event object matching the given id
  9. *
  10. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  11. *
  12. * <ot:events.getById as="event"
  13. * eventId="1">
  14. * <f:debug>{event}</f:debug>
  15. * </ot:events.getById>
  16. *
  17. * @package Opentalent\OtTemplating\ViewHelpers
  18. */
  19. class GetByIdViewHelper extends AbstractViewHelper {
  20. use TemplateVariableViewHelperTrait;
  21. /**
  22. * >> Required to prevent typo3 to escape the html output
  23. * @var boolean
  24. */
  25. protected $escapeOutput = false;
  26. /**
  27. * @var EventRepository
  28. */
  29. protected $eventRepository;
  30. /**
  31. * -- This method is expected by Fluid --
  32. * Declares the viewhelper's parameters
  33. */
  34. public function initializeArguments()
  35. {
  36. $this->registerArgument(
  37. 'as',
  38. 'string',
  39. 'Name of the returned array',
  40. true
  41. );
  42. $this->registerArgument(
  43. 'eventId',
  44. 'integer',
  45. 'Id of the event',
  46. true
  47. );
  48. }
  49. /**
  50. * -- This method is expected by Fluid --
  51. * Renders the content as html
  52. *
  53. * @return string
  54. * @throws ApiRequestException
  55. */
  56. public function render()
  57. {
  58. $as = $this->arguments['as'];
  59. $eventId = $this->arguments['eventId'];
  60. $event = $this->eventRepository->findById($eventId);
  61. $variables = [$as => $event];
  62. return $this->renderChildrenWithVariables($variables);
  63. }
  64. /**
  65. * @param EventRepository $eventRepository
  66. */
  67. public function injectEventRepository(EventRepository $eventRepository)
  68. {
  69. $this->eventRepository = $eventRepository;
  70. }
  71. }