|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Virtual Linked Element. |
|
5
|
|
|
* |
|
6
|
|
|
* As elemental is based on widgets which have a natural has_one relation to an |
|
7
|
|
|
* object, this is a workaround for allowing the same element to be linked to |
|
8
|
|
|
* multiple pages. |
|
9
|
|
|
* |
|
10
|
|
|
* {@see ElementalGridFieldAddExistingAutocompleter} |
|
11
|
|
|
* |
|
12
|
|
|
* @package elemental |
|
13
|
|
|
*/ |
|
14
|
|
|
class ElementVirtualLinked extends BaseElement { |
|
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
private static $title = 'Virtual linked Block'; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private static $singular_name = 'Virtual linked Block'; |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
private static $has_one = array( |
|
|
|
|
|
|
26
|
|
|
'LinkedElement' => 'BaseElement' |
|
27
|
|
|
); |
|
28
|
|
|
|
|
29
|
|
|
public function getTitle() |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
return $this->LinkedElement()->getTitle(); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function i18n_singular_name() |
|
35
|
|
|
{ |
|
36
|
|
|
return _t(__CLASS__, $this->LinkedElement()->config()->title); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getCMSFields() { |
|
|
|
|
|
|
40
|
|
|
$message = sprintf('<p>%s</p><p><a href="%2$s">%2$s</a></p>', |
|
41
|
|
|
_t('ElementVirtualLinked.DESCRIBE', 'This is a virtual copy of a block. To edit, visit'), |
|
42
|
|
|
$this->LinkedElement()->getEditLink() |
|
|
|
|
|
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
|
|
$fields = new FieldList( |
|
46
|
|
|
new TabSet('Root', $main = new Tab('Main')) |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
if ($this->isInvalidPublishState()) { |
|
50
|
|
|
$warning = 'Error: The original block is not published. This block will not work on the live site until you click the link below and publish it.'; |
|
51
|
|
|
$main->push(new LiteralField('WarningHeader', '<p class="message error">' .$warning. '</p>')); |
|
52
|
|
|
} |
|
53
|
|
|
$main->push(new LiteralField('Existing', $message)); |
|
54
|
|
|
|
|
55
|
|
|
$this->extend('updateCMSFields', $fields); |
|
56
|
|
|
|
|
57
|
|
|
return $fields; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getExtraClass() { |
|
61
|
|
|
return $this->LinkedElement()->ClassName . ' ' . $this->getField('ExtraClass'); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Detect when a user has published a ElementVirtualLinked block |
|
66
|
|
|
* but has not published the LinkedElement block. |
|
67
|
|
|
*/ |
|
68
|
|
|
public function isInvalidPublishState() { |
|
69
|
|
|
$block = $this->LinkedElement(); |
|
|
|
|
|
|
70
|
|
|
return (!$block->isPublished() && $this->isPublished()); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function getCMSPublishedState() { |
|
|
|
|
|
|
74
|
|
|
if ($this->isInvalidPublishState()) { |
|
75
|
|
|
$colour = '#C00'; |
|
76
|
|
|
$text = 'Error'; |
|
77
|
|
|
$html = new HTMLText('PublishedState'); |
|
78
|
|
|
$html->setValue(sprintf( |
|
79
|
|
|
'<span style="color: %s;">%s</span>', |
|
80
|
|
|
$colour, |
|
81
|
|
|
htmlentities($text) |
|
82
|
|
|
)); |
|
83
|
|
|
return $html; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$publishedState = null; |
|
87
|
|
|
foreach($this->extension_instances as $instance) { |
|
88
|
|
|
if (method_exists($instance, 'getCMSPublishedState')) { |
|
89
|
|
|
$instance->setOwner($this); |
|
90
|
|
|
$publishedState = $instance->getCMSPublishedState(); |
|
91
|
|
|
$instance->clearOwner(); |
|
92
|
|
|
break; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
return $publishedState; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.