Merge pull request #9546 from andrewandante/feature/disambiguate_first_methods_on_lists

deprecate First and Last in order to disambiguate for array lists
This commit is contained in:
Sam Minnée 2020-06-23 14:46:29 +12:00 committed by GitHub
commit 714c4cba9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 6 deletions

View File

@ -358,6 +358,8 @@ iteration.
* `$Even`, `$Odd`: Returns boolean, handy for zebra striping. * `$Even`, `$Odd`: Returns boolean, handy for zebra striping.
* `$EvenOdd`: Returns a string, either 'even' or 'odd'. Useful for CSS classes. * `$EvenOdd`: Returns a string, either 'even' or 'odd'. Useful for CSS classes.
* `$First`, `$Last`, `$Middle`: Booleans about the position in the list. * `$First`, `$Last`, `$Middle`: Booleans about the position in the list.
* Note: as of CMS 4.7.0 `$IsFirst` and `$IsLast` will be preferred. The original
syntax will continue to work, but will be deprecated in a future release.
* `$FirstLast`: Returns a string, "first", "last", "first last" (if both), or "". Useful for CSS classes. * `$FirstLast`: Returns a string, "first", "last", "first last" (if both), or "". Useful for CSS classes.
* `$Pos`: The current position in the list (integer). * `$Pos`: The current position in the list (integer).
Will start at 1, but can take a starting index as a parameter. Will start at 1, but can take a starting index as a parameter.

View File

@ -2,6 +2,8 @@
namespace SilverStripe\View; namespace SilverStripe\View;
use SilverStripe\Dev\Deprecation;
/** /**
* Defines an extra set of basic methods that can be used in templates * Defines an extra set of basic methods that can be used in templates
* that are not defined on sub-classes of {@link ViewableData}. * that are not defined on sub-classes of {@link ViewableData}.
@ -24,6 +26,8 @@ class SSViewer_BasicIteratorSupport implements TemplateIteratorProvider
public static function get_template_iterator_variables() public static function get_template_iterator_variables()
{ {
return [ return [
'IsFirst',
'IsLast',
'First', 'First',
'Last', 'Last',
'FirstLast', 'FirstLast',
@ -57,21 +61,41 @@ class SSViewer_BasicIteratorSupport implements TemplateIteratorProvider
* *
* @return bool * @return bool
*/ */
public function First() public function IsFirst()
{ {
return $this->iteratorPos == 0; return $this->iteratorPos == 0;
} }
/**
* @deprecated 5.0.0 Use IsFirst() to avoid clashes with SS_Lists
* @return bool
*/
public function First()
{
Deprecation::notice('5.0.0', 'Use IsFirst() to avoid clashes with SS_Lists');
return $this->IsFirst();
}
/** /**
* Returns true if this object is the last in a set. * Returns true if this object is the last in a set.
* *
* @return bool * @return bool
*/ */
public function Last() public function IsLast()
{ {
return $this->iteratorPos == $this->iteratorTotalItems - 1; return $this->iteratorPos == $this->iteratorTotalItems - 1;
} }
/**
* @deprecated 5.0.0 Use IsLast() to avoid clashes with SS_Lists
* @return bool
*/
public function Last()
{
Deprecation::notice('5.0.0', 'Use IsLast() to avoid clashes with SS_Lists');
return $this->IsLast();
}
/** /**
* Returns 'first' or 'last' if this is the first or last object in the set. * Returns 'first' or 'last' if this is the first or last object in the set.
* *
@ -79,13 +103,13 @@ class SSViewer_BasicIteratorSupport implements TemplateIteratorProvider
*/ */
public function FirstLast() public function FirstLast()
{ {
if ($this->First() && $this->Last()) { if ($this->IsFirst() && $this->IsLast()) {
return 'first last'; return 'first last';
} }
if ($this->First()) { if ($this->IsFirst()) {
return 'first'; return 'first';
} }
if ($this->Last()) { if ($this->IsLast()) {
return 'last'; return 'last';
} }
return null; return null;
@ -98,7 +122,7 @@ class SSViewer_BasicIteratorSupport implements TemplateIteratorProvider
*/ */
public function Middle() public function Middle()
{ {
return !$this->First() && !$this->Last(); return !$this->IsFirst() && !$this->IsLast();
} }
/** /**