|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DNADesign\ElementalVirtual\Model; |
|
4
|
|
|
|
|
5
|
|
|
use TractorCow\AutoComplete\AutoCompleteField; |
|
6
|
|
|
use SilverStripe\ElementalVirtual\Forms\ElementalGridFieldAddExistingAutocompleter; |
|
|
|
|
|
|
7
|
|
|
use DNADesign\Elemental\Models\BaseElement; |
|
8
|
|
|
use SilverStripe\Forms\FieldList; |
|
9
|
|
|
use SilverStripe\Forms\LiteralField; |
|
10
|
|
|
use SilverStripe\Forms\Tab; |
|
11
|
|
|
use SilverStripe\Forms\TabSet; |
|
12
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
|
13
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Virtual Linked Element. |
|
17
|
|
|
* |
|
18
|
|
|
* As elemental is based on a natural has_one relation to an object, |
|
19
|
|
|
* this allows the same element to be linked to multiple pages. |
|
20
|
|
|
* |
|
21
|
|
|
* {@see ElementalGridFieldAddExistingAutocompleter} |
|
22
|
|
|
*/ |
|
23
|
|
|
class ElementVirtual extends BaseElement |
|
24
|
|
|
{ |
|
25
|
|
|
private static $icon = 'font-icon-block-link'; |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
private static $has_one = [ |
|
|
|
|
|
|
28
|
|
|
'LinkedElement' => BaseElement::class |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
private static $description = 'Reused element'; |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
private static $table_name = 'ElementVirtual'; |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
private static $singular_name = 'virtual block'; |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
private static $inline_editable = false; |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param BaseElement |
|
44
|
|
|
* @param boolean $isSingleton |
|
45
|
|
|
* @param DataModel $model |
|
|
|
|
|
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct($record = null, $isSingleton = false, $model = null) |
|
48
|
|
|
{ |
|
49
|
|
|
parent::__construct($record, $isSingleton, $model); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
$this->LinkedElement()->setVirtualOwner($this); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function getCMSFields() |
|
55
|
|
|
{ |
|
56
|
|
|
$invalid = $this->isInvalidPublishState(); |
|
57
|
|
|
|
|
58
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) use ($invalid) { |
|
59
|
|
|
$fields->removeByName('Title'); |
|
60
|
|
|
|
|
61
|
|
|
$message = sprintf( |
|
62
|
|
|
'<p>%s</p><p><a href="%2$s" target="_blank">Click here to edit the original</a></p>', |
|
63
|
|
|
_t(__CLASS__ . '.VirtualDescription', 'This is a virtual copy of an element.'), |
|
64
|
|
|
$this->LinkedElement()->getEditLink() |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
if ($invalid) { |
|
68
|
|
|
$warning = _t( |
|
69
|
|
|
__CLASS__ . '.InvalidPublishStateWarning', |
|
70
|
|
|
'Error: The original element is not published. This element will not work on the live site until you click the link below and publish it.' |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
$fields->addFieldToTab('Root.Main', LiteralField::create('WarningHeader', '<p class="message error">' . $warning . '</p>')); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$autocomplete = AutoCompleteField::create( |
|
77
|
|
|
'LinkedElementID', |
|
78
|
|
|
_t(__CLASS__ . '.LinkedElement', 'Linked Element'), |
|
79
|
|
|
'', |
|
80
|
|
|
BaseElement::class, |
|
81
|
|
|
'Title' |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
$autocomplete->setLabelField('VirtualLinkedSummary'); |
|
85
|
|
|
$autocomplete->setDisplayField('VirtualLinkedSummary'); |
|
86
|
|
|
$autocomplete->setSourceFilter(['AvailableGlobally' => 1]); |
|
87
|
|
|
|
|
88
|
|
|
$fields->replaceField( |
|
89
|
|
|
'LinkedElementID', |
|
90
|
|
|
$autocomplete |
|
91
|
|
|
); |
|
92
|
|
|
$fields->addFieldToTab('Root.Main', LiteralField::create('Existing', $message)); |
|
93
|
|
|
}); |
|
94
|
|
|
|
|
95
|
|
|
return parent::getCMSFields(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getType() |
|
102
|
|
|
{ |
|
103
|
|
|
return sprintf( |
|
104
|
|
|
_t(__CLASS__ . '.BlockType', 'Virtual Block') |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Detect when a user has published a linked element but has not published |
|
110
|
|
|
* the LinkedElement. |
|
111
|
|
|
* |
|
112
|
|
|
* @return boolean |
|
113
|
|
|
*/ |
|
114
|
|
|
public function isInvalidPublishState() |
|
115
|
|
|
{ |
|
116
|
|
|
$element = $this->LinkedElement(); |
|
117
|
|
|
|
|
118
|
|
|
return (!$element->isPublished() && $this->isPublished()); |
|
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Get a unique anchor name. |
|
123
|
|
|
* |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getAnchor() |
|
127
|
|
|
{ |
|
128
|
|
|
$linkedElement = $this->LinkedElement(); |
|
129
|
|
|
|
|
130
|
|
|
if ($linkedElement && $linkedElement->exists()) { |
|
131
|
|
|
return $linkedElement->getAnchor(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return 'e' . $this->ID; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getSummary() |
|
141
|
|
|
{ |
|
142
|
|
|
if ($linked = $this->LinkedElement()) { |
|
143
|
|
|
return $linked->getSummary(); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getTitle() |
|
151
|
|
|
{ |
|
152
|
|
|
if ($linked = $this->LinkedElement()) { |
|
153
|
|
|
return $linked->Title; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Override to render template based on LinkedElement |
|
159
|
|
|
* |
|
160
|
|
|
* @return string|null HTML |
|
161
|
|
|
*/ |
|
162
|
|
|
public function forTemplate($holder = true) |
|
163
|
|
|
{ |
|
164
|
|
|
if ($linked = $this->LinkedElement()) { |
|
165
|
|
|
return $linked->forTemplate($holder); |
|
166
|
|
|
} |
|
167
|
|
|
return null; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
protected function provideBlockSchema() |
|
171
|
|
|
{ |
|
172
|
|
|
$blockSchema = parent::provideBlockSchema(); |
|
173
|
|
|
$blockSchema['content'] = $this->getSummary(); |
|
174
|
|
|
return $blockSchema; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths