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

Add a new FieldtypeHasFiles interface and update FieldtypeFile and FieldtypeImage to implement it. This interface is intended to be implemented by any Fieldtype that works with files to provide a common interface among them.

This commit is contained in:
Ryan Cramer
2021-07-08 11:36:19 -04:00
parent 88ae66e186
commit 498010ad8f
3 changed files with 126 additions and 2 deletions

View File

@@ -166,6 +166,42 @@ interface WireTrackable {
*/
interface FieldtypePageTitleCompatible { }
/**
* Indicates Fieldtype manages files
*
*/
interface FieldtypeHasFiles {
/**
* Whether or not given Page/Field has any files connected with it
*
* @param Page $page
* @param Field $field
* @return bool
*
*/
public function hasFiles(Page $page, Field $field);
/**
* Get array of full path/file for all files managed by given page and field
*
* @param Page $page
* @param Field $field
* @return array
*
*/
public function getFiles(Page $page, Field $field);
/**
* Get path where files are (or would be) stored
*
* @param Page $page
* @param Field $field
* @return string
*
*/
public function getFilesPath(Page $page, Field $field);
}
/**
* Indicates that an Inputfield provides tree selection capabilities

View File

@@ -18,7 +18,7 @@
*
*/
class FieldtypeFile extends FieldtypeMulti implements ConfigurableModule {
class FieldtypeFile extends FieldtypeMulti implements ConfigurableModule, FieldtypeHasFiles {
public static function getModuleInfo() {
return array(
@@ -1580,6 +1580,59 @@ class FieldtypeFile extends FieldtypeMulti implements ConfigurableModule {
'validators' => $validators, // file validators in use indexed by file extension
);
}
/**
* Whether or not given Page/Field has any files connected with it
*
* #pw-internal For FieldtypeHasFiles interface
*
* @param Page $page
* @param Field $field
* @return bool
* @since 3.0.181
*
*/
public function hasFiles(Page $page, Field $field) {
if(!$field->type instanceof FieldtypeFile) return false;
$value = $page->get($field->name);
return ($value instanceof Pagefile || ($value instanceof Pagefiles && $value->count() > 0));
}
/**
* Get array of full path/file for all files managed by given page and field
*
* #pw-internal For FieldtypeHasFiles interface
*
* @param Page $page
* @param Field $field
* @return array
* @since 3.0.181
*
*/
public function getFiles(Page $page, Field $field) {
$value = $page->get($field->name);
if($value instanceof Pagefile) {
/** @var Pagefile $value */
return array($value->filename());
}
if($value instanceof Pagefiles && $value->count()) {
/** @var Pagefiles $value */
return $value->explode('filename');
}
return array();
}
/**
* Get path where files are (or would be) stored
*
* @param Page $page
* @param Field $field
* @return string
*
*/
public function getFilesPath(Page $page, Field $field) {
return PagefilesManager::_path($page);
}
/**
* Field config

View File

@@ -15,7 +15,7 @@
*
*/
class FieldtypeImage extends FieldtypeFile {
class FieldtypeImage extends FieldtypeFile implements FieldtypeHasFiles {
/**
* File schema is configured to store dimensions for image files 'width', 'height', 'ratio' (flag)
@@ -228,6 +228,41 @@ class FieldtypeImage extends FieldtypeFile {
return $value;
}
/**
* Get array of full path/file for all files managed by given page and field (also includes image variations)
*
* #pw-internal For FieldtypeHasFiles interface
*
* @param Page $page
* @param Field $field
* @return array
* @since 3.0.181
*
*/
public function getFiles(Page $page, Field $field) {
$value = $page->get($field->name);
$files = array();
if($value instanceof Pageimage) {
$value = array($value);
} else if(!$value instanceof Pageimages) {
return array();
}
foreach($value as $pageimage) {
$file = $pageimage->filename();
$path = dirname($file) . '/';
$files[] = $file;
$variations = $pageimage->getVariations(array('info' => true, 'verbose' => false));
foreach($variations as $basename) {
$files[] = $path . $basename;
}
}
return $files;
}
/**
* Get Inputfields to configure fields using this Fieldtype
*