__('Page List Select Multiple', __FILE__), // Module Title
'summary' => __('Selection of multiple pages from a ProcessWire page tree list', __FILE__), // Module Summary
'version' => 102,
'permanent' => true,
);
}
public function init() {
parent::init();
$this->set('parent_id', 0);
$this->set('labelFieldName', 'title');
$this->set('startLabel', $this->_('Add'));
$this->set('cancelLabel', $this->_('Cancel'));
$this->set('selectLabel', $this->_('Select'));
$this->set('unselectLabel', $this->_('Unselect'));
$this->set('moreLabel', $this->_('More'));
$this->set('removeLabel', $this->_('Remove'));
}
protected function renderListItem($label, $value, $class = '') {
if($class) $class = " $class";
$out =
"
" .
// "" .
" " .
"$value" .
"$label " .
"" .
"";
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();
}
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.') . '
';
}
$out = "" . $this->renderListItem("Label", "1", "itemTemplate");
foreach($this->value as $page_id) {
$page = $this->pages->get((int) $page_id);
if(!$page || !$page->id) continue;
$label = $page->getText($this->labelFieldName, true, true);
if(!strlen($label)) $label = $page->name;
$out .= $this->renderListItem($label, $page->id);
}
$out .= "
";
$this->addClass('InputfieldPageListSelectMultipleData');
$attrs = $this->getAttributes();
unset($attrs['value']);
$attrs['data-root'] = $this->parent_id; // rootPageID
$attrs['data-href'] = "#wrap_{$this->id}"; // selectSelectHref
$attrs['data-start'] = $this->startLabel; // selectStartLabel
$attrs['data-cancel'] = $this->cancelLabel; // selectCancelLabel
$attrs['data-select'] = $this->selectLabel; // selectSelectLabel
$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;
}
/**
* Convert the CSV string provide in the $input to an array of ints needed for this fieldtype
*
* @param WireInputData $input
* @return $this
*
*/
public function ___processInput(WireInputData $input) {
parent::___processInput($input);
$value = $this->attr('value');
if(is_array($value)) $value = reset($value);
$value = trim($value);
if(strpos($value, ",") !== false) $value = explode(",", $value);
else if($value) $value = array($value);
else $value = array();
foreach($value as $k => $v) {
$value[$k] = (int) $v;
}
$this->attr('value', $value);
return $this;
}
}