Feature #67880

Add count to listNum

Added by Christian Stern almost 4 years ago. Updated over 1 year ago.

Status:
Closed
Priority:
Should have
Category:
TypoScript
Target version:
Start date:
2015-07-02
Due date:
% Done:

100%

Estimated time:
1.00 h
PHP Version:
Tags:
Complexity:
no-brainer
Sprint Focus:
Remote Sprint

Description

I would like to extend listnum by the following function: count

Quiet often we are dealing with comma separated things like the content of field:records or similiar values.

In some cases we need to know how many items are present inside the csv. Until now I used a simple REGISTER:counter inside a split to determine the amount or sql with LENGTH and REPLACE when extracting from database.

If listNum could just return the amount of items, this wolud be awesome!

I would like to propose the following changes to typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php

I only added those three lines to public function listNum. Maybe it its enough to achieve count functionality.

// Return the amout of items, if requested
if ($listNum === 'count') {
        return count($temp);
}

Here is the complete part of public function listNum:

public function listNum($content, $listNum, $char) {
        $char = $char ?: ',';
        if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($char)) {
                $char = chr($char);
        }
        $temp = explode($char, $content);
        $last = '' . (count($temp) - 1);
        // Take a random item if requested
        if ($listNum === 'rand') {
                $listNum = rand(0, count($temp) - 1);
        }
        $index = $this->calc(str_ireplace('last', $last, $listNum));
        // Return the amout of items, if requested
        if ($listNum === 'count') {
                return count($temp);
        }
        else {
                return $temp[$index];
        }
}

Here is a typoscript example how to use/test it. After patch is set the output should be 9 in the first row. All follwing outputs should contain a true().

testListnum = PAGE
testListnum {
  # should return 9
  1 = TEXT
  1 {
    value = x,y,z,1,2,3,a,b,c
    listNum = count
    wrap = |<br>
  }

  # should be c
  3 = TEXT
  3 {
    current = 1
    setCurrent = x,y,z,1,2,3,a,b,c
    setCurrent {
      listNum = last
    }

    override = true (listNum = last)
    override {
      if {
        value.data = CURRENT
        equals = c
      }
    }
    wrap = |<br>
  }

  # should be b
  4 = TEXT
  4 {
    current = 1
    setCurrent = x,y,z,1,2,3,a,b,c
    setCurrent {
      listNum = last - 1
    }

    override = true (listNum = last - 1)
    override {
      if {
        value.data = CURRENT
        equals = b
      }
    }
    wrap = |<br>
  }

  # should be x
  5 = TEXT
  5 {
    current = 1
    setCurrent = x,y,z,1,2,3,a,b,c
    setCurrent {
      listNum = 0
    }

    override = true (listNum = 0)
    override {
      if {
        value.data = CURRENT
        equals = x
      }
    }
    wrap = |<br>
  }

  # should be 2
  6 = TEXT
  6 {
    current = 1
    setCurrent = x,y,z,1,2,3,a,b,c
    setCurrent {
      listNum = 4
    }

    override = true (listNum = 4)
    override {
      if {
        value.data = CURRENT
        equals = 2
      }
    }
    wrap = |<br>
  }

  # should be inside list
  7 = TEXT
  7 {
    current = 1
    setCurrent = x,y,z,1,2,3,a,b,c
    setCurrent {
      listNum = rand
    }

    override = true (listNum = rand)
    override {
      if {
        value = x,y,z,1,2,3,a,b,c
        isInList.data = CURRENT
      }
    }
    wrap = |<br>
  }
}


Related issues

Related to TYPO3 Core - Bug #68779: Wrong documentation for our new count feature in split Closed 2015-08-06

Associated revisions

Revision 585b18a7 (diff)
Added by Michael Oehlhof almost 4 years ago

[FEATURE] Add returnCount to split

returnCount works like returnKey. It interupts
further processing and directly returns the requested
result. In our case the amount of elements after
the splitting.

Resolves: #67880
Releases: master
Change-Id: Icc50d1e0e4a2ce4d7566ce6f390b09f5e0608f95
Reviewed-on: http://review.typo3.org/40874
Reviewed-by: Andreas Fernandez <>
Reviewed-by: Daniel Goerz <>
Tested-by: Daniel Goerz <>
Reviewed-by: Frederic Gaus <>
Tested-by: Frederic Gaus <>
Reviewed-by: Benjamin Mack <>
Tested-by: Benjamin Mack <>

Revision 304af411 (diff)
Added by Stefan Froemken almost 4 years ago

[BUGFIX] Changed listNum to split in documentation

There is a mistake in our Feature documentation.
Change listNum to split.

Resolves: #68779
Related: #67880
Releases: master
Change-Id: If5a2066d9db12100541dc43163ecae94667d5699
Reviewed-on: http://review.typo3.org/42301
Reviewed-by: Susanne Moog <>
Tested-by: Susanne Moog <>
Reviewed-by: Andreas Fernandez <>
Tested-by: Andreas Fernandez <>

History

#1 Updated by Christian Stern almost 4 years ago

I just found out, that retruning count($temp) is not enough. $content could be empty, thus count($temp) will retun 1 which would be wrong.

if ($listNum === 'count') {
    if (!empty($content)) {
        return count($temp);
    }
    else {
       return 0;
    }
}

#2 Updated by Michael Oehlhof almost 4 years ago

  • Sprint Focus set to Remote Sprint

#3 Updated by Gerrit Code Review almost 4 years ago

  • Status changed from New to Under Review

Patch set 1 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/40874

#4 Updated by Gerrit Code Review almost 4 years ago

Patch set 2 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/40874

#5 Updated by Gerrit Code Review almost 4 years ago

Patch set 3 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/40874

#6 Updated by Alexander Opitz almost 4 years ago

  • Target version changed from 6.2.14 to 7.4 (Backend)

Not for 6.2 branch

#7 Updated by Gerrit Code Review almost 4 years ago

Patch set 4 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/40874

#8 Updated by Gerrit Code Review almost 4 years ago

Patch set 5 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/40874

#9 Updated by Gerrit Code Review almost 4 years ago

Patch set 6 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/40874

#10 Updated by Michael Oehlhof almost 4 years ago

  • Status changed from Under Review to Resolved
  • % Done changed from 0 to 100

#11 Updated by Riccardo De Contardi over 1 year ago

  • Status changed from Resolved to Closed

Also available in: Atom PDF