Combining TypoScript and Fluid: Iterations?

2019-08-11 23:39发布

问题:

I'm combining a TypoScript CONTENT Object with a fluid template.

In the page template:

<f:cObject typoscriptObjectPath="lib.myItem" />

In TS:

lib.myItem = CONTENT
lib.myItem {
  table = tt_content
  select.where = colPos = 0
  select.languageField = sys_language_uid
  renderObj = FLUIDTEMPLATE
  renderObj {
    file = {$customContentTemplatePath}/Myfile.html
    layoutRootPath = {$customContentLayoutPath}
    partialRootPath = {$customContentPartialPath}
    dataProcessing {
      10 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor
      10.references.fieldName = image
    }
  }
}

In Myfile.html:

{namespace v=FluidTYPO3\Vhs\ViewHelpers}

<div class="small-12 medium-6 large-4 columns">
    <f:for each="{files}" as="file">
      <v:media.image src="{file}" srcset="1200,900,600" srcsetDefault="600" alt="{file.alternative}" treatIdAsReference="1"/>
    </f:for>
    <div class="fp-ql-txt">
      {data.header} >
    </div>
</div>

But now I realized that because the template is applied by the renderObj for each content element, I don't have access to fluid's for-each information about iteration. So, I can't do this:

  <f:for each="{data}" as="item" iteration="itemIterator">
  {itemIterator.cycle}
  </f:for>

to find out in which of the rendered items we are ... because each element is rendered individually by renderObj.

How can I get the iteration information about the renderObj's products? Only in TS with the old and terrifying counters as in http://typo3-beispiel.net/index.php?id=9 ?

回答1:

You could make your own IteratorDataProcessor:

<?php
namespace Vendor\MyExt\DataProcessing;

use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;
use TYPO3\CMS\Frontend\ContentObject\Exception\ContentRenderingException;

/**
 * This data processor will keep track of how often he was called and whether it is an
 * even or odd number.
 */
class IteratorProcessor implements DataProcessorInterface, SingletonInterface
{
    /**
     * @var int
     */
    protected $count = 0;

    /**
     * Process data for multiple CEs and keep track of index
     *
     * @param ContentObjectRenderer $cObj The content object renderer, which contains data of the content element
     * @param array $contentObjectConfiguration The configuration of Content Object
     * @param array $processorConfiguration The configuration of this processor
     * @param array $processedData Key/value store of processed data (e.g. to be passed to a Fluid View)
     * @return array the processed data as key/value store
     * @throws ContentRenderingException
     */
    public function process(ContentObjectRenderer $cObj, array $contentObjectConfiguration, array $processorConfiguration, array $processedData)
    {
        $iterator = [];
        $iterator['index'] = $this->count;
        $iterator['isFirst'] = $this->count === 0;
        $this->count++;
        $iterator['cycle'] = $this->count;
        $iterator['isEven'] = $this->count % 2 === 0;
        $iterator['isOdd'] = !$iterator['isEven'];
        $processedData['iterator'] = $iterator;
        return $processedData;
    }
}

In Typoscript you pass your data through that processor:

dataProcessing {
    10 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor
    10 {
        references.fieldName = image
    }
    20 = Vendor\MyExt\DataProcessing\IteratorProcessor
}

In fluid you can access the stuff you set in your DataProcessor with e.g. {iterator.isFirst}.



回答2:

You should check out the DatabaseQueryProcessor shipped with the TYPO3 Core.

https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/Fluidtemplate/Index.html#dataprocessing

Please note that data processing only work inside the FLUIDTEMPLATE cObject.

You also find a working example inside the documentation.