ImagesViewHelper.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\OtCore\ViewHelpers\OtAbstractViewHelper;
  6. use TYPO3\CMS\Core\Utility\GeneralUtility;
  7. /**
  8. * This view helper provides an an array of the FAL images files
  9. * that can be used to display a carousel
  10. *
  11. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  12. *
  13. * <ot:carousel.images as="images"
  14. * limit="5"
  15. * countAs="count">
  16. * <f:debug>{images}</f:debug>
  17. * </ot:carousel.images>
  18. *
  19. * @package Opentalent\OtTemplating\ViewHelpers
  20. */
  21. class ImagesViewHelper extends OtAbstractViewHelper {
  22. use TemplateVariableViewHelperTrait;
  23. /**
  24. * >> Required to prevent typo3 to escape the html output
  25. * @var boolean
  26. */
  27. protected $escapeOutput = false;
  28. /**
  29. * -- This method is expected by Fluid --
  30. * Declares the viewhelper's parameters
  31. */
  32. public function initializeArguments()
  33. {
  34. $this->registerArgument(
  35. 'as',
  36. 'string',
  37. 'Name of the returned array',
  38. true
  39. );
  40. $this->registerArgument(
  41. 'limit',
  42. 'integer',
  43. 'Max number of images to return (mdefault: 5)',
  44. false,
  45. 5
  46. );
  47. $this->registerArgument(
  48. 'countAs',
  49. 'string',
  50. "Name of the returned variable that contains the array's length",
  51. true,
  52. 'count'
  53. );
  54. }
  55. /**
  56. * -- This method is expected by Fluid --
  57. * Renders the content as html
  58. *
  59. * @return string
  60. * @throws \Exception
  61. */
  62. public function render()
  63. {
  64. // Get current settings
  65. $as = $this->arguments['as'];
  66. $limit = $this->arguments['limit'];
  67. $countAs = $this->arguments['countAs'];
  68. // Get images
  69. $falViewhelper = GeneralUtility::makeInstance(FalViewHelper::class);
  70. $pageUid = $GLOBALS['TSFE']->page;
  71. $falViewhelper->arguments['slide'] = -1;
  72. $falViewhelper->arguments['limit'] = null;
  73. $falViewhelper->arguments['slideCollectReverse'] = false;
  74. $falViewhelper->arguments['slideCollect'] = 0;
  75. $falViewhelper->arguments['asObjects'] = false;
  76. $resources = $falViewhelper->getResources($pageUid);
  77. $images = [];
  78. $count = 0;
  79. foreach ($resources as $resource) {
  80. if (preg_match('/^image\/.*/', $resource['mimetype'])) {
  81. $images[] = $resource;
  82. $count += 1;
  83. if ($count >= $limit) {
  84. break;
  85. }
  86. }
  87. }
  88. $variables = [$as => $images, $countAs => $count];
  89. return $this->renderChildrenWithVariables($variables);
  90. }
  91. }