Access a content element's database field value from within the object's typoscript?

Hi everyone,

I got a typoscript that renders a certain type of content element with fluid like that:

`tt_content.recentlyCommentedPagesListing =< lib.contentElement
tt_content.recentlyCommentedPagesListing {
templateName = Listing

templateRootPaths {
10 = EXT:angularcli_suite/Resources/Private/Templates/RecentlyCommentedPages
}

partialRootPaths {
10 = EXT:angularcli_suite/Resources/Private/Partials/RecentlyCommentedPages
}

variables {
listing < lib.recentlyCommentedPages
containerListing < lib.recentlyCommentedPages_In_Container
images < lib.recentlyCommentedPages_Images
typo3Host < lib.WaXCode_AngularCLISuite_AppMenu_Viewer_typo3Host
}
}`

Within the very same typoscript Iā€™m using a select to generate the content for the variable ā€œlistingā€ which is than passed by the fluid-template to an angular component:

`lib.recentlyCommentedPages = CONTENT
lib.recentlyCommentedPages {
table = pages
wrap = |

select {
	orderBy = content.xc_recently_commented_pages_comment_time DESC
	max     = 15
join		= tt_content content ON content.pid = pages.uid AND content.xc_recently_commented_pages_comment != '' AND content.xc_recently_commented_pages_comment_time <= CURRENT_TIMESTAMP() AND content.deleted = 0 AND content.hidden = 0 AND ( content.starttime = 0 OR content.starttime <= UNIX_TIMESTAMP()) AND ( content.endtime = 0 OR content.endtime >= UNIX_TIMESTAMP()) AND ###VALID### LIKE CONCAT( '%', content.fe_group,'%')
	
	markers {
		VALID.data = TSFE:fe_user|user|usergroup
	}
	
	pidInList     	= 1
	recursive     	= 10
	selectFields  	= slug, content.uid AS content_uid, content.xtension_script, content.xtension_css_selector, content.bodytext, content.xc_recently_commented_pages_comment, content.xc_recently_commented_pages_comment_time, content.xtension_feed_configuration, content.xc_searchbar_inlineview_disallowed, content.xc_searchbar_inlineview_max_disallowed

}

renderObj = COA
renderObj {
10 = TEXT
10 {
field = xc_recently_commented_pages_comment_time
strtotime = 1
strftime = %d.%m.%Y
wrap = {<<<Ā°>>>timeOfComment<<<Ā°>>>:<<<Ā°>>>|<<<Ā°>>>,
}

	11 = TEXT
	11 {
		field = slug
		wrap  = <<<Ā°>>>slug<<<Ā°>>>:<<<Ā°>>>|<<<Ā°>>>,
	}

	12 = TEXT
	12 {
		field = content_uid
		wrap  = <<<Ā°>>>uid<<<Ā°>>>:<<<Ā°>>>|<<<Ā°>>>,
	}

	13 = TEXT
	13 {
		field = bodytext
		wrap  = <<<Ā°>>>bodytext<<<Ā°>>>:<<<[>>>|<<<]>>>,
	}

	14 = TEXT
	14 {
		field = xc_recently_commented_pages_comment
		wrap  = <<<Ā°>>>comment<<<Ā°>>>:<<<Ā°>>>|<<<Ā°>>>,
	}

	15 = TEXT
	15 {
		field = xc_searchbar_inlineview_disallowed
		wrap  = <<<Ā°>>>disallowed<<<Ā°>>>:<<<Ā°>>>|<<<Ā°>>>,
	}

	16 = TEXT
	16 {
		field = xtension_feed_configuration
		wrap  = <<<Ā°>>>feed<<<Ā°>>>:<<<Ā°>>>|<<<Ā°>>>,
	}

	17 = TEXT
	17 {
		field = xtension_script
		wrap  = <<<Ā°>>>xscript<<<Ā°>>>:<<<Ā°>>>|<<<Ā°>>>,
	}

	18 = TEXT
	18 {
		field = xtension_css_selector
		wrap  = <<<Ā°>>>css_selector<<<Ā°>>>:<<<[>>>|<<<]>>>,
	}

	19 = TEXT
	19 {
		field = xc_searchbar_inlineview_max_disallowed
		wrap  = <<<Ā°>>>disallowedMax<<<Ā°>>>:<<<Ā°>>>|<<<Ā°>>>}<<<^>>>
	}
}

}`

