From 4ffde04a5cabc466d69735b5bbc04fa983dc99d0 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 10 Nov 2023 13:06:04 -0500 Subject: [PATCH] Improvements to value rendering mode of InputfieldPageListSelect, InputfieldPageListSelectMultiple, as well as InputfieldSelect and InputfieldText, which are inherited by several others. --- .../InputfieldPageListSelect.module | 31 +++---- .../InputfieldPageListSelectCommon.php | 84 +++++++++++++++++++ .../InputfieldPageListSelectMultiple.module | 60 ++++++++----- .../Inputfield/InputfieldSelect.module | 18 +++- .../InputfieldText/InputfieldText.module | 6 ++ 5 files changed, 159 insertions(+), 40 deletions(-) create mode 100644 wire/modules/Inputfield/InputfieldPageListSelect/InputfieldPageListSelectCommon.php diff --git a/wire/modules/Inputfield/InputfieldPageListSelect/InputfieldPageListSelect.module b/wire/modules/Inputfield/InputfieldPageListSelect/InputfieldPageListSelect.module index 4882afde..594ab7a4 100644 --- a/wire/modules/Inputfield/InputfieldPageListSelect/InputfieldPageListSelect.module +++ b/wire/modules/Inputfield/InputfieldPageListSelect/InputfieldPageListSelect.module @@ -1,8 +1,13 @@ __('Selection of a single page from a ProcessWire page tree list', __FILE__), // Module Summary 'version' => 101, 'permanent' => true, - ); + ); } public function init() { @@ -35,23 +42,15 @@ class InputfieldPageListSelect extends Inputfield implements InputfieldPageListS $this->set('showPath', false); parent::init(); } - + public function renderReady(Inputfield $parent = null, $renderValueMode = false) { - static $process = null; - if(is_null($process)) { - /** @var ProcessPageList $process */ - $process = $this->wire('modules')->get('ProcessPageList'); // prerequisite module - $process->setPageLabelField($this->attr('name'), $this->labelFieldName); - $process->renderReady(); - } + $this->pageListReady($this->attr('name'), $this->labelFieldName); return parent::renderReady($parent, $renderValueMode); } - + public function ___render() { - if(!strlen($this->parent_id)) { - return "

" . $this->_('Unable to render this field due to missing parent page in field settings.') . "

"; - } + if(!strlen("$this->parent_id")) return $this->renderParentError(); $this->addClass('InputfieldPageListSelectData'); $attrs = $this->getAttributes(); @@ -69,7 +68,11 @@ class InputfieldPageListSelect extends Inputfield implements InputfieldPageListS return $out; } - + + public function ___renderValue() { + return $this->renderMarkupValue($this->val()); + } + public function ___processInput(WireInputData $input) { parent::___processInput($input); $this->value = (int) $this->value; diff --git a/wire/modules/Inputfield/InputfieldPageListSelect/InputfieldPageListSelectCommon.php b/wire/modules/Inputfield/InputfieldPageListSelect/InputfieldPageListSelectCommon.php new file mode 100644 index 00000000..51a748ad --- /dev/null +++ b/wire/modules/Inputfield/InputfieldPageListSelect/InputfieldPageListSelectCommon.php @@ -0,0 +1,84 @@ +pageList) return; + $this->pageList = $this->wire()->modules->get('ProcessPageList'); // prerequisite module + $this->pageList->setPageLabelField($name, $labelFieldName); + $this->pageList->renderReady(); + } + + /** + * Render markup value for PageListSelect/PageListSelectMultiple + * + * @param int|int[] $value + * @return string + * + */ + public function renderMarkupValue($value) { + if(empty($value)) return ''; + $pages = $this->wire()->pages; + if(is_array($value)) { + $labels = array(); + foreach($value as $id) { + $page = $pages->get((int) "$id"); + $labels[] = $this->getPageLabel($page); + } + return ''; + } else { + $page = $pages->get((int) "$value"); + $label = $this->getPageLabel($page); + return "

$label

"; + } + } + + /** + * Get label to represent given $page + * + * @param Page $page + * @return string + * + */ + public function getPageLabel(Page $page) { + if(!$page->id) return ''; + if(!$page->viewable(false)) { + $label = sprintf($this->_('Page %d not viewable'), $page->id); + } else if($this->hasInputfield instanceof InputfieldPage) { + $label = $this->hasInputfield->getPageLabel($page); + } else { + $label = $page->getUnformatted('title|name'); + } + return $this->wire()->sanitizer->entities($label); + } + + /** + * @return string + * + */ + public function renderParentError() { + return + "

" . + $this->_('Unable to render this field due to missing parent page in field settings.') . + "

