1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-09 16:26:59 +02:00

Add feature request processwire/processwire-requests#457 - support for returning template property values (name template.name, etc) in $pages->findRaw()

This commit is contained in:
Ryan Cramer
2022-09-20 12:20:14 -04:00
parent f510392e7e
commit c3ea89cf03

View File

@@ -23,6 +23,7 @@ class PagesRaw extends Wire {
* *
*/ */
public function __construct(Pages $pages) { public function __construct(Pages $pages) {
parent::__construct();
$this->pages = $pages; $this->pages = $pages;
} }
@@ -302,6 +303,12 @@ class PagesRawFinder extends Wire {
*/ */
protected $childrenFields = array(); protected $childrenFields = array();
/**
* @var array
*
*/
protected $templateFields = array();
/** /**
* @var array * @var array
* *
@@ -561,6 +568,12 @@ class PagesRawFinder extends Wire {
$this->unsetFields['parent_id'] = 'parent_id'; $this->unsetFields['parent_id'] = 'parent_id';
} }
if(count($this->templateFields) && !isset($this->nativeFields['templates_id'])) {
// we need templates_id if finding any template properties
$this->nativeFields['templates_id'] = 'templates_id';
$this->unsetFields['templates_id'] = 'templates_id';
}
// requested native pages table fields/properties // requested native pages table fields/properties
if(count($this->nativeFields) || $this->getAll || $this->getPaths) { if(count($this->nativeFields) || $this->getAll || $this->getPaths) {
// one or more native pages table column(s) requested // one or more native pages table column(s) requested
@@ -582,6 +595,11 @@ class PagesRawFinder extends Wire {
$this->findParent(); $this->findParent();
} }
// requested template fields
if(count($this->templateFields)) {
$this->findTemplate();
}
// remove runtime only fields // remove runtime only fields
if(count($this->unsetFields)) { if(count($this->unsetFields)) {
foreach($this->unsetFields as $name) { foreach($this->unsetFields as $name) {
@@ -615,9 +633,11 @@ class PagesRawFinder extends Wire {
if($this->options['flat']) { if($this->options['flat']) {
$delimiter = is_string($this->options['flat']) ? $this->options['flat'] : '.'; $delimiter = is_string($this->options['flat']) ? $this->options['flat'] : '.';
foreach($this->values as $key => $value) { foreach($this->values as $key => $value) {
if(is_array($value)) {
$this->values[$key] = $this->flattenValues($value, '', $delimiter); $this->values[$key] = $this->flattenValues($value, '', $delimiter);
} }
} }
}
if($this->options['nulls']) { if($this->options['nulls']) {
$this->populateNullValues($this->values); $this->populateNullValues($this->values);
@@ -666,7 +686,7 @@ class PagesRawFinder extends Wire {
// Array where [ 'field' => [ 'subfield' ]] // Array where [ 'field' => [ 'subfield' ]]
$colName = $fieldName; // array $colName = $fieldName; // array
$fieldName = $key; $fieldName = $key;
if($fieldName === 'parent' || $fieldName === 'children') { if($fieldName === 'parent' || $fieldName === 'children' || $fieldName === 'template') {
// passthru // passthru
} else if(in_array($fieldName, $runtimeNames) && !$fields->get($fieldName)) { } else if(in_array($fieldName, $runtimeNames) && !$fields->get($fieldName)) {
// passthru // passthru
@@ -691,7 +711,7 @@ class PagesRawFinder extends Wire {
list($fieldName, $colName) = explode('[', $fieldName, 2); list($fieldName, $colName) = explode('[', $fieldName, 2);
$colName = rtrim($colName, ']'); $colName = rtrim($colName, ']');
} }
if($fieldName === 'parent' || $fieldName === 'children') { if($fieldName === 'parent' || $fieldName === 'children' || $fieldName === 'template') {
// passthru // passthru
} else if(in_array($fieldName, $runtimeNames) && !$fields->get($fieldName)) { } else if(in_array($fieldName, $runtimeNames) && !$fields->get($fieldName)) {
// passthru // passthru
@@ -715,6 +735,9 @@ class PagesRawFinder extends Wire {
// @todo not yet supported // @todo not yet supported
$this->childrenFields[$fullName] = $colName; $this->childrenFields[$fullName] = $colName;
} else if($fieldName === 'template') {
$this->templateFields[$fullName] = $colName;
} else if($fullName === 'url' || $fullName === 'path') { } else if($fullName === 'url' || $fullName === 'path') {
if($this->wire()->modules->isInstalled('PagePaths')) { if($this->wire()->modules->isInstalled('PagePaths')) {
$this->runtimeFields[$fullName] = $fullName; $this->runtimeFields[$fullName] = $fullName;
@@ -1194,6 +1217,48 @@ class PagesRawFinder extends Wire {
} }
} }
/**
* Find and apply values for template.[property]
*
* @since 3.0.206
*
*/
protected function findTemplate() {
$templates = $this->wire()->templates;
$templateFields = $this->templateFields;
$templateData = array();
$templateIds = array();
foreach($this->values as /* $pageId => */ $data) {
$templateId = $data['templates_id'];
if(!isset($templateIds[$templateId])) $templateIds[$templateId] = $templateId;
}
foreach($templateIds as $templateId) {
$template = $templates->get($templateId);
$templateData[$templateId] = array();
foreach($templateFields as /* $fullName => */ $colName) {
if(empty($colName)) $colName = 'name';
$value = $template->get($colName);
if(is_object($value)) continue; // object values not allowed here
$templateData[$templateId][$colName] = $value;
}
}
if(!$this->getMultiple && count($this->templateFields) < 2) {
$colName = reset($this->templateFields);
foreach($templateData as $templateId => $data) {
$templateData[$templateId] = $data[$colName];
}
}
foreach($this->values as $pageId => $data) {
$templateId = $data['templates_id'];
$this->values[$pageId]['template'] = $templateData[$templateId];
}
}
/** /**
* Find runtime generated fields * Find runtime generated fields
* *