Contao\PageModel::findParentsById PHP Method

findParentsById() public static method

Find the parent pages of a page
public static findParentsById ( integer $intId ) : Collection | PageModel[] | PageModel | null
$intId integer The page's ID
return Contao\Model\Collection | PageModel[] | PageModel | null A collection of models or null if there are no parent pages
    public static function findParentsById($intId)
    {
        $arrModels = array();
        while ($intId > 0 && ($objPage = static::findByPk($intId)) !== null) {
            $intId = $objPage->pid;
            $arrModels[] = $objPage;
        }
        if (empty($arrModels)) {
            return null;
        }
        return static::createCollection($arrModels, 'tl_page');
    }

Usage Example

 /**
  * Get the details of a page including inherited parameters
  *
  * @return PageModel The page model
  *
  * @throws NoRootPageFoundException If no root page is found
  */
 public function loadDetails()
 {
     // Loaded already
     if ($this->blnDetailsLoaded) {
         return $this;
     }
     // Set some default values
     $this->protected = (bool) $this->protected;
     $this->groups = $this->protected ? \StringUtil::deserialize($this->groups) : false;
     $this->layout = $this->includeLayout ? $this->layout : false;
     $this->mobileLayout = $this->includeLayout ? $this->mobileLayout : false;
     $this->cache = $this->includeCache ? $this->cache : false;
     $pid = $this->pid;
     $type = $this->type;
     $alias = $this->alias;
     $name = $this->title;
     $title = $this->pageTitle ?: $this->title;
     $folderUrl = '';
     $palias = '';
     $pname = '';
     $ptitle = '';
     $trail = array($this->id, $pid);
     // Inherit the settings
     if ($this->type == 'root') {
         $objParentPage = $this;
         // see #4610
     } else {
         // Load all parent pages
         $objParentPage = \PageModel::findParentsById($pid);
         if ($objParentPage !== null) {
             while ($pid > 0 && $type != 'root' && $objParentPage->next()) {
                 $pid = $objParentPage->pid;
                 $type = $objParentPage->type;
                 // Parent title
                 if ($ptitle == '') {
                     $palias = $objParentPage->alias;
                     $pname = $objParentPage->title;
                     $ptitle = $objParentPage->pageTitle ?: $objParentPage->title;
                 }
                 // Page title
                 if ($type != 'root') {
                     $alias = $objParentPage->alias;
                     $name = $objParentPage->title;
                     $title = $objParentPage->pageTitle ?: $objParentPage->title;
                     $folderUrl = basename($alias) . '/' . $folderUrl;
                     $trail[] = $objParentPage->pid;
                 }
                 // Cache
                 if ($objParentPage->includeCache && $this->cache === false) {
                     $this->cache = $objParentPage->cache;
                 }
                 // Layout
                 if ($objParentPage->includeLayout) {
                     if ($this->layout === false) {
                         $this->layout = $objParentPage->layout;
                     }
                     if ($this->mobileLayout === false) {
                         $this->mobileLayout = $objParentPage->mobileLayout;
                     }
                 }
                 // Protection
                 if ($objParentPage->protected && $this->protected === false) {
                     $this->protected = true;
                     $this->groups = \StringUtil::deserialize($objParentPage->groups);
                 }
             }
         }
         // Set the titles
         $this->mainAlias = $alias;
         $this->mainTitle = $name;
         $this->mainPageTitle = $title;
         $this->parentAlias = $palias;
         $this->parentTitle = $pname;
         $this->parentPageTitle = $ptitle;
         $this->folderUrl = $folderUrl;
     }
     // Set the root ID and title
     if ($objParentPage !== null && $objParentPage->type == 'root') {
         $this->rootId = $objParentPage->id;
         $this->rootAlias = $objParentPage->alias;
         $this->rootTitle = $objParentPage->title;
         $this->rootPageTitle = $objParentPage->pageTitle ?: $objParentPage->title;
         $this->domain = $objParentPage->dns;
         $this->rootLanguage = $objParentPage->language;
         $this->language = $objParentPage->language;
         $this->staticFiles = $objParentPage->staticFiles;
         $this->staticPlugins = $objParentPage->staticPlugins;
         $this->dateFormat = $objParentPage->dateFormat;
         $this->timeFormat = $objParentPage->timeFormat;
         $this->datimFormat = $objParentPage->datimFormat;
         $this->adminEmail = $objParentPage->adminEmail;
         // Store whether the root page has been published
         $time = \Date::floorToMinute();
         $this->rootIsPublic = $objParentPage->published && ($objParentPage->start == '' || $objParentPage->start <= $time) && ($objParentPage->stop == '' || $objParentPage->stop > $time + 60);
         $this->rootIsFallback = true;
         $this->rootUseSSL = $objParentPage->useSSL;
         $this->rootFallbackLanguage = $objParentPage->language;
         // Store the fallback language (see #6874)
         if (!$objParentPage->fallback) {
             $this->rootIsFallback = false;
             $this->rootFallbackLanguage = null;
             $objFallback = static::findPublishedFallbackByHostname($objParentPage->dns);
             if ($objFallback !== null) {
                 $this->rootFallbackLanguage = $objFallback->language;
             }
         }
     } elseif (TL_MODE == 'FE' && $this->type != 'root') {
         \System::log('Page ID "' . $this->id . '" does not belong to a root page', __METHOD__, TL_ERROR);
         throw new NoRootPageFoundException('No root page found');
     }
     $this->trail = array_reverse($trail);
     // Do not cache protected pages
     if ($this->protected) {
         $this->cache = 0;
     }
     // Use the global date format if none is set (see #6104)
     if ($this->dateFormat == '') {
         $this->dateFormat = \Config::get('dateFormat');
     }
     if ($this->timeFormat == '') {
         $this->timeFormat = \Config::get('timeFormat');
     }
     if ($this->datimFormat == '') {
         $this->datimFormat = \Config::get('datimFormat');
     }
     // Prevent saving (see #6506 and #7199)
     $this->preventSaving();
     $this->blnDetailsLoaded = true;
     return $this;
 }
All Usage Examples Of Contao\PageModel::findParentsById