1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-12 17:54:44 +02:00

Add support for more fieldtypes within file/image custom fields, plus improved support for them in repeaters, and an update for PR #160

This commit is contained in:
Ryan Cramer
2021-06-18 10:43:43 -04:00
parent 04115a878f
commit c33897b908
2 changed files with 36 additions and 12 deletions

View File

@@ -234,13 +234,32 @@ class InputfieldRepeater extends Inputfield implements InputfieldItemList {
*
*/
protected function preloadInputfieldAssets($fieldIDs = array()) {
if(empty($fieldIDs)) $fieldIDs = $this->field->get('repeaterFields');
if(!is_array($fieldIDs)) return;
$fields = $this->wire()->fields;
$items = $this->attr('value');
$item = count($items) ? $items->first() : null;
foreach($fieldIDs as $fieldID) {
$field = $this->wire('fields')->get($fieldID);
$field = $fields->get($fieldID);
$fieldtype = $field ? $field->type : null; /** @var Fieldtype $fieldtype */
if(!$item && $fieldtype && $fieldtype instanceof FieldtypeFile) {
// repeater has no items yet and this is a file or image field
if($fieldtype->getFieldsTemplate($field)) {
// if it has custom fields, it needs a real example rather than $this->page substitute
// so we generate the first repeater item as a ready page. it is okay that it replaces
// the null $item for remaining iterations, as having a live item is always preferable
$item = $this->getNextReadyPage(array());
}
}
if($field) try {
// the following forces assets to be loaded
$inputfield = $field->getInputfield($this->page);
$inputfield = $field->getInputfield($item ? $item : $this->page);
if($inputfield) $inputfield->renderReady(null, false);
} catch(\Exception $e) {
$this->warning("Repeater '$this->name' preload '$field': " . $e->getMessage(), Notice::debug);

View File

@@ -1310,10 +1310,16 @@ class InputfieldFile extends Inputfield implements InputfieldItemList, Inputfiel
$value = $this->val();
$pagefiles = $value instanceof Pagefile ? $value->pagefiles : $value;
if(!$pagefiles instanceof Pagefiles) {
if($this->hasPage && $this->hasField) {
$value = $this->hasPage->get($this->hasField->name);
$pagefiles = $value instanceof Pagefile ? $value->pagefiles : $value;
}
if(!$pagefiles instanceof Pagefiles) {
// no value present on this Inputfield
return false;
}
}
if($this->itemFieldgroup === false) {
// item fieldgroup already determined not in use
@@ -1328,19 +1334,18 @@ class InputfieldFile extends Inputfield implements InputfieldItemList, Inputfiel
$this->itemFieldgroup = $template->fieldgroup;
}
if($item) {
$page = $pagefiles->getPage();
if($page && method_exists($page, 'getForPage') && wireInstanceOf($page, 'RepeaterPage')) {
$id = '_' . $this->pagefileId($item, "repeater{$page->id}_");
} else {
$id = '_' . $this->pagefileId($item);
$context = '';
$process = $this->wire()->process;
if($item && $process instanceof WirePageEditor) {
$contextPage = $process->getPage();
if(wireInstanceOf($contextPage, 'RepeaterPage')) {
$context = "repeater{$contextPage->id}_";
}
} else {
$id = '';
}
/** @var Page $page */
$page = $pagefiles->getFieldsPage();
$id = $item ? ('_' . $this->pagefileId($item, $context)) : '';
$inputfields = $this->itemFieldgroup->getPageInputfields($page, $id, '', false);
if(!$inputfields) return false;