1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
class CMSEditLinkField extends ReadonlyField |
|
|
|
|
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* what are we linking to? |
9
|
|
|
* @var DataObject |
10
|
|
|
*/ |
11
|
|
|
protected $linkedObject = null; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* appendix for field name |
15
|
|
|
* @var String |
16
|
|
|
*/ |
17
|
|
|
protected $nameAppendix = '_CMSEditLink'; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* |
22
|
|
|
* @param string $name e.g. MyLinkedObjectID |
23
|
|
|
* @param string $title e.g. My Fancy Title |
24
|
|
|
* @param DataObject $linkedObject e.g. $this->MyLinkedObjectID() |
25
|
|
|
* @param string $methodOrVariable (OPTIONAL) - e.g. MyFullTitle |
26
|
|
|
*/ |
27
|
|
|
public function __construct($name, $title, $linkedObject, $methodOrVariable = 'getTitle') |
28
|
|
|
{ |
29
|
|
|
$name .= $this->nameAppendix; |
30
|
|
|
if ($linkedObject && $linkedObject->exists() && $linkedObject->hasMethod('CMSEditLink')) { |
31
|
|
|
$this->linkedObject = $linkedObject; |
32
|
|
|
if($this->linkedObject->hasMethod($methodOrVariable)) { |
33
|
|
|
$description = $this->linkedObject->$methodOrVariable(); |
34
|
|
|
} elseif(isset($this->linkedObject->$methodOrVariable)) { |
35
|
|
|
$description = $this->linkedObject->$methodOrVariable; |
36
|
|
|
} else { |
37
|
|
|
$description = 'ERROR!'; |
38
|
|
|
user_error($methodOrVariable.' does not exist on '.$this->linkedObject.' (as method or variable)'); |
39
|
|
|
} |
40
|
|
|
$content = '<p class="cms-edit-link"><a href="'.$this->linkedObject->CMSEditLink().'">'.Convert::raw2xml($description).'</a></p>'; |
41
|
|
|
$this->dontEscape = true; |
42
|
|
|
|
43
|
|
|
return parent::__construct($name, $title, $content); |
|
|
|
|
44
|
|
|
} else { |
45
|
|
|
return parent::__construct($name, $title); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* |
51
|
|
|
* |
52
|
|
|
* @param string $s |
53
|
|
|
*/ |
54
|
|
|
function setNameAppendix($s) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$this->nameAppendix = $s; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
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.