'Submit', // Module Title 'summary' => __('Form submit button', __FILE__), // Module Summary 'version' => 103, 'permanent' => true, ); } /** * Names of submit buttons created * * @var array * */ protected static $submitNames = array(); /** * Additional dropdown actions added to the button * * @var array * */ protected $dropdownItems = array(); /** * Init * */ public function init() { parent::init(); $this->attr('type', 'submit'); $this->attr('name', 'submit'); $this->attr('value', $this->_('Submit')); // Standard submit button label $this->set('submitValue', false); // becomes string after processInput $this->attr('class', 'ui-button ui-widget ui-state-default ui-corner-all'); $this->set('textClass', 'ui-button-text'); $this->skipLabel = Inputfield::skipLabelBlank; $this->set('small', false); $this->set('html', ''); $this->set('text', ''); // name of 'hidden' input that will receive the clicked dropdown item value $this->set('dropdownInputName', '_action_value'); // Selected dropdown value becomes submit value? If set to false, then only the // dropdownInputName above will contain the selected value $this->set('dropdownSubmit', true); // dropdown selection required? (when true, clicking submit without dropdown selection opens dropdown) $this->set('dropdownRequired', false); } public function set($key, $value) { if($key == 'header') { $this->showInHeader($value); } else if($key == 'secondary') { $this->setSecondary($value); } return parent::set($key, $value); } public function get($key) { if($key == 'header') return $this->hasClass('pw-head-button'); if($key == 'secondary') return $this->hasClass('ui-priority-secondary'); return parent::get($key); } public function setAttribute($key, $value) { if($key === 'name') self::$submitNames[$value] = $value; return parent::setAttribute($key, $value); } /** * Show another copy of this button in the header? * * @param bool $show True=yes, false=no (default=true) * @return $this * */ public function showInHeader($show = true) { if($show) { $this->addClass('pw-head-button'); } else { $this->removeClass('pw-head-button'); } return $this; } /** * Make this button secondary? (slightly faded) * * Note: by default, buttons are not secondary * * @param bool $secondary Default=true * @return $this * */ public function setSecondary($secondary = true) { if($secondary) { $this->addClass('ui-priority-secondary'); } else { $this->removeClass('ui-priority-secondary'); } return $this; } /** * Make this button small? * * By default, buttons are regular size. This makes them small. * Supported only for non-dropdown, non-header buttons. * * @param bool $small Default=true * @return $this * */ public function setSmall($small = true) { $this->set('small', $small ? true : false); return $this; } /** * Render ready * * @param Inputfield|InputfieldWrapper|null The parent InputfieldWrapper that is rendering it, or null if no parent. * @param bool $renderValueMode Specify true only if this is for `Inputfield::renderValue()` rather than `Inputfield::render()`. * @return bool True if assets were just added, false if already added. * */ public function renderReady(Inputfield $parent = null, $renderValueMode = false) { $class = $this->attr('class'); if(strpos($class, 'head_button_clone') !== false) { // if legacy class name used, convert to updated pw- class name to accomodate 3rd party usages $class = str_replace('head_button_clone', 'pw-head-button', $class); $this->attr('class', $class); } if($this->getSetting('small')) { $this->addClass('InputfieldSubmitSmall', 'wrapClass'); } return parent::renderReady($parent, $renderValueMode); } /** * Render the button * * @return string * */ public function ___render() { $sanitizer = $this->wire()->sanitizer; $attrs = $this->getAttributesString(); $icon = $this->icon ? $sanitizer->name($this->icon) : ''; $icon = $icon ? wireIconMarkup($icon) . ' ' : ''; $buttonText = $this->getSetting('html'); // option for non-encoded button text if(empty($buttonText)) { $buttonText = $this->getSetting('text'); if(empty($buttonText)) $buttonText = $this->attr('value'); $buttonText = $this->entityEncode($buttonText); } $buttonText = $icon . $buttonText; $textClass = $sanitizer->entities($this->getSetting('textClass')); if(!empty($textClass)) $buttonText = "$buttonText"; $out = ""; if($this->getSetting('small')) $out = "$out"; if(count($this->dropdownItems)) $out .= $this->renderDropdown(); return $out; } /** * Render the dropdown to accompany the button * * @return string * */ protected function renderDropdown() { if($this->wire('input')->get('modal')) return ''; $config = $this->wire()->config; $file = $config->debug ? 'dropdown.js' : 'dropdown.min.js'; $config->scripts->add($config->urls->InputfieldSubmit . $file); $numValues = 0; $dropdownID = $this->attr('id') . '_dropdown'; $out = ""; if($numValues) { // there are values that can be populated to a hidden input $inputID = $dropdownID . '_value'; $attr = "type='hidden' name='{$this->dropdownInputName}' id='$inputID' value='' "; // copy the submitted dropdown value to the submit button value? if($this->dropdownSubmit) $attr .= "data-pw-dropdown-submit='1' "; // render the output $out = "" . str_replace("