database-query data processor 

The \TYPO3\CMS\Frontend\DataProcessing\DatabaseQueryProcessor , alias database-query, fetches records from the database, using standard TypoScript select semantics. The result is then passed to the FLUIDTEMPLATE as an array.

This way a FLUIDTEMPLATE cObject can iterate over the array of records.

The database-query processor can be used in combination with the record-transformation data processor to use additional computed information.

Options: 

Name Type Default
if condition ''
string / stdWrap ''
string / stdWrap 'records'
array of Data processors []
if
if
Type
if condition
Default
''

Only if the condition is met the data processor is executed.

table
table
Type
string / stdWrap
Required

true

Default
''

Name of the table from which the records should be fetched.

as
as
Type
string / stdWrap
Default
'records'

The variable's name to be used in the Fluid template.

dataProcessing
dataProcessing
Type
array of Data processors
Default
[]

Array of data processors to be applied to all fetched records.

Example: Usage in combination with the RecordTransformationProcessor 

Example usage for the data processor in conjunction with the record-transformation data processor.

EXT:my_extension/Configuration/Sets/Main/setup.typoscript
page = PAGE
page {
  10 = PAGEVIEW
  10 {
    paths.10 = EXT:my_extension/Resources/Private/Templates/
    dataProcessing {
      10 = database-query
      10 {
        as = categories
        table = sys_category
        where = parent=0
        dataProcessing.10 = record-transformation
      }
    }
  }
}
Copied!

For usage of the variables within Fluid see Example: Usage with FLUIDTEMPLATE.

Example: Display speaker records 

Please see also About the examples.

In this example, an extension stores the speakers of a conference in the table tx_myextension_speaker, with the fields name, company, bio, and photo. A content element lists the speakers from the folder in its field pages.

TypoScript

We define the dataProcessing property to use the DatabaseQueryProcessor :

EXT:my_extension/Configuration/Sets/Main/setup.typoscript
lib.contentElement.templateRootPaths.20 = EXT:my_extension/Resources/Private/Templates/ContentElements/

tt_content {
  myextension_speakers =< lib.contentElement
  myextension_speakers {
    templateName = Speakers
    dataProcessing.10 = database-query
    dataProcessing.10 {
      if.isTrue.field = pages
      table = tx_myextension_speaker
      pidInList.field = pages
      orderBy = name
      as = speakers
      # Fetches the photo of each speaker
      dataProcessing {
        10 = files
        10 {
          references.fieldName = photo
        }
      }
    }
  }
}
Copied!
The Fluid template

In the Fluid template, iterate over the records. As the files data processor processes the photo of each record, the template can output the photos as well.

EXT:my_extension/Resources/Private/Templates/ContentElements/Speakers.fluid.html
<html data-namespace-typo3-fluid="true" xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers">
  <h2>Data in variable speakers</h2>
  <f:debug inline="true">{speakers}</f:debug>

  <h2>Output</h2>
  <div class="row">
    <f:for each="{speakers}" as="speaker">
      <div class="col-12 col-md-4">
        <div class="card">
          <f:if condition="{speaker.files.0}">
            <f:image image="{speaker.files.0}" class="card-img-top h-auto" width="400"/>
          </f:if>
          <div class="card-body">
            <h5 class="card-title">{speaker.data.name}</h5>
            <h6 class="card-subtitle text-body-secondary">{speaker.data.company}</h6>
            <div class="card-text"><f:format.html>{speaker.data.bio}</f:format.html></div>
          </div>
        </div>
      </div>
    </f:for>
  </div>
</html>
Copied!
Output

Each entry of the records array contains the data of the table in data and the data of the photo in files.

Dump of the variable speakers with 3 records, each with data and files, and three cards: Alex Example of Example Agency with a photo, Kai Muster of Muster AG, and Robin Sample of Sample Ltd, each with a short biography

Example: Display sorted records from an MM table working with workspaces 

Please see also About the examples.

We define the dataProcessing property to use the DatabaseQueryProcessor . To make use of the sorting field in the MM table, a join is required.

However, performing this join will cause the fields from the MM table to be selected in the query. This can break the output when previewing the page using workspaces.

To prevent this issue, we use the selectFields property to explicitly define which fields should be retrieved.

config/sites/my-site/setup.typoscript
tt_content.myextension_mycontentelement =< lib.contentElement
tt_content.myextension_mycontentelement {
  templateName = MyContentElement
  templateRootPaths {
    102 = EXT:my_extension/Resources/Private/Templates/
  }
  partialRootPaths {
    102 = EXT:my_extension/Resources/Private/Partials/
  }
  layoutRootPaths {
    102 = EXT:my_extension/Resources/Private/Layouts/
  }

  dataProcessing {
    10 = database-query
    10 {
      table = tx_myextension_myrecord
      pidInList.data = site:rootPageId
      selectFields = tx_myextension_myrecord.*
      join = tx_myextension_myrecord_mm mm ON mm.uid_foreign = tx_myextension_myrecord.uid
      where.data = field:uid
      where.intval = 1
      where.wrap = mm.uid_local=|
      orderBy = mm.sorting
      as = myrecord

      # resolving the image reference
      dataProcessing {
        10 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor
        10 {
          references.fieldName = image
          as = images
        }
      }
    }
  }
}
Copied!