1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-11 09:14:58 +02:00

Update InputfieldWrapper class so that you can now get new Inputfield object instances from it directly and in a manner that IDEs know what Inputfield module you are working with, so that phpdoc'ing it isn't necessary. This works from accesses to InputfieldWrapper, InputfieldForm, InputfieldFieldset. i.e. $f = $inputfields->InputfieldText; is equivalent to $modules->get('InputfieldText'); but more understandable to the IDE (like PhpStorm).

This commit is contained in:
Ryan Cramer
2021-06-18 11:50:09 -04:00
parent 7465f5339d
commit 99e89fe0fb
3 changed files with 140 additions and 77 deletions

View File

@@ -1434,16 +1434,14 @@ abstract class Inputfield extends WireData implements Module {
$conditionsNote = $this->_('Read more about [how to use this](http://processwire.com/api/selectors/inputfield-dependencies/).');
/** @var InputfieldWrapper $fields */
$fields = $this->wire(new InputfieldWrapper());
$inputfields = $this->wire(new InputfieldWrapper());
/** @var InputfieldFieldset $fieldset */
$fieldset = $this->modules->get('InputfieldFieldset');
$fieldset = $inputfields->InputfieldFieldset;
$fieldset->label = $this->_('Visibility');
$fieldset->attr('name', 'visibility');
$fieldset->icon = 'eye';
/** @var InputfieldSelect $field */
$field = $this->modules->get("InputfieldSelect");
$field = $inputfields->InputfieldSelect;
$field->attr('name', 'collapsed');
$field->label = $this->_('Presentation');
$field->icon = 'eye-slash';
@@ -1466,8 +1464,7 @@ abstract class Inputfield extends WireData implements Module {
$field->attr('value', (int) $this->collapsed);
$fieldset->append($field);
/** @var InputfieldText $field */
$field = $this->modules->get("InputfieldText");
$field = $inputfields->InputfieldText;
$field->label = $this->_('Show this field only if');
$field->description = $this->_('Enter the conditions under which the field will be shown.') . ' ' . $conditionsText;
$field->notes = $conditionsNote;
@@ -1479,10 +1476,9 @@ abstract class Inputfield extends WireData implements Module {
$fieldset->append($field);
$fieldset->collapsed = $this->collapsed == Inputfield::collapsedNo && !$this->getSetting('showIf') ? Inputfield::collapsedYes : Inputfield::collapsedNo;
$fields->append($fieldset);
$inputfields->append($fieldset);
/** @var InputfieldInteger $field */
$field = $this->modules->get('InputfieldInteger');
$field = $inputfields->InputfieldInteger;
$value = (int) $this->getSetting('columnWidth');
if($value < 10 || $value >= 100) $value = 100;
$field->label = sprintf($this->_('Column width (%d%%)'), $value);
@@ -1497,12 +1493,11 @@ abstract class Inputfield extends WireData implements Module {
$field->description = $this->_("The percentage width of this field's container (10%-100%). If placed next to other fields with reduced widths, it will create floated columns."); // Description of colWidth option
$field->notes = $this->_("Note that not all fields will work at reduced widths, so you should test the result after changing this."); // Notes for colWidth option
if(!$this->wire('input')->get('process_template')) if($value == 100) $field->collapsed = Inputfield::collapsedYes;
$fields->append($field);
$inputfields->append($field);
if(!$this instanceof InputfieldWrapper) {
/** @var InputfieldCheckbox $field */
$field = $this->modules->get('InputfieldCheckbox');
$field = $inputfields->InputfieldCheckbox;
$field->label = $this->_('Required?');
$field->icon = 'asterisk';
$field->attr('name', 'required');
@@ -1510,14 +1505,13 @@ abstract class Inputfield extends WireData implements Module {
$field->attr('checked', $this->getSetting('required') ? 'checked' : '');
$field->description = $this->_("If checked, a value will be required for this field.");
$field->collapsed = $this->getSetting('required') ? Inputfield::collapsedNo : Inputfield::collapsedYes;
$fields->add($field);
$inputfields->add($field);
$requiredAttr = $this->getSetting('requiredAttr');
if($requiredAttr !== null) {
// Inputfield must have set requiredAttr to some non-null value before this will appear as option in config
$field->columnWidth = 50; // required checkbox
/** @var InputfieldCheckbox $f */
$f = $this->modules->get('InputfieldCheckbox');
$f = $inputfields->InputfieldCheckbox;
$f->attr('name', 'requiredAttr');
$f->label = $this->_('Also use HTML5 “required” attribute?');
$f->showIf = "required=1, showIf='', requiredIf=''";
@@ -1525,11 +1519,10 @@ abstract class Inputfield extends WireData implements Module {
$f->icon = 'html5';
$f->columnWidth = 50;
if($requiredAttr) $f->attr('checked', 'checked');
$fields->add($f);
$inputfields->add($f);
}
/** @var InputfieldText $field */
$field = $this->modules->get('InputfieldText');
$field = $inputfields->InputfieldText;
$field->label = $this->_('Required only if');
$field->icon = 'asterisk';
$field->description = $this->_('Enter the conditions under which a value will be required for this field.') . ' ' . $conditionsText;
@@ -1538,10 +1531,10 @@ abstract class Inputfield extends WireData implements Module {
$field->attr('value', $this->getSetting('requiredIf'));
$field->collapsed = $field->attr('value') ? Inputfield::collapsedNo : Inputfield::collapsedYes;
$field->showIf = "required>0";
$fields->add($field);
$inputfields->add($field);
}
return $fields;
return $inputfields;
}
/**

View File

@@ -3,7 +3,7 @@
/**
* ProcessWire InputfieldWrapper
*
* ProcessWire 3.x, Copyright 2016 by Ryan Cramer
* ProcessWire 3.x, Copyright 2021 by Ryan Cramer
* https://processwire.com
*
* About InputfieldWrapper
@@ -25,6 +25,48 @@
*
* @method string renderInputfield(Inputfield $inputfield, $renderValueMode = false) #pw-group-output
* @method Inputfield new($typeName, $name = '', $label = '', array $settings = array()) #pw-group-manipulation
*
* @property InputfieldAsmSelect $InputfieldAsmSelect
* @property InputfieldButton $InputfieldButton
* @property InputfieldCheckbox $InputfieldCheckbox
* @property InputfieldCheckboxes $InputfieldCheckboxes
* @property InputfieldCKEditor $InputfieldCkeditor
* @property InputfieldCommentsAdmin $InputfieldCommentsAdmin
* @property InputfieldDatetime $InputfieldDatetime
* @property InputfieldEmail $InputfieldEmail
* @property InputfieldFieldset $InputfieldFieldset
* @property InputfieldFieldsetClose $InputfieldlFieldsetClose
* @property InputfieldFieldsetOpen $InputfieldFieldsetOpen
* @property InputfieldFieldsetTabOpen $InputfieldFieldsetTabOpen
* @property InputfieldFile $InputfieldFile
* @property InputfieldFloat $InputfieldFloat
* @property InputfieldForm $InputfieldForm
* @property InputfieldHidden $InputfieldHidden
* @property InputfieldIcon $InputfieldIcon
* @property InputfieldImage $InputfieldImage
* @property InputfieldInteger $InputfieldInteger
* @property InputfieldMarkup $InputfieldMarkup
* @property InputfieldName $InputfieldName
* @property InputfieldPage $InputfieldPage
* @property InputfieldPageAutocomplete $InputfieldPageAutocomplete
* @property InputfieldPageListSelect $InputfieldPageListSelect
* @property InputfieldPageListSelectMultiple $InputfieldPageListSelectMultiple
* @property InputfieldPageName $InputfieldPageName
* @property InputfieldPageTable $InputfieldPageTable
* @property InputfieldPageTitle $InputfieldPageTitle
* @property InputfieldPassword $InputfieldPassword
* @property InputfieldRadios $InputfieldRadios
* @property InputfieldRepeater $InputfieldRepeater
* @property InputfieldSelect $InputfieldSelect
* @property InputfieldSelectMultiple $InputfieldSelectMultiple
* @property InputfieldSelector $InputfieldSelector
* @property InputfieldSubmit $InputfieldSubmit
* @property InputfieldText $InputfieldText
* @property InputfieldTextarea $InputfieldTextarea
* @property InputfieldTextTags $InputfieldTextTags
* @property InputfieldToggle $InputfieldToggle
* @property InputfieldURL $InputfieldURL
* @property InputfieldWrapper $InputfieldWrapper
*
*/
@@ -197,6 +239,13 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
*/
public function __get($key) {
if($key === 'children') return $this->children();
if(strpos($key, 'Inputfield') === 0 && strlen($key) > 10) {
if($key === 'InputfieldWrapper') return $this->wire(new InputfieldWrapper());
$value = $this->wire()->modules->get($key);
if($value && $value instanceof Inputfield) return $value;
if(wireClassExists($key)) return $this->wire(new $key());
$value = null;
}
$value = parent::get($key);
if(is_null($value)) $value = $this->getChildByName($key);
return $value;
@@ -1408,11 +1457,14 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre
$inputfields = parent::___getConfigInputfields();
// remove all options for 'collapsed' except collapsedYes and collapsedNo
foreach($inputfields as $f) {
if($f->attr('name') != 'collapsed') continue;
/** @var InputfieldSelect $f */
$f = $inputfields->getChildByName('collapsed');
if($f) {
// remove all options for 'collapsed' except collapsedYes and collapsedNo
foreach($f->getOptions() as $value => $label) {
if(!in_array($value, array(Inputfield::collapsedNo, Inputfield::collapsedYes))) $f->removeOption($value);
if(!in_array($value, array(Inputfield::collapsedNo, Inputfield::collapsedYes))) {
$f->removeOption($value);
}
}
}