"; + } +} diff --git a/wire/modules/Inputfield/InputfieldPageListSelect/InputfieldPageListSelectMultiple.module b/wire/modules/Inputfield/InputfieldPageListSelect/InputfieldPageListSelectMultiple.module index 6dfd51cd..a986cb7b 100644 --- a/wire/modules/Inputfield/InputfieldPageListSelect/InputfieldPageListSelectMultiple.module +++ b/wire/modules/Inputfield/InputfieldPageListSelect/InputfieldPageListSelectMultiple.module @@ -1,17 +1,20 @@ __('Page List Select Multiple', __FILE__), // Module Title @@ -38,47 +43,53 @@ class InputfieldPageListSelectMultiple extends Inputfield $this->set('parent_id', 0); $this->set('labelFieldName', 'title'); $this->set('startLabel', $this->_('Add')); - $this->set('cancelLabel', $this->_('Cancel')); + $this->set('cancelLabel', $this->_('Close')); $this->set('selectLabel', $this->_('Select')); + $this->set('selectedLabel', $this->_('Selected')); $this->set('unselectLabel', $this->_('Unselect')); $this->set('moreLabel', $this->_('More')); $this->set('removeLabel', $this->_('Remove')); } + /** + * @param string $label Entity encoded label text + * @param int $value + * @param string $class + * @return string + * @throws WireException + * + */ protected function renderListItem($label, $value, $class = '') { if($class) $class = " $class"; + $sanitizer = $this->wire()->sanitizer; + $sortIcon = wireIconMarkup('arrows itemSort'); + $trashIcon = wireIconMarkup('trash'); + $trashLabel = $sanitizer->entities1($this->removeLabel); $out = "
  • " . - // "" . - " " . - "$value" . - "$label " . - "" . + "$sortIcon " . + "$value" . + "$label " . + "$trashIcon" . "
  • "; return $out; } - + public function renderReady(Inputfield $parent = null, $renderValueMode = false) { - static $process = null; - if(is_null($process)) { - /** @var ProcessPageList $process */ - $process = $this->wire('modules')->get('ProcessPageList'); // prerequisite module - $process->setPageLabelField($this->attr('name'), $this->labelFieldName); - $process->renderReady(); - } + $this->pageListReady($this->attr('name'), $this->labelFieldName); return parent::renderReady($parent, $renderValueMode); } - + public function ___render() { + + $pages = $this->wire()->pages; - if(!strlen($this->parent_id)) { - return "

    " . $this->_('Unable to render this field due to missing parent page in field settings.') . '

    '; - } + if(!strlen("$this->parent_id")) return $this->renderParentError(); $out = "
      " . $this->renderListItem("Label", "1", "itemTemplate"); foreach($this->value as $page_id) { - $page = $this->pages->get((int) $page_id); + $page = $pages->get((int) $page_id); if(!$page || !$page->id) continue; $label = $page->getText($this->labelFieldName, true, true); if(!strlen($label)) $label = $page->name; @@ -94,20 +105,25 @@ class InputfieldPageListSelectMultiple extends Inputfield $attrs['data-root'] = $this->parent_id; // rootPageID $attrs['data-href'] = "#wrap_{$this->id}"; // selectSelectHref $attrs['data-start'] = $this->startLabel; // selectStartLabel - $attrs['data-cancel'] = $this->_('Close'); // $this->cancelLabel; // selectCancelLabel + $attrs['data-cancel'] = $this->cancelLabel; // selectCancelLabel $attrs['data-select'] = $this->selectLabel; // selectSelectLabel - $attrs['data-selected'] = $this->_('Selected'); + $attrs['data-selected'] = $this->selectedLabel; $attrs['data-unselect'] = $this->unselectLabel; // selectUnselectLabel $attrs['data-more'] = $this->moreLabel; // moreLabel $attrs['data-labelName'] = $this->attr('name'); $attrStr = $this->getAttributesString($attrs); $attrStr = "value='" . implode(',', $this->value) . "' $attrStr"; + $out .= ""; return $out; } + public function ___renderValue() { + return $this->renderMarkupValue($this->val()); + } + /** * Convert the CSV string provide in the $input to an array of ints needed for this fieldtype * diff --git a/wire/modules/Inputfield/InputfieldSelect.module b/wire/modules/Inputfield/InputfieldSelect.module index ec0a524f..3749388c 100644 --- a/wire/modules/Inputfield/InputfieldSelect.module +++ b/wire/modules/Inputfield/InputfieldSelect.module @@ -737,7 +737,6 @@ class InputfieldSelect extends Inputfield implements InputfieldHasSelectableOpti public function ___renderValue() { $out = ''; - $sanitizer = $this->wire()->sanitizer; foreach($this->options as $value => $label) { @@ -752,14 +751,25 @@ class InputfieldSelect extends Inputfield implements InputfieldHasSelectableOpti } else { if($this->isOptionSelected($value)) $o = $label; } - + if(strlen($o)) { - $out .= "
    1. " . $sanitizer->entities($o) . "
    2. "; + $o = $this->entityEncode($o, true); + if($this instanceof InputfieldHasArrayValue) { + $out .= "
    3. $o
    4. "; + } else { + $out = $o; + } } } if(strlen($out)) { - $out = ""; + if($this instanceof InputfieldHasArrayValue) { + $out = ""; + } else { + $out = "

      $out

      "; + } + } else { + bd($this->options); } return $out; diff --git a/wire/modules/Inputfield/InputfieldText/InputfieldText.module b/wire/modules/Inputfield/InputfieldText/InputfieldText.module index eb9ed048..a3837299 100644 --- a/wire/modules/Inputfield/InputfieldText/InputfieldText.module +++ b/wire/modules/Inputfield/InputfieldText/InputfieldText.module @@ -126,6 +126,12 @@ class InputfieldText extends Inputfield { return $out; } + public function ___renderValue() { + $value = (string) $this->val(); + if(!strlen($value)) return ''; + return '

      ' . $this->wire()->sanitizer->entities($value) . '

      '; + } + /** * Get all attributes in an associative array *