Forum

Thread tagged as: Question

pagelist enquiry

I understand this older add-on field type may be unsupported, but I'm currently trying to return the details for a parent page to use as a breadcrumb. The standard navigation system will not suffice as the user is picking a limited number of specific pages to be used as cameo links on the home page.

I can obtain the ID of the parent page using small modification to pagelist, but I'm lost trying to work out the rest:

$parent = $Page->pageParentID();

            if (is_object($Page)) {
                $store['pagePath'] = $Page->pagePath();
                $store['pageTitle'] = $Page->pageTitle();
                $store['pageNavText'] = $Page->pageNavText();
                $store['id'] = $Page->id();
                $store['pageSortPath'] = $Page->pageSortPath();
                $store['pageThumb'] = $Page->pageThumb();
                $store['colour'] = $Page->colour();
                $store['description'] = $Page->description();
                $store['pageParentID'] = 'ID: '.$parent;

                $store['_default'] = $Page->pagePath();
            }

Is it possible to obtain the pageNavText and pagePath of the parent from the parent ID and have it available to a pagelist content type?

Tony Astley

Tony Astley 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, you could use:

$Pages = new PerchContent_Pages;
$ParentPage = $Pages->find($parent);

Thanks Drew, I'll take a look.

Hello Drew,

I've modified the code as follows:

$Pages = new PerchContent_Pages();
            $Page = $Pages->find($this->raw_item);

            $ParentPage = $Pages->find($parent);

            if (is_object($Page)) {
                $store['pagePath'] = $Page->pagePath();
                $store['pageTitle'] = $Page->pageTitle();
                $store['pageNavText'] = $Page->pageNavText();
                $store['id'] = $Page->id();
                $store['pageSortPath'] = $Page->pageSortPath();
                $store['pageThumb'] = $Page->pageThumb();               
                $store['_default'] = $Page->pagePath();


                $store['ParentpageNavText'] = $ParentPage->pageNavText();

            }
        }
        return $store;
    }

    public function get_processed($raw=false)
    {    
        if (is_array($raw) && isset($raw['pagePath'])) { 
            if ($this->Tag->output()) {
                if (isset($raw[$this->Tag->output()])) {
                    return $raw[$this->Tag->output()];
                }
            }
            return $raw['pagePath'];
        }

        return $raw;
    }

However when trying to save I get the error message:

Call to a member function pageNavText()

It refers to the line:

$store['ParentpageNavText'] = $ParentPage->pageNavText();

Any pointers would be appreciated.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Where is $parent defined?

Just seen a potential fix, I'll give it a go and come back shortly...

Go there eventually... I needed the extra if statement for generating the parents values into the $store

Full working script:

<?php
/**
 * A field type for selecting a page from the site tree.
 *
 * Examples:
 *
 * <perch:content id="page" type="pagelist" label="Page" /> (output="path" by default)
 * <perch:content id="page" type="pagelist" label="Page" output="title" />
 * <perch:content id="page" type="pagelist" label="Page" output="navtext" />
 * 
 */
class PerchFieldType_pagelist extends PerchAPI_FieldType
{

    public function render_inputs($details=array())
    {
        $id  = $this->Tag->input_id();
        $val = '';

        if (isset($details[$id]) && $details[$id]!='') {
            $json = $details[$id];
            $val  = $json['id']; 
        }

        if (!class_exists('PerchContent_Pages')) {
            include_once(PERCH_CORE.'/apps/content/PerchContent_Pages.class.php');
            include_once(PERCH_CORE.'/apps/content/PerchContent_Page.class.php');
        }


        $Pages = new PerchContent_Pages();
        $pages = $Pages->get_page_tree();



        $opts   = array();
        $opts[] = array('label'=>'', 'value'=>'');

        if (PerchUtil::count($pages)) {
            foreach($pages as $Item) {               

                $parent = $Item->pageParentID();
                $ParentPage = $Pages->find($parent);

                $depth = $Item->pageDepth()-1;
                if ($depth < 0 ) $depth = 0;         
                $opts[] = array('label'=>str_repeat('-', $depth).' '.$Item->pageNavText(), 'value'=>$Item->id());
            }
        }

        if(PerchUtil::count($opts)) {
            $s = $this->Form->select($id, $opts, $val);
        } else {
            $s = '-';
        }

        return $s;
    }

    public function get_raw($post=false, $Item=false) 
    {
        $store  = array();
        $id     = $this->Tag->id();

        if ($post===false) $post = $_POST;

        if (isset($post[$id])) {
            $this->raw_item = trim($post[$id]);

            if (!class_exists('PerchContent_Pages')) {
                include_once(PERCH_CORE.'/apps/content/PerchContent_Pages.class.php');
                include_once(PERCH_CORE.'/apps/content/PerchContent_Page.class.php');
            }



            $Pages = new PerchContent_Pages();
            $Page = $Pages->find($this->raw_item);

            $parent = $Page->pageParentID();
            $ParentPage = $Pages->find($parent);

            if (is_object($Page)) {
                $store['pagePath']              =   $Page->pagePath();
                $store['pageTitle']             =   $Page->pageTitle();
                $store['pageNavText']           =   $Page->pageNavText();
                $store['id']                    =   $Page->id();
                $store['pageSortPath']          =   $Page->pageSortPath();
                $store['pageThumb']             =   $Page->pageThumb();
                $store['colour']                =   $Page->colour();
                $store['description']           =   $Page->description();

                $store['_default']              =   $Page->pagePath();
            }

            if (is_object($ParentPage)) {
                $store['parentPageNavText']     =   $ParentPage->pageNavText();
                $store['parentPagePath']        =   $ParentPage->pagePath();
                $store['parentPageTitle']       =   $ParentPage->pageTitle();
                $store['parentPageNavText']     =   $ParentPage->pageNavText();
                $store['parentPageID']          =   $ParentPage->id();
                $store['parentPageSortPath']    =   $ParentPage->pageSortPath();
                $store['parentPageThumb']       =   $ParentPage->pageThumb();
                $store['parentPageColour']      =   $ParentPage->colour();
                $store['parentPageDescription'] =   $ParentPage->description();
            }
        }
        return $store;
    }

    public function get_processed($raw=false)
    {    
        if (is_array($raw) && isset($raw['pagePath'])) { 
            if ($this->Tag->output()) {
                if (isset($raw[$this->Tag->output()])) {
                    return $raw[$this->Tag->output()];
                }
            }
            return $raw['pagePath'];
        }

        return $raw;
    }

    public function get_search_text($raw=false)
    {
        return false;
    }
}
?>