Within that select I need to modify the ā€œjoinā€-clause so that every found element will only be listet if itā€™s pageā€™s slug contains a string stored in a field in tt_content of the content element that is currently being processed.

Basically I need to get the value of a tt_content-field of the content element that the typoscript is currently rendering. I canā€™t seem to find a way to access the content elementā€™s fields, though. For example I tried accessing it via lib.contentElement.data.fieldName but that didnā€™t work or I tried to do it the wrong way.

Can someone help me out on this?

Hi Salvatore!

This is a large TypoScript, so please pardon me if I donā€™t catch it all the first time. Hereā€™s what I see:

  1. If the TypoScript is defined in the order you have posted it here, it will not be working because you place tt_content.recentlyCommentedPagesListing.variables.listing < lib.recentlyCommentedPages before lib.recentlyCommentedPages is defined.

  2. I also wonder if it would be easier if you defined a DataProcessor in FLUIDTEMPLATEā€™s dataProcessing property, like the DatabaseQueryProcessor instead of inserting the result of a CONTENT cObject into a variable.

  3. The value of the field from the content element currently being rendered in the frontend will be accessible within tt_content.recentlyCommentedPagesListing (e.g. through .data = field:fieldName or .field = fieldName properties), but for example not within lib.recentlyCommentedPages.renderObj because the new query will overwrite it. You can solve this by using LOAD_REGISTER within tt_content.recentlyCommentedPagesListing. For example like this:

lib.recentlyCommentedPages.renderObj.stdWrap {
  cObject = LOAD_REGISTER
  cObject {
    contentElementFieldValue = TEXT
    contentElementFieldValue.field = fieldName
 }
}

Now you can for example access this value within lib.recentlyCommentedPages.renderObj thus:

lib.recentlyCommentedPages.renderObj {
  99 = TEXT
  99.data = register:contentElementFieldValue
}

Remember to use RESET_REGISTER when you no longer need the value in the registry.

Since your TypoScript was not correctly formatted in your post, Iā€™m taking the liberty of repeating it here with what I assume is the correct formatting:

tt_content.recentlyCommentedPagesListing =< lib.contentElement
tt_content.recentlyCommentedPagesListing {
	templateName = Listing

	templateRootPaths {
		10 = EXT:angularcli_suite/Resources/Private/Templates/RecentlyCommentedPages
	}

	partialRootPaths {
		10 = EXT:angularcli_suite/Resources/Private/Partials/RecentlyCommentedPages
	}

	variables {
		listing < lib.recentlyCommentedPages
		containerListing < lib.recentlyCommentedPages_In_Container
		images < lib.recentlyCommentedPages_Images
		typo3Host < lib.WaXCode_AngularCLISuite_AppMenu_Viewer_typo3Host
	}
}

