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