TYPO3: Use Content Element uid in extbase / fluid templates

20. Juli 2012, Felix Nagel in TYPO3, Extbase & Fluid, English

  • logo-typo3 01
    (c) www.typo3.org, CC BY-ND 2.5

Currently extbase and fluid do not have a built-in functionality to use the uid of the current content element (aka our extbase plugin) within a fluid template.

 

This is an example of how to get the uid using a simple viewHelper. This works for third party extensions as we don't need to assign the uid to a view.

 

I use this to set a unique HTML id attribute so its easier to select the element with jQuery.

														class Tx_MyExt_ViewHelpers_UidViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
														/**
														* @var Tx_Extbase_Configuration_ConfigurationManagerInterface
														*/
														protected $configurationManager;
														/**
														* @param Tx_Extbase_Configuration_ConfigurationManagerInterface An instance of the Configuration Manager
														* @return void
														*/
														public function injectConfigurationManager(Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager) {
														$this->configurationManager = $configurationManager;
														}
														/**
														* Set uid of the content element
														*
														* @return int $uid The uid of the content element
														*/
														public function render() {
														// fallback
														$uid = uniqid();
														if ($this->templateVariableContainer->exists("contentObjectData")) {
														// this works for templates but not for partials
														$contentObjectData = $this->templateVariableContainer->get("contentObjectData");
														$uid = $contentObjectData['uid'];
														} else {
														// this should work in every circumstance
														$cObj = $this->configurationManager->getContentObject();
														$uid = $cObj->data['uid'];
														}
														return $uid;
														}
														}
														
How to receive the CE uid in a fluid view helper

Die Kommentarfunktion ist für diesen Artikel deaktiviert.

2 Kommentare

Felix Nagel

13. Oktober 2012

Das Problem besteht darin das wir in templates die UID schon im Kontext haben, bei partials nicht.

 

Ich glaube man könnte den configurationManager auch nur injecten wenn man ihn wirklich braucht.


Jan

12. Oktober 2012

Danke für die Idee das abfragen der ContentUid in einen ViewHelper auszulagern.

Eine Frage habe ich noch:

Macht es wirklich Sinn das zuerst über den templateVariableContainer zu versuchen, wenn sowieso der configurationManager injected wird und dieser überall funktioniert?

 

Danke Jan