ImagesViewHelper.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\Carousel;
  3. use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
  4. use FluidTYPO3\Vhs\ViewHelpers\Page\Resources\FalViewHelper;
  5. use Opentalent\OtTemplating\Domain\Repository\EventRepository;
  6. use Psr\Log\LoggerAwareInterface;
  7. use Psr\Log\LoggerAwareTrait;
  8. use TYPO3\CMS\Core\Utility\GeneralUtility;
  9. use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
  10. /**
  11. * This view helper provides an an array of the FAL images files
  12. * that can be used to display a carousel
  13. *
  14. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  15. *
  16. * <ot:carousel.images as="images"
  17. * limit="5"
  18. * countAs="count">
  19. * <f:debug>{images}</f:debug>
  20. * </ot:carousel.images>
  21. *
  22. * @package Opentalent\OtTemplating\ViewHelpers
  23. */
  24. class ImagesViewHelper extends AbstractViewHelper implements LoggerAwareInterface {
  25. use LoggerAwareTrait;
  26. use TemplateVariableViewHelperTrait;
  27. /**
  28. * >> Required to prevent typo3 to escape the html output
  29. * @var boolean
  30. */
  31. protected $escapeOutput = false;
  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. 'limit',
  46. 'integer',
  47. 'Max number of images to return (mdefault: 5)',
  48. false,
  49. 5
  50. );
  51. $this->registerArgument(
  52. 'countAs',
  53. 'string',
  54. "Name of the returned variable that contains the array's length",
  55. true,
  56. 'count'
  57. );
  58. }
  59. /**
  60. * -- This method is expected by Fluid --
  61. * Renders the content as html
  62. *
  63. * @return string
  64. * @throws \Exception
  65. */
  66. public function render()
  67. {
  68. // Get current settings
  69. $as = $this->arguments['as'];
  70. $limit = $this->arguments['limit'];
  71. $countAs = $this->arguments['countAs'];
  72. // Get images
  73. $falViewhelper = GeneralUtility::makeInstance(FalViewHelper::class);
  74. $pageUid = $GLOBALS['TSFE']->page;
  75. $resources = $falViewhelper->getResources($pageUid);
  76. $images = [];
  77. $count = 0;
  78. foreach ($resources as $resource) {
  79. if (preg_match('/^image\/.*/', $resource['mimetype'])) {
  80. $images[] = $resource;
  81. $count += 1;
  82. if ($count >= $limit) {
  83. break;
  84. }
  85. }
  86. }
  87. $variables = [$as => $images, $countAs => $count];
  88. return $this->renderChildrenWithVariables($variables);
  89. }
  90. /**
  91. * @param EventRepository $eventRepository
  92. */
  93. public function injectEventRepository(EventRepository $eventRepository)
  94. {
  95. $this->eventRepository = $eventRepository;
  96. }
  97. }