1 <?php
2 3 4 5 6 7 8 9
10
11 class ToggleField extends ReadonlyField {
12
13 14 15
16 public $labelMore;
17
18 19 20
21 public $labelLess;
22
23 24 25 26
27 public $truncateMethod = 'FirstSentence';
28
29 30 31 32
33 public $truncateChars;
34
35 36 37 38 39
40 function __construct($name, $title = "", $value = "") {
41 $this->labelMore = _t('ToggleField.MORE', 'more');
42 $this->labelLess = _t('ToggleField.LESS', 'less');
43
44 $this->startClosed(true);
45
46 parent::__construct($name, $title, $value);
47 }
48
49 function Field() {
50 $content = '';
51
52 Requirements::javascript(SAPPHIRE_DIR . "/thirdparty/prototype/prototype.js");
53 Requirements::javascript(SAPPHIRE_DIR . "/thirdparty/behaviour/behaviour.js");
54 Requirements::javascript(SAPPHIRE_DIR . "/javascript/prototype_improvements.js");
55 Requirements::javascript(SAPPHIRE_DIR . "/javascript/ToggleField.js");
56
57 if($this->startClosed) $this->addExtraClass('startClosed');
58
59 $valforInput = $this->value ? Convert::raw2att($this->value) : "";
60 $rawInput = Convert::html2raw($valforInput);
61
62 if($this->charNum) $reducedVal = substr($rawInput,0,$this->charNum);
63 else $reducedVal = DBField::create('Text',$rawInput)->{$this->truncateMethod}();
64
65
66 if(strlen($reducedVal) < strlen($rawInput)) {
67 $content = <<<HTML
68 <div class="readonly typography contentLess" style="display: none">
69 $reducedVal
70 <a href="#" class="triggerMore">$this->labelMore</a>
71 </div>
72 <div class="readonly typography contentMore">
73 $this->value
74 <a href="#" class="triggerLess">$this->labelLess</a>
75 </div>
76 <br />
77 <input type="hidden" name="$this->name" value="$valforInput" />
78 HTML;
79 } else {
80 $this->dontEscape = true;
81 $content = parent::Field();
82 }
83
84 return $content;
85 }
86
87 88 89 90 91
92 public function startClosed($bool) {
93 ($bool) ? $this->addExtraClass('startClosed') : $this->removeExtraClass('startClosed');
94 }
95
96 function Type() {
97 return "toggleField";
98 }
99 }
100
101 ?>
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-