Version: current

      1 <?php
      2 /**
      3  * ReadonlyField with added toggle-capabilities - will preview the first sentence of the contained text-value,
      4  * and show the full content by a javascript-switch.
      5  *
      6  * Caution: Strips HTML-encoding for the preview.
      7  * @package forms
      8  * @subpackage fields-dataless
      9  */
     10
     11 class ToggleField extends ReadonlyField {
     12
     13     /**
     14      * @var $labelMore string Text shown as a link to see the full content of the field
     15      */
     16     public $labelMore;
     17
     18     /**
     19      * @var $labelLess string Text shown as a link to see the partial view of the field content
     20      */
     21     public $labelLess;
     22
     23     /**
     24      * @see Text
     25      * @var $truncateMethod string (FirstSentence|FirstParagraph)
     26      */
     27     public $truncateMethod = 'FirstSentence';
     28
     29     /**
     30      * @var $truncateChars int Number of chars to preview (optional).
     31      *  Truncating will be applied with $truncateMethod by default.
     32      */
     33     public $truncateChars;
     34
     35     /**
     36      * @param name The field name
     37      * @param title The field title
     38      * @param value The current value
     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         // only create togglefield if the truncated content is shorter
     66         if(strlen($reducedVal) < strlen($rawInput)) {
     67             $content = <<<HTML
     68             <div class="readonly typography contentLess" style="display: none">
     69                 $reducedVal
     70                 &nbsp;<a href="#" class="triggerMore">$this->labelMore</a>
     71             </div>
     72             <div class="readonly typography contentMore">
     73                 $this->value
     74                 &nbsp;<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      * Determines if the field should render open or closed by default.
     89      *
     90      * @param boolean
     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. -