Monday, July 19, 2010

Let users find custom templates with Contao 2.9

This article has developers as audience, if you came here wondering why your custom template doesn’t get listed, please contact the author of the extension and tell him about this article :)

From version 2.9 Contao introduces the concept to pick templates from different directories and subdirectories.

Contao 2.9 introduces also the concept of themes, and let users assign a directory of templates to a theme. You can read the full announcement here.

If an user put a custom template into a subdirectory of the main templates dir, some extensions will not show it.

Every developers should update their extensions to reflect the above changes, here I will propose a cross-version solution to solve this problem.

If your extension uses a custom template, you will have a line like this in your d.c.a (let me tell we are talking about the tl_example dca):

$GLOBALS[‘TL_DCA’][‘tl_example’][‘fields’][‘my_template_field’] = array(
‘inputType’
=> ‘select’,
‘options’
=> $this->getTemplateGroup(‘my_prefix’)
);






in order to list the templates that exists in the template folder assigned to a theme, you must make the following changes:




Create a class wherever you want




(the “Contao way”  of doing it is to create a class named as the dca inside the same file.)




 


class tl_example extends Backend {
public function __construct() {
parent
::__construct();
}

public function listTemplates($dc) {
if (version_compare(VERSION.BUILD, '2.9.0', '>=')) {
return $this->getTemplateGroup(‘my_prefix’, $dc->activeRecord->pid);
}
else {
return $this->getTemplateGroup(‘my_prefix’);
}
}
}





 




then change the field’s entry in your dca to take the list of templates from the above function:




 


$GLOBALS[‘TL_DCA’][‘tl_example’][‘fields’][‘my_template_field’] = array(
‘inputType’
=> ‘select’,
‘options_callback’
=> array('tl_example', 'listTemplates')
);





These changes will make you extension compatible with Contao 2.9 and mantain the compatibilty with older versions.




Please comment below if you find errors or you simply  want to say something :)

No comments:

Post a Comment