From d24b942921426e14ea8489883b08f9f51bf73c7d Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 22 Apr 2022 11:28:26 -0400 Subject: [PATCH] Add @adrianbj PR #214 which adds a configurable columns option for Selector fields. Co-authored-by: adrianbj --- .../Fieldtype/FieldtypeSelector.module | 15 +++++++++++-- .../InputfieldSelector.module | 22 ++++++++++++++----- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/wire/modules/Fieldtype/FieldtypeSelector.module b/wire/modules/Fieldtype/FieldtypeSelector.module index 605b2c27..6fd7a911 100644 --- a/wire/modules/Fieldtype/FieldtypeSelector.module +++ b/wire/modules/Fieldtype/FieldtypeSelector.module @@ -104,14 +104,25 @@ class FieldtypeSelector extends Fieldtype { public function ___getConfigInputfields(Field $field) { $inputfields = parent::___getConfigInputfields($field); + $modules = $this->wire()->modules; - $f = $this->wire('modules')->get('InputfieldText'); + /** @var InputfieldText $f */ + $f = $modules->get('InputfieldText'); $f->attr('name', 'initValue'); $f->label = $this->_('Initial Selector Value'); $f->description = $this->_('Enter an initial selector string that will be used as the enforced starting point for any selectors generated from this field. Any pages matching the user selector will be bound within the selector you enter here.'); $f->notes = $this->_('Example: template=product'); $f->attr('value', $field->get('initValue') ? $field->get('initValue') : ''); - $inputfields->add($f); + $inputfields->add($f); + + /** @var InputfieldText $f */ + $f = $modules->get('InputfieldText'); + $f->attr('name', 'previewColumns'); + $f->label = $this->_('Preview Columns'); + $f->description = $this->_('Enter a CSV string of columns/field names to show in previews generated from this field.'); + $f->notes = $this->_('Example: name, template, parent, id'); + $f->attr('value', $field->get('previewColumns') ? $field->get('previewColumns') : ''); + $inputfields->add($f); return $inputfields; } diff --git a/wire/modules/Inputfield/InputfieldSelector/InputfieldSelector.module b/wire/modules/Inputfield/InputfieldSelector/InputfieldSelector.module index 8a7c4240..42773f16 100644 --- a/wire/modules/Inputfield/InputfieldSelector/InputfieldSelector.module +++ b/wire/modules/Inputfield/InputfieldSelector/InputfieldSelector.module @@ -740,7 +740,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule { protected function renderTestSelector($selector) { try { $selector = $this->sanitizeSelectorString($selector); - $cnt = $this->wire('pages')->count($selector, array('allowCustom' => true)); + $cnt = $this->wire()->pages->count($selector, array('allowCustom' => true)); $out = ''; // take into account a limit=n if(strpos($selector, 'limit=') !== false && preg_match('/\blimit=(\d+)/', $selector, $matches)) { @@ -755,12 +755,22 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule { $bookmark = array( 'defaultSelector' => $selector, 'initSelector' => $this->initValue, - ); + ); $previewColumns = $this->sessionGet('previewColumns'); - if($previewColumns && is_string($previewColumns)) $previewColumns = explode(',', $previewColumns); - if(is_array($previewColumns) && count($previewColumns)) $bookmark['columns'] = $previewColumns; - $this->wire('modules')->includeModule('ProcessPageLister'); - $id = ((int) $this->wire('input')->get('id')) . '_' . $this->attr('name'); + if($previewColumns && is_string($previewColumns)) { + $previewColumns = explode(',', str_replace(' ', ',', $previewColumns)); + } + if(is_array($previewColumns) && count($previewColumns)) { + $a = array(); + $sanitizer = $this->wire()->sanitizer; + foreach($previewColumns as $key => $col) { + $col = empty($col) ? '' : $sanitizer->name($col); + if(strlen("$col")) $a[] = $col; + } + if(count($a)) $bookmark['columns'] = $a; + } + $this->wire()->modules->includeModule('ProcessPageLister'); + $id = ((int) $this->wire()->input->get('id')) . '_' . $this->attr('name'); $url = ProcessPageLister::addSessionBookmark($id, $bookmark); if($url) { $title = $this->_('Pages that match your selector');