FormSelectViewHelper.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Meramo\Mrmbepages\ViewHelper\Form;
  3. use TYPO3\CMS\Core\Utility\GeneralUtility;
  4. use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
  5. class FormSelectViewHelper extends AbstractViewHelper
  6. {
  7. /**
  8. * Tag Name
  9. *
  10. * @var string
  11. */
  12. protected $tagName = 'select';
  13. /**
  14. * Initialize Arguments
  15. */
  16. public function initializeArguments(): void
  17. {
  18. $this->registerArgument('label', 'string', 'select field label');
  19. $this->registerArgument('name', 'string', 'select name attribute');
  20. $this->registerArgument('id', 'string', 'id attribute select field');
  21. $this->registerArgument('options', 'array', 'Associative array with keys as integers and values as string');
  22. $this->registerArgument('required', 'boolean');
  23. }
  24. /**
  25. * @return string
  26. */
  27. public function render()
  28. {
  29. $output = '';
  30. if(isset($this->argument['label']) && $this->argument['label']) {
  31. $label = $this->argument['label'];
  32. $output .= '<label for="Category">'.$label.'</label>';
  33. }
  34. if(isset($this->argument['name']) && $this->argument['name']) {
  35. $this->tag->addAttribute($this->argument['name']);
  36. }
  37. if(isset($this->argument['id']) && $this->argument['id']) {
  38. $this->tag->addAttribute($this->argument['id']);
  39. }
  40. if(isset($this->argument['required']) && $this->argument['required']) {
  41. $this->tag->addAttribute($this->argument['required']);
  42. }
  43. if(isset($this->argument['options']) && $this->argument['options']) {
  44. if(is_array($this->argument['options'])) {
  45. $options = $this->argument['options'];
  46. foreach($options as $k => $value) {
  47. $output .= '<option value="'.$value.'" >"'.ucfirst($value).'"</option>';
  48. }
  49. }
  50. }
  51. $this->tag->forceClosingTag(true);
  52. return $output;
  53. }
  54. }