Skip to content

Instantly share code, notes, and snippets.

@adrexia
Last active March 18, 2020 03:05
Show Gist options
  • Save adrexia/b2e084f3c8fe2d187a8f05b4880274ca to your computer and use it in GitHub Desktop.
Save adrexia/b2e084f3c8fe2d187a8f05b4880274ca to your computer and use it in GitHub Desktop.
folder.php
<?php
private static $has_one = array(
'ImagePath' => 'Folder'
);
// in settings
$fields->insertBefore(
new TreeDropdownField(
"ImagePathID",
'Folder',
"Folder"
),'Content');
// in getcmsfields
if($this->ImagePath()->exists()) {
$file->setFolderName(str_replace('assets', '', $this->ImagePath()->RelativeLink()));
} else {
$file->setFolderName('Uploads/Splash-Images');
}
// silverstripe 4 setting folder name is a bit more complicated
public function setFolder($upload) {
$name = $this->getFolderPath($this->ImagePath());
$upload->setFolderName($name);
return $upload;
}
/**
* Generate the folder path of the folder
* @param $parent
* @return string
*/
public function getFolderPath($folder)
{
$name = [];
array_push($name, $folder->Name);
while (($folder = $folder->Parent()) && $folder->exists()) {
array_push($name, $folder->Name);
}
$name = array_reverse($name);
return implode('/', $name);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment