When implementing custom content elements and/or page templates with FluidTYPO3 you might encounter situations where you want to preprocess a FlexForms's values or add some more template variables. This is easy enough with controllers which come with some nice side effect.

To add such controllers we first of all create the according directory in our provider extension in a well known Extbase fashion located at MyExt/Classes/Controller. For content elements we'll need a ContentController.php and for page templates a PageController.php (surprise!). Both controllers need to extend their abstract counterparts:

/**
 * Controller which is used to render custom content elements
 *
 * @package MyExt
 * @subpackage Controller
 */
class ContentController extends Tx_Fluidcontent_Controller_AbstractContentController {
}

respectively

/**
 * Controller which is used to render page templates
 *
 * @package MyExt
 * @subpackage Controller
 */
class PageController extends Tx_Fluidpages_Controller_AbstractPageController {
}

Now, to make those controllers effective simply add actions for each template file - content or page - named nameOfTemplateAction i.e. the name of the template in a lower camel case notation suffixed with Action. For our example teaser content element from part three of the quickstart guide this would look like:

/**
 * Controller which is used to render custom content elements
 *
 * @package MyExt
 * @subpackage Controller
 */
class ContentController extends Tx_Fluidcontent_Controller_AbstractContentController {

    /**
     * This action will be invoked when the custom
     * content element with template "Teaser.html"
     * is rendered
     *
     * @return void
     */
    public function teaserAction() {
    }

}

While this action is all empty and doesn't do anything by itself it already has the positive side effect of providing the view with the correct controller context when rendering the template. That means all viewhelpers that reference resources in your extension like <f:uri.resource /> or <f:translate /> will now work without providing the extension name to the viewhelper with extensionName="myext".

The original purpose of such a controller is different and there's more to it though. Remember: it's an Extbase action controller so there's lot of things you can do. You can access the underlying Flexform's values with $this->getData(), inject new variables into the template with $this->view->assign(), access object- and configuration managers - you name it.

Now, have a lot of fun!

comments powered by Disqus