Version: current

      1 <?php
      2 /**
      3  * Single action button.
      4  * The action buttons are <input type="submit"> tags.
      5  * @package forms
      6  * @subpackage actions
      7  */
      8 class FormAction extends FormField {
      9
     10     protected $extraData;
     11
     12     protected $action;
     13
     14     /**
     15      * Enables the use of <button> instead of <input>
     16      * in {@link Field()} - for more customizeable styling.
     17      *
     18      * @var boolean $useButtonTag
     19      */
     20     public $useButtonTag = false;
     21
     22     private $buttonContent = null;
     23
     24     /**
     25      * Add content inside a button field.
     26      */
     27     function setButtonContent($content) {
     28         $this->buttonContent = (string) $content;
     29     }
     30
     31
     32     /**
     33      * Create a new action button.
     34      * @param action The method to call when the button is clicked
     35      * @param title The label on the button
     36      * @param form The parent form, auto-set when the field is placed inside a form
     37      * @param extraData A piece of extra data that can be extracted with $this->extraData.  Useful for
     38      *                  calling $form->buttonClicked()->extraData()
     39      * @param extraClass A CSS class to apply to the button in addition to 'action'
     40      */
     41     function __construct($action, $title = "", $form = null, $extraData = null, $extraClass = '') {
     42         $this->extraData = $extraData;
     43         $this->addExtraClass($extraClass);
     44         $this->action = "action_$action";
     45
     46         parent::__construct($this->action, $title, null, $form);
     47     }
     48
     49     static function create($action, $title = "", $extraData = null, $extraClass = null) {
     50         return new FormAction($action, $title, null, $extraData, $extraClass);
     51     }
     52
     53     function actionName() {
     54         return substr($this->name,7);
     55     }
     56
     57     /**
     58      * Set the full action name, including action_
     59      * This provides an opportunity to replace it with something else
     60      */
     61     function setFullAction($fullAction) {
     62         $this->action = $fullAction;
     63     }
     64
     65     function extraData() {
     66         return $this->extraData;
     67     }
     68
     69     /**
     70      * Create a submit input, or button tag
     71      * using {@link FormField->createTag()} functionality.
     72      *
     73      * @return HTML code for the input OR button element
     74      */
     75     function Field() {
     76         if($this->useButtonTag) {
     77             $attributes = array(
     78                 'class' => 'action' . ($this->extraClass() ? $this->extraClass() : ''),
     79                 'id' => $this->id(),
     80                 'type' => 'submit',
     81                 'name' => $this->action,
     82                 'tabindex' => $this->getTabIndex()
     83             );
     84             if($this->isReadonly()) {
     85                 $attributes['disabled'] = 'disabled';
     86                 $attributes['class'] = $attributes['class'] . ' disabled';
     87             }
     88
     89             return $this->createTag('button', $attributes, $this->buttonContent ? $this->buttonContent : $this->Title());
     90         } else {
     91             $attributes = array(
     92                 'class' => 'action' . ($this->extraClass() ? $this->extraClass() : ''),
     93                 'id' => $this->id(),
     94                 'type' => 'submit',
     95                 'name' => $this->action,
     96                 'value' => $this->Title(),
     97                 'tabindex' => $this->getTabIndex()
     98             );
     99             if($this->isReadonly()) {
    100                 $attributes['disabled'] = 'disabled';
    101                 $attributes['class'] = $attributes['class'] . ' disabled';
    102             }
    103             $attributes['title'] = ($this->description) ? $this->description : $this->Title();
    104
    105             return $this->createTag('input', $attributes);
    106         }
    107     }
    108
    109     /**
    110      * Does not transform to readonly by purpose.
    111      * Globally disabled buttons would break the CMS.
    112      */
    113     function performReadonlyTransformation() {
    114         $clone = clone $this;
    115         $clone->setReadonly(true);
    116         return $clone;
    117     }
    118
    119     function readonlyField() {
    120         return $this;
    121     }
    122 }
    123
    124 /**
    125  * @package forms
    126  * @subpackage actions
    127  */
    128 class FormAction_WithoutLabel extends FormAction {
    129     function Title(){
    130         return null;
    131     }
    132 }
    133 ?>
    134

[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. -