mirror of
https://github.com/processwire/processwire.git
synced 2025-08-23 14:56:51 +02:00
Update FieldtypePage to support a runtime option to create Page references that don't exist when value is set by title. This is to accommodate a feature in ImportPagesCSV module.
This commit is contained in:
@@ -494,7 +494,7 @@ class FieldtypePage extends FieldtypeMulti implements Module, ConfigurableModule
|
|||||||
// string has been normalized to newline separated only
|
// string has been normalized to newline separated only
|
||||||
$values = explode("\n", $values);
|
$values = explode("\n", $values);
|
||||||
foreach($values as $str) {
|
foreach($values as $str) {
|
||||||
$v = $this->sanitizeValueString($page, $field, $str); // recursive
|
$v = $this->sanitizeValueString($page, $field, trim($str)); // recursive
|
||||||
if($v && $v->id) {
|
if($v && $v->id) {
|
||||||
if(!$result) $result = $this->wire('pages')->newPageArray();
|
if(!$result) $result = $this->wire('pages')->newPageArray();
|
||||||
$result->add($v);
|
$result->add($v);
|
||||||
@@ -503,12 +503,30 @@ class FieldtypePage extends FieldtypeMulti implements Module, ConfigurableModule
|
|||||||
|
|
||||||
} else if($parent_id) {
|
} else if($parent_id) {
|
||||||
// set by title
|
// set by title
|
||||||
|
$value = trim($value);
|
||||||
$parentIDs = is_array($parent_id) ? implode('|', $parent_id) : $parent_id;
|
$parentIDs = is_array($parent_id) ? implode('|', $parent_id) : $parent_id;
|
||||||
$result = $this->wire('pages')->get("parent_id=$parentIDs, title=" . $this->wire('sanitizer')->selectorValue($value));
|
// find by title
|
||||||
// set by name
|
$pageTitle = $this->wire('sanitizer')->selectorValue($value);
|
||||||
if(!$result->id) $result = $this->wire('pages')->get("parent_id=$parentIDs, name=" .
|
$result = $this->wire('pages')->get("parent_id=$parentIDs, title=$pageTitle");
|
||||||
$this->wire('sanitizer')->selectorValue($this->wire('sanitizer')->pageNameUTF8($value)));
|
// if cannot find by title, find by name
|
||||||
|
if(!$result->id) {
|
||||||
|
$pageName = $this->wire('sanitizer')->selectorValue($this->wire('sanitizer')->pageNameUTF8($value));
|
||||||
|
$result = $this->wire('pages')->get("parent_id=$parentIDs, name=$pageName");
|
||||||
|
}
|
||||||
|
if(!$result->id && $field->get('_sanitizeValueString') === 'create' && $field->get('template_id')) {
|
||||||
|
// option to create page if it does not already exist (useful for imports)
|
||||||
|
// to use this, you must $field->set('_sanitizeValueString', 'create'); ahead of time
|
||||||
|
$template = $this->wire('templates')->get((int) $field->get('template_id'));
|
||||||
|
$parent = $this->wire('pages')->get((int) $parent_id);
|
||||||
|
if($template && $parent->id) {
|
||||||
|
$result = $this->wire('pages')->newPage(array('template' => $template));
|
||||||
|
$result->parent = $parent;
|
||||||
|
$result->title = $value;
|
||||||
|
$result->name = $this->wire('sanitizer')->pageNameUTF8($value);
|
||||||
|
$result->save(array('adjustName' => true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// here
|
||||||
} else {
|
} else {
|
||||||
$template_ids = self::getTemplateIDs($field, true);
|
$template_ids = self::getTemplateIDs($field, true);
|
||||||
if(!empty($template_ids)) {
|
if(!empty($template_ids)) {
|
||||||
|
Reference in New Issue
Block a user