1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-24 07:13:08 +02:00

Update FieldtypePage to implement the Fieldtype::__importValue() method for page import support

This commit is contained in:
Ryan Cramer
2017-08-11 10:37:57 -04:00
parent 7ff278a9ff
commit 9e76a2b770
2 changed files with 41 additions and 5 deletions

View File

@@ -288,7 +288,7 @@ class FieldtypePage extends FieldtypeMulti implements Module, ConfigurableModule
if(!empty($options['human'])) return implode("\n", $a);
return $a;
}
protected function exportValuePage(Page $page, Field $field, Page $value, array $options = array()) {
if($page) {}
if($field) {}
@@ -313,6 +313,42 @@ class FieldtypePage extends FieldtypeMulti implements Module, ConfigurableModule
}
return $a;
}
/**
* Import value
*
* @param Page $page
* @param Field $field
* @param array|string $value
* @param array $options
* @return PageArray
*
*/
public function ___importValue(Page $page, Field $field, $value, array $options = array()) {
$pageArray = $this->wire('pages')->newPageArray();
if(empty($value)) return $pageArray;
if(is_string($value)) $value = array($value);
foreach($value as $item) {
if(is_array($item)) {
$path = $item['path'];
} else if(is_string($item)) {
// system option
$path = $item;
}
$p = $this->wire('pages')->get($path);
if(!$p->id) {
$pageArray->error("Unable to find page '$path' to add to field '$field->name'");
} else if(!$this->isValidPage($p, $field, $page)) {
$reason = $page->get('_isValidPage');
$warning = "Page '$p->path' is not allowed by field '$field->name' " . ($reason ? "($reason)" : "");
$pageArray->error($warning);
} else {
$pageArray->add($p);
}
}
return $pageArray;
}
/**
* Format the given value for output.

View File

@@ -220,7 +220,7 @@ class InputfieldPage extends Inputfield implements ConfigurableModule {
if(!$field instanceof Field) throw new WireException('isValidPage requires a valid Field or field name');
if($editPage && $page->id == $editPage->id) {
$editPage->set('_isValidPage', "Page is referencing itself and circular page reference not allowed");
$editPage->setQuietly('_isValidPage', "Page is referencing itself and circular page reference not allowed");
return false; // prevent circular reference
}
@@ -242,7 +242,7 @@ class InputfieldPage extends Inputfield implements ConfigurableModule {
// looks like its okay
} else {
// also fails $pages->cont() check, so definitely not valid
if($editPage) $editPage->set('_isValidPage', "Page $page does not match findPagesSelector: $selector");
if($editPage) $editPage->setQuietly('_isValidPage', "Page $page does not match findPagesSelector: $selector");
$valid = false;
}
}
@@ -265,7 +265,7 @@ class InputfieldPage extends Inputfield implements ConfigurableModule {
$valid = false;
}
if(!$valid && $editPage) {
$editPage->set('_isValidPage', "Page $page does not have required parent $parent_id");
$editPage->setQuietly('_isValidPage', "Page $page does not have required parent $parent_id");
}
} else {
// PHP version prior to 5.3.8
@@ -283,7 +283,7 @@ class InputfieldPage extends Inputfield implements ConfigurableModule {
if(!$hasRequiredTemplate) {
$valid = false;
if($editPage) {
$editPage->set('_isValidPage', "Page $page does not have required template(s): " . implode(',', $template_ids));
$editPage->setQuietly('_isValidPage', "Page $page does not have required template(s): " . implode(',', $template_ids));
}
}