Source of file ElementController.php
Size: 2,978 Bytes - Last Modified: 2021-12-24T06:44:48+00:00
/var/www/docs.ssmods.com/process/src/src/Controllers/ElementController.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 | <?php namespace DNADesign\Elemental\Controllers; use DNADesign\Elemental\Models\BaseElement; use SilverStripe\Control\Controller; use SilverStripe\Control\Director; use SilverStripe\View\Requirements; /** * Optional controller for every element which has its own logic, e.g. in forms. * * It always handles a single element, usually passed in as a database * identifier through the controller URL. Needs to be constructed as a nested * controller within a {@link ContentController}. * * ## Forms * You can add forms like in any other SilverStripe controller. If you need * access to the element from within a form, you can use * `$this->controller->getElement()` inside the form logic. * * @package Elemental */ class ElementController extends Controller { /** * @var BaseElement $element */ protected $element; /** * A list of default (example) styles to include * * @config * @var string[] */ private static $default_styles = []; /** * Whether to include default (example) styles * * @config * @var bool */ private static $include_default_styles = true; /** * @param BaseElement $element */ public function __construct(BaseElement $element) { $this->element = $element; parent::__construct(); $this->setFailover($this->element); } /** * @return BaseElement */ public function getElement() { return $this->element; } /** * Renders the managed {@link BaseElement} wrapped with the current * {@link ElementController}. * * @return string HTML */ public function forTemplate() { $defaultStyles = $this->config()->get('default_styles'); $this->extend('updateForTemplateDefaultStyles', $defaultStyles); if ($this->config()->get('include_default_styles') && !empty($defaultStyles)) { foreach ($defaultStyles as $stylePath) { Requirements::css($stylePath); } } $template = 'DNADesign\\Elemental\\' . $this->element->config()->get('controller_template'); $this->extend('updateForTemplateTemplate', $template); return $this->renderWith([ 'type' => 'Layout', $template ]); } /** * @param string $action * * @return string */ public function Link($action = null) { $page = Director::get_current_page(); if ($page && !($page instanceof ElementController)) { return Controller::join_links( $page->Link($action), '#'. $this->element->getAnchor() ); } $curr = Controller::curr(); if ($curr && !($curr instanceof ElementController)) { return Controller::join_links( $curr->Link($action), '#'. $this->element->getAnchor() ); } } } |