lib.recentlyCommentedPages = CONTENT
lib.recentlyCommentedPages {
	table = pages
	wrap = |
	select {
		orderBy = content.xc_recently_commented_pages_comment_time DESC
		max     = 15
		join		= tt_content content ON content.pid = pages.uid AND content.xc_recently_commented_pages_comment != '' AND content.xc_recently_commented_pages_comment_time <= CURRENT_TIMESTAMP() AND content.deleted = 0 AND content.hidden = 0 AND ( content.starttime = 0 OR content.starttime <= UNIX_TIMESTAMP()) AND ( content.endtime = 0 OR content.endtime >= UNIX_TIMESTAMP()) AND ###VALID### LIKE CONCAT( '%', content.fe_group,'%')
	
		markers {
			VALID.data = TSFE:fe_user|user|usergroup
		}
	
		pidInList     	= 1
		recursive     	= 10
		selectFields  	= slug, content.uid AS content_uid, content.xtension_script, content.xtension_css_selector, content.bodytext, content.xc_recently_commented_pages_comment, content.xc_recently_commented_pages_comment_time, content.xtension_feed_configuration, content.xc_searchbar_inlineview_disallowed, content.xc_searchbar_inlineview_max_disallowed
	}

	renderObj = COA
	renderObj {
		10 = TEXT
		10 {
			field = xc_recently_commented_pages_comment_time
			strtotime = 1
			strftime = %d.%m.%Y
			wrap = {<<<Ā°>>>timeOfComment<<<Ā°>>>:<<<Ā°>>>|<<<Ā°>>>,
		}

		11 = TEXT
		11 {
			field = slug
			wrap  = <<<Ā°>>>slug<<<Ā°>>>:<<<Ā°>>>|<<<Ā°>>>,
		}

		12 = TEXT
		12 {
			field = content_uid
			wrap  = <<<Ā°>>>uid<<<Ā°>>>:<<<Ā°>>>|<<<Ā°>>>,
		}

		13 = TEXT
		13 {
			field = bodytext
			wrap  = <<<Ā°>>>bodytext<<<Ā°>>>:<<<[>>>|<<<]>>>,
		}

		14 = TEXT
		14 {
			field = xc_recently_commented_pages_comment
			wrap  = <<<Ā°>>>comment<<<Ā°>>>:<<<Ā°>>>|<<<Ā°>>>,
		}

		15 = TEXT
		15 {
			field = xc_searchbar_inlineview_disallowed
			wrap  = <<<Ā°>>>disallowed<<<Ā°>>>:<<<Ā°>>>|<<<Ā°>>>,
		}

		16 = TEXT
		16 {
			field = xtension_feed_configuration
			wrap  = <<<Ā°>>>feed<<<Ā°>>>:<<<Ā°>>>|<<<Ā°>>>,
		}

		17 = TEXT
		17 {
			field = xtension_script
			wrap  = <<<Ā°>>>xscript<<<Ā°>>>:<<<Ā°>>>|<<<Ā°>>>,
		}

		18 = TEXT
		18 {
			field = xtension_css_selector
			wrap  = <<<Ā°>>>css_selector<<<Ā°>>>:<<<[>>>|<<<]>>>,
		}

		19 = TEXT
		19 {
			field = xc_searchbar_inlineview_max_disallowed
			wrap  = <<<Ā°>>>disallowedMax<<<Ā°>>>:<<<Ā°>>>|<<<Ā°>>>}<<<^>>>
		}
	}
}

I hope that helps. Please let me know how it goes and feel free to ask any further questions!

ā€” Mathias

Hi Mathias!

Thanx for reformatting the code.

  1. In my code the select statement comes first, youā€™re right. Should have pertained the order, sorry.

  2. I used the only way I found to work searching the internet. Surely using dataProcessing would be easier, if you say so, but I would have to rewrite a lot and I never used the dataProcessing before.

  3. What Iā€™d like to do is to add a Marker in the select that enables to adjenct the following to the join ā€œā€¦AND pages.slug LIKE CONCAT( ā€˜%ā€™, ###ORIGIN###,ā€˜%ā€™)ā€ where ###ORIGIN### would be defined in markers like:

markers {
			VALID.data = TSFE:fe_user|user|usergroup
			ORIGIN.data = The field-value of the current rendered content element
		}

Ok, solved it!

Thanx Mathias, I wouldnā€™t have found out without your help.

Turns out that the solution is fairly simple:

select {
		orderBy = content.xc_recently_commented_pages_comment_time DESC
		max     = 15
    join		= tt_content content ON content.pid = pages.uid AND content.xc_recently_commented_pages_comment != '' AND content.xc_recently_commented_pages_comment_time <= CURRENT_TIMESTAMP() AND content.deleted = 0 AND content.hidden = 0 AND ( content.starttime = 0 OR content.starttime <= UNIX_TIMESTAMP()) AND ( content.endtime = 0 OR content.endtime >= UNIX_TIMESTAMP()) AND ###VALID### LIKE CONCAT( '%', content.fe_group,'%') AND slug LIKE CONCAT( '%',###ORIGIN###,'%')
		
		markers {
			VALID.data 	= TSFE:fe_user|user|usergroup	
			ORIGIN.data = field : xc_recently_commented_pages_urlOrigin
		}
		
		pidInList     	= 1
		recursive     	= 10
		selectFields  	= slug, content.uid AS content_uid, content.xtension_script, content.xtension_css_selector, content.bodytext, content.xc_recently_commented_pages_comment, content.xc_recently_commented_pages_comment_time, content.xtension_feed_configuration, content.xc_searchbar_inlineview_disallowed, content.xc_searchbar_inlineview_max_disallowed
  }

Just needed to add an extra Marker (ā€œORIGINā€) which then can be added to the join like ā€œAND slug LIKE CONCAT( ā€˜%ā€™,###ORIGIN###,ā€˜%ā€™)ā€. Now only entries that contain ā€œfield : xc_recently_commented_pages_urlOriginā€ will be selected.

1 Like

Great to hear you solved it! :smiley: :+1:

ā€” Mathias