Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS

Accessing theme templates in the CMS? [SOLVED]


Reply

4 Posts   467 Views

Avatar
Double-A-Ron

7 March 2014 at 12:00pm Community Member, 604 Posts

I'm extending LeftAndMain to create a custom CMS area. This is loosely based on the Settings area of the CMS.

After looking into how the Settings area is put together in code, I noticed this function that sets the theme to display tabs correctly:

public function getResponseNegotiator() {
      $neg = parent::getResponseNegotiator();
      $controller = $this;
      $neg->setCallback('CurrentForm', function() use(&$controller) {
         return $controller->renderWith($controller->getTemplatesWithSuffix('_Content'));
      });
      return $neg;
   }

This gets a template located in /cms/templates/Includes/CMSSettingsController_Content.ss

So I duplicated this in my custom LeftAndMain child and created a duplicate template. This is not a module as it is very specific to the application and we want to keep code/templates in one area of the file system.

When I do the same as above, getTemplatesWithSuffix only returns LeftAndMain.ss.

I have stepped through the code and found that even though my custom template is found, it is ignored because $theme is null in this subsequent function:
SSViewer.php

public static function hasTemplate($templates) {
      $manifest = SS_TemplateLoader::instance()->getManifest();

      if(Config::inst()->get('SSViewer', 'theme_enabled')) {
         $theme = Config::inst()->get('SSViewer', 'theme');
      } else {
         $theme = null;
      }

      foreach ((array) $templates as $template) {
         if ($manifest->getCandidateTemplate($template, $theme)) return true;
      }

      return false;
   }

So how do I make the CMS aware of the theme? Or is a way of doing this?

Avatar
Double-A-Ron

7 March 2014 at 2:38pm (Last edited: 7 March 2014 2:38pm), Community Member, 604 Posts

Worth adding: Theme is set in config.yml like so:

---
Name: mysite
After: 'framework/*','cms/*'
---
# YAML configuration for SilverStripe
# See http://doc.silverstripe.org/framework/en/topics/configuration
# Caution: Indentation through two spaces, not tabs
SSViewer:
theme: 'mytheme'

I thought the After: might have been the culprit, but even if I remove it entirely and do a dev/build, the CMS still can't get hold of it.

Avatar
simon_w

7 March 2014 at 3:31pm Forum Moderator, 474 Posts

The CMS deliberately unsets the theme, so it won't load templates from there. Put the templates in mysite/templates/ instead.

Avatar
Double-A-Ron

11 March 2014 at 8:37am Community Member, 604 Posts

Thanks Simon,

I wasn't really interested in separating this one template from the rest of the codebase (for future maintenance reasons). But your suggestion did work.

In then end I was able to get the template to load from the theme directory by adding this to my LeftAndMain extended class:

public function init() {
   parent::init();
   
   Config::inst()->update('SSViewer', 'theme_enabled', true);
      
   ...
}