How can I get the parent object of a file?

Hi,

I wonder if it is possible to get the parent object of a file and I even found a solution but I’m not sure is this is the best and shortest way … Here’s what I came up with:

function get_parent($object) {
    
    $objectPath = explode('/', $object->url());
    //a::show($objectPath);
    $pageUriStart = array_search('content', $objectPath)+1;
    //echo "Start bei $pageUriStart<br>";
    $pageUriEnd = count($objectPath)-2;
    //echo "Ende bei bei $pageUriEnd<br>";
    $objectPath = array_slice($objectPath, $pageUriStart, $pageUriEnd-$pageUriStart+1);
    //a::show($objectPath);
    $pageUri = array();
    foreach ($objectPath as $p) {
        $part = explode('-', $p);
        array_shift($part);
        $part = implode('-', $part);
        //echo "Part: $part<br>";
        array_push($pageUri, $part);
        //a::show($pageUri);
    }
    $pageUri = implode('/', $pageUri);
    //echo "URI: $pageUri<br>";
    
    return page($pageUri);
    
}

I saved this in plugins/ as get_perent.php. Is this a good way to make it available anywhere?

Bonus question: How can I transform this into something like $object->getPartent().

Background: Im building a function figure($image) that gets an image file object and prepares the HTML output with all tags and a link to a bigger version of the image. The image is named image.jpg and the bigger version myimage-big.jpg for instance, and I want to be able to finde the bigger image automatically since they will be always named by the scheme “append -big”.

Thanks for your comments!
Tobi

$file->page()

returns the file’s page object

1 Like

Damn … Am I blind or is that missing on the cheat sheet …? :-/

Thanks!

It’s not on the cheat sheet, only in the source code.