From 903ed3e6754dd2d76058ce2b7e3673ab0d84826a Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Thu, 22 Apr 2021 10:42:13 -0400 Subject: [PATCH] Update FieldtypeText to support custom selection of Inputfield (so long as it implements InputfieldHasTextValue), plus some other minor updates. --- wire/modules/Fieldtype/FieldtypeText.module | 92 +++++++++++++------ .../Fieldtype/FieldtypeTextareaHelper.php | 2 +- .../Inputfield/InputfieldSelect.module | 17 +++- .../Inputfield/InputfieldTextarea.module | 3 +- 4 files changed, 82 insertions(+), 32 deletions(-) diff --git a/wire/modules/Fieldtype/FieldtypeText.module b/wire/modules/Fieldtype/FieldtypeText.module index 4ec53e00..9668d272 100644 --- a/wire/modules/Fieldtype/FieldtypeText.module +++ b/wire/modules/Fieldtype/FieldtypeText.module @@ -8,7 +8,7 @@ * For documentation about the fields used in this class, please see: * /wire/core/Fieldtype.php * - * ProcessWire 3.x, Copyright 2019 by Ryan Cramer + * ProcessWire 3.x, Copyright 2021 by Ryan Cramer * https://processwire.com * * @@ -19,7 +19,7 @@ class FieldtypeText extends Fieldtype { public static function getModuleInfo() { return array( 'title' => 'Text', - 'version' => 101, + 'version' => 102, 'summary' => 'Field that stores a single line of text', 'permanent' => true, ); @@ -93,9 +93,11 @@ class FieldtypeText extends Fieldtype { $textformatters = $field->get('textformatters'); if($this->allowTextFormatters() && is_array($textformatters)) { + $modules = $this->wire()->modules; foreach($textformatters as $name) { - if(!$textformatter = $this->wire('modules')->get($name)) continue; - $textformatter->formatValue($page, $field, $value); + /** @var Textformatter $textformatter */ + $textformatter = $modules->get($name); + if($textformatter) $textformatter->formatValue($page, $field, $value); } } @@ -127,8 +129,11 @@ class FieldtypeText extends Fieldtype { * */ public function getInputfield(Page $page, Field $field) { - $inputField = $this->wire('modules')->get('InputfieldText'); - return $inputField; + $modules = $this->wire()->modules; + $inputfieldClass = $field->get('inputfieldClass'); + $inputfield = $inputfieldClass ? $modules->getModule($inputfieldClass) : null; + if(!$inputfield) $inputfield = $modules->get('InputfieldText'); + return $inputfield; } /** @@ -173,32 +178,61 @@ class FieldtypeText extends Fieldtype { */ public function ___getConfigInputfields(Field $field) { $inputfields = parent::___getConfigInputfields($field); - if($this->allowTextFormatters()) { - /** @var Modules $modules */ - $modules = $this->wire('modules'); - $textformatters = $modules->findByPrefix('Textformatter'); - - if(count($textformatters)) { - /** @var InputfieldAsmSelect $f */ - $f = $this->modules->get('InputfieldAsmSelect'); - $f->attr('name', 'textformatters'); - $f->label = $this->_('Text Formatters'); + if(!$this->allowTextFormatters()) return $inputfields; + + $modules = $this->wire()->modules; + $textformatters = $modules->findByPrefix('Textformatter'); + + if(!count($textformatters)) return $inputfields; + + /** @var InputfieldFieldset $fieldset */ + $fieldset = $modules->get('InputfieldFieldset'); + $fieldset->attr('name', '_text_fieldset'); + $fieldset->label = $this->_('Text settings'); + $fieldset->icon = 'text-width'; + $inputfields->add($fieldset); + + /** @var InputfieldAsmSelect $f */ + $f = $this->modules->get('InputfieldAsmSelect'); + $f->attr('name', 'textformatters'); + $f->label = $this->_('Text formatters'); - foreach($textformatters as $moduleName) { - $info = $modules->getModuleInfo($moduleName); - $f->addOption($moduleName, "$info[title]"); - } - - $value = $field->get('textformatters'); - if(!is_array($value)) $value = array(); - $f->val($value); - $f->description = $this->_('If you want to apply any automatic formatting to the field when it is prepared for output, select one or more text formatters above. If you select more than one, drag them into the order they should be applied.'); - $f->notes = $this->_('For plain text fields that will not contain HTML or markup, we recommend selecting the **HTML Entity Encoder** option above.'); - - $inputfields->add($f); - } + foreach($textformatters as $moduleName) { + $info = $modules->getModuleInfo($moduleName); + $f->addOption($moduleName, "$info[title]"); } + $value = $field->get('textformatters'); + if(!is_array($value)) $value = array(); + $f->val($value); + $f->description = + $this->_('If you want to apply any automatic formatting to the field when it is prepared for output, select one or more text formatters here.') . ' ' . + $this->_('If you select more than one, drag them into the order they should be applied.') . ' ' . + sprintf($this->_('Find more in the [text formatter modules directory](%s).'), 'https://processwire.com/modules/category/textformatter/'); + $f->notes = + $this->_('For plain text fields that will not contain HTML or markup, we recommend selecting the **HTML Entity Encoder** option above.'); + $fieldset->add($f); + + if($field->type->className() === 'FieldtypeText') { + /** @var InputfieldSelect $field */ + $defaultLabel = $this->_('Default'); + $f = $this->modules->get('InputfieldRadios'); + $f->attr('name', 'inputfieldClass'); + $f->label = $this->_('Input module'); + $f->description = $this->_('Save after changing this as it may affect what settings are available on the “Input” tab.'); + $f->addOption('', $this->_('Text') . " [span.detail] - $defaultLabel [/span]"); + foreach($modules->findByPrefix('Inputfield', 2) as $moduleName => $moduleInfo) { + if($moduleName === 'InputfieldText') continue; + if(stripos($moduleName, 'textarea') !== false) continue; + if(!wireInstanceOf($moduleName, 'InputfieldHasTextValue')) continue; + $f->addOption($moduleName, "$moduleInfo[title] [span.detail] - $moduleInfo[summary] [/span]"); + } + $f->val((string) $field->get('inputfieldClass')); + $f->collapsed = Inputfield::collapsedBlank; + $fieldset->add($f); + } + + return $inputfields; } diff --git a/wire/modules/Fieldtype/FieldtypeTextareaHelper.php b/wire/modules/Fieldtype/FieldtypeTextareaHelper.php index d5009a43..8843f4e7 100644 --- a/wire/modules/Fieldtype/FieldtypeTextareaHelper.php +++ b/wire/modules/Fieldtype/FieldtypeTextareaHelper.php @@ -250,7 +250,7 @@ class FieldtypeTextareaHelper extends Wire { $editURL = $this->wire('config')->urls->admin . "setup/field/edit?id=$field->id"; $modulesURL = $this->wire('config')->urls->admin . "module/"; $inputfieldClass = $field->get('inputfieldClass'); - $findURL = "http://modules.processwire.com/search/?q=$inputfieldClass"; + $findURL = "https://processwire.com/search/?q=$inputfieldClass&t=Modules"; $tab = '
     '; $note = diff --git a/wire/modules/Inputfield/InputfieldSelect.module b/wire/modules/Inputfield/InputfieldSelect.module index 83ec3e60..a4ed7c6a 100644 --- a/wire/modules/Inputfield/InputfieldSelect.module +++ b/wire/modules/Inputfield/InputfieldSelect.module @@ -17,7 +17,7 @@ * @property bool $valueAddOption If value attr set from API (only) that is not an option, add it as an option? (default=false) 3.0.171+ * */ -class InputfieldSelect extends Inputfield { +class InputfieldSelect extends Inputfield implements InputfieldHasSelectableOptions { /** * Options specific to this Select @@ -82,6 +82,21 @@ class InputfieldSelect extends Inputfield { return $this; } + /** + * Add selectable option with label, optionally for specific language + * + * @param string|int $value + * @param string $label + * @param Language|null $language + * @return $this + * @since 3.0.176 + * + */ + public function addOptionLabel($value, $label, $language = null) { + $this->optionLanguageLabel($language, $value, $label); + return $this; + } + /** * Add multiple options at once * diff --git a/wire/modules/Inputfield/InputfieldTextarea.module b/wire/modules/Inputfield/InputfieldTextarea.module index 02abae61..f65cb9e6 100644 --- a/wire/modules/Inputfield/InputfieldTextarea.module +++ b/wire/modules/Inputfield/InputfieldTextarea.module @@ -125,7 +125,7 @@ class InputfieldTextarea extends InputfieldText { * Process input * * @param WireInputData $input - * @return $this + * @return self|Inputfield * */ public function ___processInput(WireInputData $input) { @@ -175,6 +175,7 @@ class InputfieldTextarea extends InputfieldText { $inputfields->remove($inputfields->getChildByName('pattern')); // pattern is not applicable to textarea //if($this->hasFieldtype !== false) $inputfields->remove($inputfields->get('maxlength')); + /** @var InputfieldInteger $field */ $field = $this->modules->get('InputfieldInteger'); $field->setAttribute('name', 'rows'); $field->label = $this->_('Rows');