Source of file ModularPage.php
Size: 4,487 Bytes - Last Modified: 2021-12-23T10:53:22+00:00
/var/www/docs.ssmods.com/process/src/code/ModularPage.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 | <?php /** * Class ModularPage. */ class ModularPage extends Page { /** * @var array */ private static $db = array( ); /** * @var array */ private static $has_many = array( 'Modules' => 'PageModule', ); /** * @var array */ public static $allowed_modules = array(); /** * @return FieldList */ public function getCMSFields() { $fields = parent::getCMSFields(); // The SiteTree Content field is used to hold the search index, never displayed $fields->replaceField('Content', LiteralField::create('Content', '')); // Modules can only be added to pages which exist if ($this->exists()) { $config = GridFieldConfig_ModuleEditor::create(); if ($this->Modules()->Count()) { $config->addComponent(new GridFieldOrderableRows('Order')); } $gridField = new GridField('Modules', 'Content blocks', $this->Modules(), $config); $fields->addFieldToTab('Root.Main', $gridField, 'Metadata'); } else { $warningField = new LiteralField('Type', '<p class="message warning">You need to save this page before you can add modules to it.</p>'); $fields->addFieldToTab('Root.Main', $warningField, 'Metadata'); } return $fields; } /** * Iterate through all the modules and add their content to the parent page, so it can be found in searches. */ public function onBeforeWrite() { $pageClass = get_called_class(); // Behaviour can be disabled via the config $writeContent = Config::inst()->get($pageClass, 'write_content'); // If a custom config doesn't exist, check ModularPage if (is_null($writeContent)) { $writeContent = Config::inst()->get('ModularPage', 'write_content'); } if ($writeContent) { $classes = ClassInfo::subclassesFor(__CLASS__); // Only run this code if we're on a valid instance of this class. // Fixes bug when changaing page type via the CMS (e.g. ModularPage -> Page) if (in_array($this->ClassName, $classes)) { if ($this->Modules()->Count()) { $searchBody = ''; foreach ($this->Modules() as $module) { $searchBody .= $module->getSearchBody().PHP_EOL; } $this->Content = $searchBody; } } } parent::onBeforeWrite(); } /** * Build the list of allowed modules for this page type. * * @return array the list of class names to be used */ public static function getAllowedModules() { $pageClass = get_called_class(); // Allow an alternate module base class to be specified, per page type $baseClass = Config::inst()->get($pageClass, 'base_class'); // If a custom config doesn't exist, check ModularPage if (empty($baseClass)) { $baseClass = Config::inst()->get('ModularPage', 'base_class'); } // If no config exists, use defaults if (empty($baseClass)) { $baseClass = 'PageModule'; } $classes = ClassInfo::subclassesFor($baseClass); // Don't let them choose the base class unset($classes[$baseClass]); // Remove any classes not on the whitelist if (count(static::$allowed_modules)) { foreach ($classes as $class) { if (!in_array($class, static::$allowed_modules)) { unset($classes[$class]); } } } return $classes; } } /** * Class ModularPage_Controller. */ class ModularPage_Controller extends Page_Controller { /** * return ArrayList. */ public function ActiveModules() { // If the CMS has asked to focus on 1 module for a preview, just show that. if ($this->request->getVar('moduleFocus')) { return $this->Modules()->filter('ID', $this->request->getVar('moduleFocus')); } $modules = $this->Modules(); return $modules; } /** * Override the $Content template variable so its never used. * Content should come from <% loop $ActiveModules %>. * * @return string */ public function Content() { return ''; } } |