Test Setup Failed
Pull Request — master (#197)
by Gorrie
01:07
created

src/Controllers/ElementController.php (1 issue)

1
<?php
2
3
namespace DNADesign\Elemental\Controllers;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use SilverStripe\Admin\LeftAndMain;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\Control\Director;
9
use SilverStripe\Core\ClassInfo;
10
use SilverStripe\i18n\i18n;
11
use SilverStripe\Security\Member;
12
use SilverStripe\View\Requirements;
13
14
/**
15
 * Optional controller for every element which has its own logic, e.g. in forms.
16
 *
17
 * It always handles a single element, usually passed in as a database
18
 * identifier through the controller URL. Needs to be constructed as a nested
19
 * controller within a {@link ContentController}.
20
 *
21
 * ## Forms
22
 * You can add forms like in any other SilverStripe controller. If you need
23
 * access to the element from within a form, you can use
24
 * `$this->controller->getElement()` inside the form logic.
25
 *
26
 * @package Elemental
27
 */
28
class ElementController extends Controller
29
{
30
    /**
31
     * @var BaseElement $element
32
     */
33
    protected $element;
34
35
    /**
36
     * A list of default (example) styles to include
37
     *
38
     * @config
39
     * @var string[]
40
     */
41
    private static $default_styles = [];
42
43
    /**
44
     * Whether to include default (example) styles
45
     *
46
     * @config
47
     * @var bool
48
     */
49
    private static $include_default_styles = true;
50
51
    /**
52
     * @param BaseElement $element
53
     */
54
    public function __construct(BaseElement $element)
55
    {
56
        $this->element = $element;
57
58
        parent::__construct();
59
60
        $this->setFailover($this->element);
61
    }
62
63
    /**
64
     * @return Element
65
     */
66
    public function getElement()
67
    {
68
        return $this->element;
0 ignored issues
show
Bug Best Practice introduced by Will Rossiter
The expression return $this->element returns the type DNADesign\Elemental\Models\BaseElement which is incompatible with the documented return type DNADesign\Elemental\Controllers\Element.
Loading history...
69
    }
70
71
    /**
72
     * Renders the managed {@link BaseElement} wrapped with the current
73
     * {@link ElementController}.
74
     *
75
     * @return string HTML
76
     */
77
    public function forTemplate()
78
    {
79
        $defaultStyles = $this->config()->get('default_styles');
80
        if ($this->config()->get('include_default_styles') && !empty($defaultStyles)) {
81
            foreach ($defaultStyles as $stylePath) {
82
                Requirements::css($stylePath);
83
            }
84
        }
85
86
        $template = $this->element->config()->get('controller_template');
87
88
        return $this->renderWith([
89
            'type' => 'Layout',
90
            'DNADesign\\Elemental\\'.$template
91
        ]);
92
    }
93
94
    /**
95
     * @param string $action
96
     *
97
     * @return string
98
     */
99
    public function Link($action = null)
100
    {
101
        $page = Director::get_current_page();
102
103
        if ($page && !($page instanceof ElementController)) {
104
            return Controller::join_links(
105
                $page->Link($action),
106
                '#'. $this->element->getAnchor()
107
            );
108
        }
109
110
        $curr = Controller::curr();
111
112
        if ($curr && !($curr instanceof ElementController)) {
113
            return Controller::join_links(
114
                $curr->Link($action),
115
                '#'. $this->element->getAnchor()
116
            );
117
        }
118
    }
119
}
120