Project

General

Profile

Actions

Feature #67880

closed

Add count to listNum

Added by Christian Stern almost 9 years ago. Updated over 6 years 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 1 (0 open1 closed)

Related to TYPO3 Core - Bug #68779: Wrong documentation for our new count feature in splitClosedStefan Froemken2015-08-06

Actions
Actions #1

Updated by Christian Stern almost 9 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;
    }
}
Actions #2

Updated by Michael Oehlhof almost 9 years ago

  • Sprint Focus set to Remote Sprint
Actions #3

Updated by Gerrit Code Review almost 9 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

Actions #4

Updated by Gerrit Code Review almost 9 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

Actions #5

Updated by Gerrit Code Review almost 9 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

Actions #6

Updated by Alexander Opitz almost 9 years ago

  • Target version changed from 6.2.14 to 7.4 (Backend)

Not for 6.2 branch

Actions #7

Updated by Gerrit Code Review almost 9 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

Actions #8

Updated by Gerrit Code Review almost 9 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

Actions #9

Updated by Gerrit Code Review almost 9 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

Actions #10

Updated by Michael Oehlhof almost 9 years ago

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

Updated by Riccardo De Contardi over 6 years ago

  • Status changed from Resolved to Closed
Actions

Also available in: Atom PDF