GetByIdViewHelper.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\Organizations;
  3. use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
  4. use Opentalent\OtCore\ViewHelpers\OtAbstractViewHelper;
  5. use Opentalent\OtCore\Domain\Model\Organization;
  6. use Opentalent\OtCore\Domain\Repository\OrganizationRepository;
  7. use Opentalent\OtCore\Exception\ApiRequestException;
  8. /**
  9. * This view helper return the Organization object matching the given id
  10. *
  11. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  12. *
  13. * <ot:organizations.getById as="organization"
  14. * organizationId="1">
  15. * <f:debug>{organization}</f:debug>
  16. * </ot:organizations.getById>
  17. *
  18. * @package Opentalent\OtTemplating\ViewHelpers
  19. */
  20. class GetByIdViewHelper extends OtAbstractViewHelper {
  21. use TemplateVariableViewHelperTrait;
  22. /**
  23. * >> Required to prevent typo3 to escape the html output
  24. * @var boolean
  25. */
  26. protected $escapeOutput = false;
  27. /**
  28. * @var OrganizationRepository
  29. *
  30. */
  31. protected $organizationRepository;
  32. /**
  33. * -- This method is expected by Fluid --
  34. * Declares the viewhelper's parameters
  35. */
  36. public function initializeArguments()
  37. {
  38. $this->registerArgument(
  39. 'as',
  40. 'string',
  41. 'Name of the returned array',
  42. true
  43. );
  44. $this->registerArgument(
  45. 'organizationId',
  46. 'integer',
  47. 'Id of the organization',
  48. true
  49. );
  50. }
  51. /**
  52. * -- This method is expected by Fluid --
  53. * Renders the content as html
  54. *
  55. * @return string
  56. * @throws ApiRequestException
  57. */
  58. public function render()
  59. {
  60. $as = $this->arguments['as'];
  61. $organizationId = $this->arguments['organizationId'];
  62. try {
  63. $organization = $this->organizationRepository->findById($organizationId);
  64. } catch (ApiRequestException $e) {
  65. $this->logger->error(sprintf('API Error: %s', $e->getMessage()));
  66. $organization = new Organization();
  67. $organization->setName("<Erreur: impossible d'afficher le résultat>");
  68. }
  69. $variables = [$as => $organization];
  70. return $this->renderChildrenWithVariables($variables);
  71. }
  72. /**
  73. * @param OrganizationRepository $organizationRepository
  74. */
  75. public function injectOrganizationRepository(OrganizationRepository $organizationRepository)
  76. {
  77. $this->organizationRepository = $organizationRepository;
  78. }
  79. }