Tim Wißmann
Tim Wißmann

Reputation: 707

Access MM relation of page record in fluid template

I'm currently building an extension with TYPO3, which does extend the pages table of the TYPO3 core by another field.

Description of my problem:

The field is a MM relation to my own record, containing a description, some images and so on... Because my extension doesn't provide its own plugin (it's an general site extension, providing the sites templates and so on) I have to access the new fields of the pages record via fluid template. But, by accessing this field within the pages info array (with <v:page.info field="myfield" ... /> or {data.myfield}), I only get the current count of referenced rows (the value of the database column in the pages-record).

So, my questions are:

What my TCA (extended pages) looks like:

$temporaryColumns = array(
    'tx_user_myext_myfield' => array(
        /* ... some smaller unimportant TCA settings here ... */
        'config' => array(
            'type' => 'group',
            'internal_type' => 'db',
            'allowed' => 'tx_user_myext_mycustomrow',
            'foreign_table' => 'tx_user_myext_mycustomrow',
            'MM' => 'tx_user_myext_mycustomrow_pages_MM',
            'MM_hasUidField' => 1,
            'multiple' => 1
        )
    )
);

(Of course, I pass $temporaryColumns to addTCAcolumns and addToAllTCAtypes. The backend functionality works fine - that's not the problem.)

EDIT: I can't build a plugin for that, because it's a general part of the website. (So, it will be settled in the template.) Only the record relations should be changeable by the user.

I hope you can help me; thank you very much for any reply to this question.

Upvotes: 1

Views: 1964

Answers (3)

Gabri&#235;l Ramaker
Gabri&#235;l Ramaker

Reputation: 31

Using the DatabaseQueryProcessor (as @bschauer says) it's possible to use a partial to separate your HTML from Typoscript.

TypoScript configuration:

page.10.variables.myRecords = CONTENT
page.10.variables.myRecords {
    table = tx_user_myext_mycustomrow
    select {
        pidInList = root,-1
        selectFields = tx_user_myext_mycustomrow.*
        join = tx_user_myext_mycustomrow_pages_MM ON tx_user_myext_mycustomrow_pages_MM.uid_local = tx_user_myext_mycustomrow.uid
        where.data = field:uid
        where.intval = 1
        where.wrap = tx_user_myext_mycustomrow_pages_MM.uid_foreign=|
        orderBy = tx_user_myext_mycustomrow_pages_MM.sorting_foreign
    }
    renderObj = FLUIDTEMPLATE
    renderObj {
      file = EXT:tx_user_myext/Resources/Private/Partials/MyTemplate.html
      dataProcessing {
        10 = TYPO3\CMS\Frontend\DataProcessing\DatabaseQueryProcessor
        10.references.fieldName = tx_user_myext_myfield
      }
    }
  }
}

Partial MyTemplate.html:

h1>{data.somefield}</h1>

In your Fluid template:

<f:format.raw>{myRecords}</f:format.raw>

Upvotes: 2

bschauer
bschauer

Reputation: 1008

Couldn't you use the DatabaseQueryProcessorfor that?

Example TypoScript configuration:

  10 = TYPO3\CMS\Frontend\DataProcessing\DatabaseQueryProcessor
  10 {
    table = tt_address
    pidInList = 123
    where = company="Acme" AND first_name="Ralph"
    order = RAND()
    as = addresses
    dataProcessing {
      10 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor
      10 {
        references.fieldName = image
      }
    }
  }

https://github.com/TYPO3/TYPO3.CMS/blob/master/typo3/sysext/frontend/Classes/DataProcessing/DatabaseQueryProcessor.php

Upvotes: 0

Dimitri Lavren&#252;k
Dimitri Lavren&#252;k

Reputation: 4879

The best way would be to extend/create the Page Model and create a plugin for showing your stuff.

It is also possible to do it with TypoScript

myRecords = CONTENT
myRecords {
  table = tx_user_myext_mycustomrow
  select {
    pidInList = root,-1
    selectFields = tx_user_myext_mycustomrow.*
    join = tx_user_myext_mycustomrow_pages_MM ON tx_user_myext_mycustomrow_pages_MM.uid_local = tx_user_myext_mycustomrow.uid
    where.data = field:_ORIG_uid // field:uid
    where.intval = 1
    where.wrap = tx_user_myext_mycustomrow_pages_MM.uid_foreign=|
    orderBy = tx_user_myext_mycustomrow_pages_MM.sorting_foreign
  }
  renderObj = COA
  renderObj.10 = TEXT
  renderObj.10.field = somefield
  renderObj.10.wrap = <h1>|</h1>
}

# add myRecords to your fluid variables. I assume that the fluidtemplate is in page.10

page.10.variables.myRecords < myRecords

In Fluid:

<f:render.raw>{myRecords}</f:render.raw>

As you see the TS solution has almost no relation to Fluid since the html is build in TS. I would advice going the way with an own plugin. It takes not much time to add the relation field to the page model and create a small plugin and is a good training for further development.

Upvotes: 3

Related Questions