mirror of
https://github.com/processwire/processwire.git
synced 2025-08-15 03:05:26 +02:00
Improvements to value rendering mode of InputfieldPageListSelect, InputfieldPageListSelectMultiple, as well as InputfieldSelect and InputfieldText, which are inherited by several others.
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
<?php namespace ProcessWire;
|
||||
|
||||
require_once(__DIR__ . '/InputfieldPageListSelectCommon.php');
|
||||
|
||||
/**
|
||||
* A Page List Selector for selecting a single page
|
||||
*
|
||||
* ProcessWire 3.x, Copyright 2023 by Ryan Cramer
|
||||
* https://processwire.com
|
||||
*
|
||||
* @property int $parent_id
|
||||
* @property string $labelFieldName
|
||||
* @property string $startLabel
|
||||
@@ -14,6 +19,8 @@
|
||||
*
|
||||
*/
|
||||
class InputfieldPageListSelect extends Inputfield implements InputfieldPageListSelection {
|
||||
|
||||
use InputfieldPageListSelectCommon;
|
||||
|
||||
public static function getModuleInfo() {
|
||||
return array(
|
||||
@@ -21,7 +28,7 @@ class InputfieldPageListSelect extends Inputfield implements InputfieldPageListS
|
||||
'summary' => __('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 "<p class='error'>" . $this->_('Unable to render this field due to missing parent page in field settings.') . "</p>";
|
||||
}
|
||||
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;
|
||||
|
@@ -0,0 +1,84 @@
|
||||
<?php namespace ProcessWire;
|
||||
|
||||
/**
|
||||
* Common methods for InputfieldPageListSelect and InputfieldPageListSelectMultiple
|
||||
*
|
||||
* @since 3.0.231
|
||||
*
|
||||
*/
|
||||
trait InputfieldPageListSelectCommon {
|
||||
|
||||
/**
|
||||
* @var ProcessPageList|null
|
||||
*
|
||||
*/
|
||||
protected $pageList = null;
|
||||
|
||||
/**
|
||||
* Render ready
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $labelFieldName
|
||||
*
|
||||
*/
|
||||
public function pageListReady($name, $labelFieldName) {
|
||||
if($this->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 '<ul><li>' . implode('</li><li>', $labels) . '</li></ul>';
|
||||
} else {
|
||||
$page = $pages->get((int) "$value");
|
||||
$label = $this->getPageLabel($page);
|
||||
return "<p>$label</p>";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
"<p class='error'>" .
|
||||
$this->_('Unable to render this field due to missing parent page in field settings.') .
|
||||
"</p>";
|
||||
}
|
||||
}
|
@@ -1,17 +1,20 @@
|
||||
<?php namespace ProcessWire;
|
||||
|
||||
require_once(__DIR__ . '/InputfieldPageListSelectCommon.php');
|
||||
|
||||
/**
|
||||
* ProcessWire Page List Select Multiple Inputfield module
|
||||
*
|
||||
* A Page List Selector for selecting multiple pages
|
||||
*
|
||||
* ProcessWire 3.x, Copyright 2022 by Ryan Cramer
|
||||
* ProcessWire 3.x, Copyright 2023 by Ryan Cramer
|
||||
* https://processwire.com
|
||||
*
|
||||
* @property string $removeLabel
|
||||
* @property string $moreLabel
|
||||
* @property string $unselectLabel
|
||||
* @property string $selectLabel
|
||||
* @property string $selectedLabel
|
||||
* @property string $cancelLabel
|
||||
* @property string $startLabel
|
||||
* @property string $labelFieldName
|
||||
@@ -23,6 +26,8 @@
|
||||
class InputfieldPageListSelectMultiple extends Inputfield
|
||||
implements InputfieldHasArrayValue, InputfieldPageListSelection, InputfieldHasSortableValue {
|
||||
|
||||
use InputfieldPageListSelectCommon;
|
||||
|
||||
public static function getModuleInfo() {
|
||||
return array(
|
||||
'title' => __('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 =
|
||||
"<li class='ui-state-default$class'>" .
|
||||
// "<span class='ui-icon ui-icon-arrowthick-2-n-s'></span>" .
|
||||
"<i class='itemSort fa fa-arrows'></i> " .
|
||||
"<span class='itemValue'>$value</span>" .
|
||||
"<span class='itemLabel'>$label</span> " .
|
||||
"<a class='itemRemove' title='$this->removeLabel' href='#'><i class='fa fa-trash'></i></a>" .
|
||||
"$sortIcon " .
|
||||
"<span class='itemValue'>$value</span>" .
|
||||
"<span class='itemLabel'>$label</span> " .
|
||||
"<a class='itemRemove' title='$trashLabel' href='#'>$trashIcon</a>" .
|
||||
"</li>";
|
||||
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 "<p class='error'>" . $this->_('Unable to render this field due to missing parent page in field settings.') . '</p>';
|
||||
}
|
||||
if(!strlen("$this->parent_id")) return $this->renderParentError();
|
||||
|
||||
$out = "<ol id='{$this->id}_items'>" . $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 .= "<input type='text' $attrStr />";
|
||||
|
||||
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
|
||||
*
|
||||
|
@@ -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 .= "<li>" . $sanitizer->entities($o) . "</li>";
|
||||
$o = $this->entityEncode($o, true);
|
||||
if($this instanceof InputfieldHasArrayValue) {
|
||||
$out .= "<li>$o</li>";
|
||||
} else {
|
||||
$out = $o;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(strlen($out)) {
|
||||
$out = "<ul class='pw-bullets'>$out</ul>";
|
||||
if($this instanceof InputfieldHasArrayValue) {
|
||||
$out = "<ul class='pw-bullets'>$out</ul>";
|
||||
} else {
|
||||
$out = "<p>$out</p>";
|
||||
}
|
||||
} else {
|
||||
bd($this->options);
|
||||
}
|
||||
|
||||
return $out;
|
||||
|
@@ -126,6 +126,12 @@ class InputfieldText extends Inputfield {
|
||||
return $out;
|
||||
}
|
||||
|
||||
public function ___renderValue() {
|
||||
$value = (string) $this->val();
|
||||
if(!strlen($value)) return '';
|
||||
return '<p>' . $this->wire()->sanitizer->entities($value) . '</p>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all attributes in an associative array
|
||||
*
|
||||
|
Reference in New Issue
Block a user