Last active
March 18, 2020 03:05
-
-
Save adrexia/b2e084f3c8fe2d187a8f05b4880274ca to your computer and use it in GitHub Desktop.
folder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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