GetByIdViewHelper.php 2.0 KB

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