diff --git a/wire/modules/Fieldtype/FieldtypeRepeater/InputfieldRepeater.module b/wire/modules/Fieldtype/FieldtypeRepeater/InputfieldRepeater.module index 6feb320a..07acef22 100644 --- a/wire/modules/Fieldtype/FieldtypeRepeater/InputfieldRepeater.module +++ b/wire/modules/Fieldtype/FieldtypeRepeater/InputfieldRepeater.module @@ -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); diff --git a/wire/modules/Inputfield/InputfieldFile/InputfieldFile.module b/wire/modules/Inputfield/InputfieldFile/InputfieldFile.module index c694adfd..4240b830 100644 --- a/wire/modules/Inputfield/InputfieldFile/InputfieldFile.module +++ b/wire/modules/Inputfield/InputfieldFile/InputfieldFile.module @@ -1311,8 +1311,14 @@ class InputfieldFile extends Inputfield implements InputfieldItemList, Inputfiel $pagefiles = $value instanceof Pagefile ? $value->pagefiles : $value; if(!$pagefiles instanceof Pagefiles) { - // no value present on this Inputfield - return false; + 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) { @@ -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;