Contao\PageModel::findFirstPublishedRegularByPid PHP Method

findFirstPublishedRegularByPid() public static method

Find the first published regular page by its parent ID
public static findFirstPublishedRegularByPid ( integer $intPid, array $arrOptions = [] ) : PageModel | null
$intPid integer The parent page's ID
$arrOptions array An optional options array
return PageModel | null The model or null if there is no published regular page
    public static function findFirstPublishedRegularByPid($intPid, array $arrOptions = array())
    {
        $t = static::$strTable;
        $arrColumns = array("{$t}.pid=? AND {$t}.type='regular'");
        if (isset($arrOptions['ignoreFePreview']) || !BE_USER_LOGGED_IN) {
            $time = \Date::floorToMinute();
            $arrColumns[] = "({$t}.start='' OR {$t}.start<='{$time}') AND ({$t}.stop='' OR {$t}.stop>'" . ($time + 60) . "') AND {$t}.published='1'";
        }
        if (!isset($arrOptions['order'])) {
            $arrOptions['order'] = "{$t}.sorting";
        }
        return static::findOneBy($arrColumns, $intPid, $arrOptions);
    }

Usage Example

Example #1
0
 /**
  * Redirect to an internal page
  *
  * @param \PageModel $objPage
  */
 public function generate($objPage)
 {
     // Forward to the jumpTo or first published page
     if ($objPage->jumpTo) {
         /** @var \PageModel $objNextPage */
         $objNextPage = $objPage->getRelated('jumpTo');
     } else {
         $objNextPage = \PageModel::findFirstPublishedRegularByPid($objPage->id);
     }
     // Forward page does not exist
     if ($objNextPage === null) {
         $this->log('Forward page ID "' . $objPage->jumpTo . '" does not exist', __METHOD__, TL_ERROR);
         throw new ForwardPageNotFoundException('Forward page not found');
     }
     $strForceLang = null;
     // Check the target page language (see #4706)
     if (\Config::get('addLanguageToUrl')) {
         $objNextPage->loadDetails();
         // see #3983
         $strForceLang = $objNextPage->language;
     }
     $strGet = '';
     $strQuery = \Environment::get('queryString');
     $arrQuery = array();
     // Extract the query string keys (see #5867)
     if ($strQuery != '') {
         $arrChunks = explode('&', $strQuery);
         foreach ($arrChunks as $strChunk) {
             list($k, ) = explode('=', $strChunk, 2);
             $arrQuery[] = $k;
         }
     }
     // Add $_GET parameters
     if (!empty($_GET)) {
         foreach (array_keys($_GET) as $key) {
             if (\Config::get('addLanguageToUrl') && $key == 'language') {
                 continue;
             }
             // Ignore the query string parameters (see #5867)
             if (in_array($key, $arrQuery)) {
                 continue;
             }
             // Ignore the auto_item parameter (see #5886)
             if ($key == 'auto_item') {
                 $strGet .= '/' . \Input::get($key);
             } else {
                 $strGet .= '/' . $key . '/' . \Input::get($key);
             }
         }
     }
     // Append the query string (see #5867)
     if ($strQuery != '') {
         $strQuery = '?' . $strQuery;
     }
     $this->redirect($this->generateFrontendUrl($objNextPage->row(), $strGet, $strForceLang) . $strQuery, $objPage->redirect == 'temporary' ? 302 : 301);
 }
All Usage Examples Of Contao\PageModel::findFirstPublishedRegularByPid