GetPreferenceViewHelper.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace Opentalent\OtTemplating\ViewHelpers\Template;
  3. use Closure;
  4. use Opentalent\OtCore\Exception\NoSuchWebsiteException;
  5. use Opentalent\OtCore\ViewHelpers\OtAbstractViewHelper;
  6. use Opentalent\OtCore\Website\OtWebsiteRepository;
  7. use Opentalent\OtTemplating\Templating\TemplateRepository;
  8. use TYPO3\CMS\Core\Utility\GeneralUtility;
  9. use TYPO3\CMS\Extbase\Object\ObjectManager;
  10. use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
  11. /**
  12. * Returns the value of the requested preference, or null if the key does not exist.
  13. *
  14. * {namespace ot=Opentalent\OtTemplating\ViewHelpers}
  15. *
  16. * {ot:template.preference('themeColor')}
  17. *
  18. * @package Opentalent\OtTemplating\ViewHelpers\Template
  19. */
  20. class GetPreferenceViewHelper extends OtAbstractViewHelper
  21. {
  22. public function __construct(
  23. private readonly OtWebsiteRepository $otWebsiteRepository,
  24. private readonly TemplateRepository $templateRepository,
  25. ) {}
  26. /**
  27. * -- This method is expected by Fluid --
  28. * Declares the viewhelper's parameters
  29. */
  30. public function initializeArguments(): void
  31. {
  32. $this->registerArgument(
  33. 'key',
  34. 'string',
  35. 'The name of the requested preference',
  36. true
  37. );
  38. }
  39. /**
  40. * -- This method is expected by Fluid --
  41. * Renders the content as html
  42. *
  43. * @return string | null
  44. * @throws NoSuchWebsiteException
  45. */
  46. public function render(): ?string
  47. {
  48. $website = $this->otWebsiteRepository->getCurrentWebsiteFromFEGlobals();
  49. $preferences = $this->templateRepository->getTemplatePreferences($website);
  50. $key = $this->arguments['key'] ?? null;
  51. return $preferences[$key];
  52. }
  53. }