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 = true; |
|
|
|
|
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
|
|
|
if ($invalid) { |
62
|
|
|
$warning = _t( |
63
|
|
|
__CLASS__ . '.InvalidPublishStateWarning', |
64
|
|
|
'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.' |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$fields->addFieldToTab('Root.Main', LiteralField::create('WarningHeader', '<p class="message error">' . $warning . '</p>')); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$availableBlocks = BaseElement::get()->filter('AvailableGlobally', 1)->exclude('ClassName', get_class($this)); |
71
|
|
|
$fields->replaceField( |
72
|
|
|
'LinkedElementID', |
73
|
|
|
TagField::create("LinkedElementRelation", $this->fieldLabel('LinkedElement'), $availableBlocks) |
|
|
|
|
74
|
|
|
// Bug: TagField (react) setIsMultiple results in empty (https://github.com/silverstripe/silverstripe-tagfield/issues/195) |
75
|
|
|
// ->setIsMultiple(false) |
76
|
|
|
->setCanCreate(false) |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
if($this->LinkedElementID){ |
80
|
|
|
$message = sprintf( |
81
|
|
|
'<p>%s</p><p><a href="%2$s" target="_blank">Click here to edit the original</a></p>', |
82
|
|
|
_t(__CLASS__ . '.VirtualDescription', 'This is a virtual copy of an element.'), |
83
|
|
|
$this->LinkedElement()->getEditLink() |
84
|
|
|
); |
85
|
|
|
$fields->addFieldToTab('Root.Main', LiteralField::create('Existing', $message)); |
86
|
|
|
} |
87
|
|
|
}); |
88
|
|
|
|
89
|
|
|
return parent::getCMSFields(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Create an intermediary UnsavedRelationList to have TagField save the LinkedElement into |
94
|
|
|
* @return UnsavedRelationList |
95
|
|
|
*/ |
96
|
|
|
public function LinkedElementRelation() |
97
|
|
|
{ |
98
|
|
|
$this->LinkedElementRelation = UnsavedRelationList::create( |
|
|
|
|
99
|
|
|
get_class($this), |
100
|
|
|
'LinkedElementRelation', |
101
|
|
|
BaseElement::class |
102
|
|
|
); |
103
|
|
|
$this->LinkedElementRelation->add($this->LinkedElementID); |
104
|
|
|
return $this->LinkedElementRelation; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Transfer LinkedElement from UnsavedRelationList to has_one LinkedElementID |
109
|
|
|
*/ |
110
|
|
|
public function onBeforeWrite() |
111
|
|
|
{ |
112
|
|
|
if($this->LinkedElementRelation && $this->LinkedElementRelation->first()){ |
113
|
|
|
$this->LinkedElementID = $this->LinkedElementRelation->first()->ID; |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return parent::onBeforeWrite(); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function getType() |
123
|
|
|
{ |
124
|
|
|
return sprintf( |
125
|
|
|
_t(__CLASS__ . '.BlockType', 'Virtual Block') |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Detect when a user has published a linked element but has not published |
131
|
|
|
* the LinkedElement. |
132
|
|
|
* |
133
|
|
|
* @return boolean |
134
|
|
|
*/ |
135
|
|
|
public function isInvalidPublishState() |
136
|
|
|
{ |
137
|
|
|
$element = $this->LinkedElement(); |
138
|
|
|
|
139
|
|
|
return (!$element->isPublished() && $this->isPublished()); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get a unique anchor name. |
144
|
|
|
* |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
public function getAnchor() |
148
|
|
|
{ |
149
|
|
|
$linkedElement = $this->LinkedElement(); |
150
|
|
|
|
151
|
|
|
if ($linkedElement && $linkedElement->exists()) { |
152
|
|
|
return $linkedElement->getAnchor(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return 'e' . $this->ID; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
public function getSummary() |
162
|
|
|
{ |
163
|
|
|
if ($linked = $this->LinkedElement()) { |
164
|
|
|
return $linked->getSummary(); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
public function getTitle() |
172
|
|
|
{ |
173
|
|
|
if ($linked = $this->LinkedElement()) { |
174
|
|
|
return $linked->Title; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Override to render template based on LinkedElement |
180
|
|
|
* |
181
|
|
|
* @return string|null HTML |
182
|
|
|
*/ |
183
|
|
|
public function forTemplate($holder = true) |
184
|
|
|
{ |
185
|
|
|
if ($linked = $this->LinkedElement()) { |
186
|
|
|
return $linked->forTemplate($holder); |
187
|
|
|
} |
188
|
|
|
return null; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
protected function provideBlockSchema() |
192
|
|
|
{ |
193
|
|
|
$blockSchema = parent::provideBlockSchema(); |
194
|
|
|
$blockSchema['content'] = $this->getSummary(); |
195
|
|
|
return $blockSchema; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
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