[FEATURE] Introduce prepend option for Select ViewHelper
authorAnja Leichsenring <aleichsenring@ab-softlab.de>
Thu, 27 Dec 2012 12:37:28 +0000 (13:37 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Tue, 5 Feb 2013 23:05:00 +0000 (00:05 +0100)
A common use case with select fields is to have a 'empty' option at
first position in the options list.

The patch provides the possibility to add a label and additionally
a value, which will be rendered in the option list.

Change-Id: I4fac7372661473d7abbcc209a0fdbd933e65e315
Resolves: #26595
Releases: 6.1
Reviewed-on: https://review.typo3.org/17276
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Thomas Loeffler
Tested-by: Thomas Loeffler
Reviewed-by: Anja Leichsenring
Tested-by: Anja Leichsenring
typo3/sysext/fluid/Classes/ViewHelpers/Form/SelectViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Form/SelectViewHelperTest.php

index 7dda15d..7907d4b 100644 (file)
@@ -41,6 +41,9 @@ namespace TYPO3\CMS\Fluid\ViewHelpers\Form;
  * If the optionValueField variable is set, the getter named after that value is used to retrieve the option key.
  * If the optionLabelField variable is set, the getter named after that value is used to retrieve the option value.
  *
+ * If the prependOptionLabel variable is set, an option item is added in first position, bearing an empty string or -
+ * If provided, the value of the prependOptionValue variable as value.
+ *
  * <code title="Domain objects">
  * <f:form.select name="users" options="{userArray}" optionValueField="id" optionLabelField="firstName" />
  * </code>
@@ -82,6 +85,8 @@ class SelectViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFie
                $this->registerArgument('sortByOptionLabel', 'boolean', 'If true, List will be sorted by label.', FALSE, FALSE);
                $this->registerArgument('selectAllByDefault', 'boolean', 'If specified options are selected if none was set before.', FALSE, FALSE);
                $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this view helper', FALSE, 'f3-form-error');
+               $this->registerArgument('prependOptionLabel', 'string', 'If specified, will provide an option at first position with the specified label.');
+               $this->registerArgument('prependOptionValue', 'string', 'If specified, will provide an option at first position with the specified value.');
        }
 
        /**
@@ -126,6 +131,11 @@ class SelectViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFie
         */
        protected function renderOptionTags($options) {
                $output = '';
+               if ($this->hasArgument('prependOptionLabel')) {
+                       $value = $this->hasArgument('prependOptionValue') ? $this->arguments['prependOptionValue'] : '';
+                       $label = $this->arguments['prependOptionLabel'];
+                       $output .= $this->renderOptionTag($value, $label, FALSE) . chr(10);
+               }
                foreach ($options as $value => $label) {
                        $isSelected = $this->isSelected($value);
                        $output .= $this->renderOptionTag($value, $label, $isSelected) . chr(10);
@@ -266,4 +276,4 @@ class SelectViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFie
        }
 }
 
-?>
\ No newline at end of file
+?>
index c133550..a65818a 100644 (file)
@@ -383,6 +383,47 @@ class SelectViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\
                $this->viewHelper->initialize();
                $this->viewHelper->render();
        }
+
+       /**
+        * @test
+        */
+       public function optionsContainPrependedItemWithEmptyValueIfPrependOptionLabelIsSet() {
+               $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
+               $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
+               $this->tagBuilder->expects($this->once())->method('setContent')->with('<option value="">please choose</option>' . chr(10) . '<option value="value1">label1</option>' . chr(10) . '<option value="value2">label2</option>' . chr(10) . '<option value="value3">label3</option>' . chr(10));
+               $this->tagBuilder->expects($this->once())->method('render');
+               $this->arguments['options'] = array(
+                       'value1' => 'label1',
+                       'value2' => 'label2',
+                       'value3' => 'label3'
+               );
+               $this->arguments['name'] = 'myName';
+               $this->arguments['prependOptionLabel'] = 'please choose';
+               $this->injectDependenciesIntoViewHelper($this->viewHelper);
+               $this->viewHelper->initialize();
+               $this->viewHelper->render();
+       }
+
+       /**
+        * @test
+        */
+       public function optionsContainPrependedItemWithCorrectValueIfPrependOptionLabelAndPrependOptionValueAreSet() {
+               $this->tagBuilder->expects($this->once())->method('addAttribute')->with('name', 'myName');
+               $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('myName');
+               $this->tagBuilder->expects($this->once())->method('setContent')->with('<option value="-1">please choose</option>' . chr(10) . '<option value="value1">label1</option>' . chr(10) . '<option value="value2">label2</option>' . chr(10) . '<option value="value3">label3</option>' . chr(10));
+               $this->tagBuilder->expects($this->once())->method('render');
+               $this->arguments['options'] = array(
+                       'value1' => 'label1',
+                       'value2' => 'label2',
+                       'value3' => 'label3'
+               );
+               $this->arguments['name'] = 'myName';
+               $this->arguments['prependOptionLabel'] = 'please choose';
+               $this->arguments['prependOptionValue'] = '-1';
+               $this->injectDependenciesIntoViewHelper($this->viewHelper);
+               $this->viewHelper->initialize();
+               $this->viewHelper->render();
+       }
 }
 
-?>
\ No newline at end of file
+?>