How can I get the parent object of a file?


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());
    $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);
    $pageUri = array();
    foreach ($objectPath as $p) {
        $part = explode('-', $p);
        $part = implode('-', $part);
        //echo "Part: $part<br>";
        array_push($pageUri, $part);
    $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!


returns the file’s page object

1 Like

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


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