ImagesViewHelper.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. $resources = $falViewhelper->getResources($pageUid);
  73. $images = [];
  74. $count = 0;
  75. foreach ($resources as $resource) {
  76. if (preg_match('/^image\/.*/', $resource['mimetype'])) {
  77. $images[] = $resource;
  78. $count += 1;
  79. if ($count >= $limit) {
  80. break;
  81. }
  82. }
  83. }
  84. $variables = [$as => $images, $countAs => $count];
  85. return $this->renderChildrenWithVariables($variables);
  86. }
  87. }