praiadeseselle/wire/modules/Inputfield/InputfieldHidden.module

99 lines
2.2 KiB
Text
Raw Permalink Normal View History

2022-03-08 15:55:41 +01:00
<?php namespace ProcessWire;
/**
* An Inputfield for handling HTML “hidden” form inputs
*
2022-11-05 18:32:48 +01:00
* ProcessWire 3.x, Copyright 2021 by Ryan Cramer
* https://processwire.com
*
2022-03-08 15:55:41 +01:00
* @property bool $renderValueAsInput Render the hidden input, even when in renderValue mode (default=false)
* @property string $initValue Initial populated value if value attribute not separately populated (default='')
*
*/
class InputfieldHidden extends Inputfield {
public static function getModuleInfo() {
return array(
'title' => __('Hidden', __FILE__), // Module Title
'version' => 101,
'summary' => __('Hidden value in a form', __FILE__), // Module Summary
'permanent' => true,
2022-11-05 18:32:48 +01:00
);
2022-03-08 15:55:41 +01:00
}
2022-11-05 18:32:48 +01:00
/**
* Construct
*
*/
2022-03-08 15:55:41 +01:00
public function __construct() {
parent::__construct();
$this->setAttribute('type', 'hidden');
$this->set('renderValueAsInput', false);
$this->initValue = '';
}
2022-11-05 18:32:48 +01:00
/**
* Render
*
* @return string
*
*/
2022-03-08 15:55:41 +01:00
public function ___render() {
return "<input " . $this->getAttributesString() . " />";
}
2022-11-05 18:32:48 +01:00
/**
* Render value
*
* @return string
*
*/
2022-03-08 15:55:41 +01:00
public function ___renderValue() {
if($this->renderValueAsInput) {
return $this->render();
} else {
return parent::___renderValue();
}
}
2022-11-05 18:32:48 +01:00
/**
* Get attributes
*
* @return array
*
*/
2022-03-08 15:55:41 +01:00
public function getAttributes() {
$attrs = parent::getAttributes();
2022-11-05 18:32:48 +01:00
if(!strlen("$attrs[value]") && $this->initValue) $attrs['value'] = (string) $this->initValue;
2022-03-08 15:55:41 +01:00
return $attrs;
}
2022-11-05 18:32:48 +01:00
/**
* Configure Inputfield
*
* @return InputfieldWrapper
*
*/
2022-03-08 15:55:41 +01:00
public function ___getConfigInputfields() {
$inputfields = parent::___getConfigInputfields();
// remove config fields that don't need to be here
$f = $inputfields->getChildByName('collapsed');
$inputfields->remove($f);
$f = $inputfields->getChildByName('columnWidth');
$inputfields->remove($f);
/** @var InputfieldText $field */
2022-11-05 18:32:48 +01:00
$field = $this->wire()->modules->get('InputfieldText');
2022-03-08 15:55:41 +01:00
$field->setAttribute('name', 'initValue');
$field->label = $this->_('Value');
$field->description = $this->_('Value to be populated in this hidden field.');
$field->setAttribute('value', $this->initValue);
$inputfields->append($field);
2022-11-05 18:32:48 +01:00
2022-03-08 15:55:41 +01:00
return $inputfields;
}
}