vendor/contao/core-bundle/src/Resources/contao/modules/ModuleBreadcrumb.php line 47

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Contao.
  4.  *
  5.  * (c) Leo Feyer
  6.  *
  7.  * @license LGPL-3.0-or-later
  8.  */
  9. namespace Contao;
  10. use Symfony\Component\Routing\Exception\ExceptionInterface;
  11. /**
  12.  * Front end module "breadcrumb".
  13.  */
  14. class ModuleBreadcrumb extends Module
  15. {
  16.     /**
  17.      * Template
  18.      * @var string
  19.      */
  20.     protected $strTemplate 'mod_breadcrumb';
  21.     /**
  22.      * Display a wildcard in the back end
  23.      *
  24.      * @return string
  25.      */
  26.     public function generate()
  27.     {
  28.         $request System::getContainer()->get('request_stack')->getCurrentRequest();
  29.         if ($request && System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest($request))
  30.         {
  31.             $objTemplate = new BackendTemplate('be_wildcard');
  32.             $objTemplate->wildcard '### ' $GLOBALS['TL_LANG']['FMD']['breadcrumb'][0] . ' ###';
  33.             $objTemplate->title $this->headline;
  34.             $objTemplate->id $this->id;
  35.             $objTemplate->link $this->name;
  36.             $objTemplate->href StringUtil::specialcharsUrl(System::getContainer()->get('router')->generate('contao_backend', array('do'=>'themes''table'=>'tl_module''act'=>'edit''id'=>$this->id)));
  37.             return $objTemplate->parse();
  38.         }
  39.         return parent::generate();
  40.     }
  41.     /**
  42.      * Generate the module
  43.      */
  44.     protected function compile()
  45.     {
  46.         /** @var PageModel $objPage */
  47.         global $objPage;
  48.         $type null;
  49.         $pageId $objPage->id;
  50.         $pages = array($objPage);
  51.         $items = array();
  52.         $blnShowUnpublished System::getContainer()->get('contao.security.token_checker')->isPreviewMode();
  53.         // Get all pages up to the root page
  54.         $objPages PageModel::findParentsById($objPage->pid);
  55.         if ($objPages !== null)
  56.         {
  57.             while ($pageId && $type != 'root' && $objPages->next())
  58.             {
  59.                 $type $objPages->type;
  60.                 $pageId $objPages->pid;
  61.                 $pages[] = $objPages->current();
  62.             }
  63.         }
  64.         // Get the first active regular page and display it instead of the root page
  65.         if ($type == 'root')
  66.         {
  67.             $objFirstPage PageModel::findFirstPublishedByPid($objPages->id);
  68.             $items[] = array
  69.             (
  70.                 'isRoot'   => true,
  71.                 'isActive' => false,
  72.                 'href'     => (($objFirstPage !== null) ? $this->getPageFrontendUrl($objFirstPage) : Environment::get('base')),
  73.                 'title'    => StringUtil::specialchars($objPages->pageTitle ?: $objPages->titletrue),
  74.                 'link'     => $objPages->title,
  75.                 'data'     => (($objFirstPage !== null) ? $objFirstPage->row() : array()),
  76.                 'class'    => ''
  77.             );
  78.             array_pop($pages);
  79.         }
  80.         for ($i=(\count($pages)-1); $i>0$i--)
  81.         {
  82.             if (($pages[$i]->hide && !$this->showHidden) || (!$pages[$i]->published && !$blnShowUnpublished))
  83.             {
  84.                 continue;
  85.             }
  86.             // Get href
  87.             switch ($pages[$i]->type)
  88.             {
  89.                 case 'redirect':
  90.                     $href $pages[$i]->url;
  91.                     if (strncasecmp($href'mailto:'7) === 0)
  92.                     {
  93.                         $href StringUtil::encodeEmail($href);
  94.                     }
  95.                     break;
  96.                 case 'forward':
  97.                     if ($pages[$i]->jumpTo)
  98.                     {
  99.                         $objNext PageModel::findPublishedById($pages[$i]->jumpTo);
  100.                     }
  101.                     else
  102.                     {
  103.                         $objNext PageModel::findFirstPublishedRegularByPid($pages[$i]->id);
  104.                     }
  105.                     if ($objNext instanceof PageModel)
  106.                     {
  107.                         $href $this->getPageFrontendUrl($objNext);
  108.                         break;
  109.                     }
  110.                     // no break
  111.                 default:
  112.                     $href $this->getPageFrontendUrl($pages[$i]);
  113.                     break;
  114.             }
  115.             $items[] = array
  116.             (
  117.                 'isRoot'   => false,
  118.                 'isActive' => false,
  119.                 'href'     => $href,
  120.                 'title'    => StringUtil::specialchars($pages[$i]->pageTitle ?: $pages[$i]->titletrue),
  121.                 'link'     => $pages[$i]->title,
  122.                 'data'     => $pages[$i]->row(),
  123.                 'class'    => ''
  124.             );
  125.         }
  126.         // Active article
  127.         if (isset($_GET['articles']))
  128.         {
  129.             $items[] = array
  130.             (
  131.                 'isRoot'   => false,
  132.                 'isActive' => false,
  133.                 'href'     => $this->getPageFrontendUrl($pages[0]),
  134.                 'title'    => StringUtil::specialchars($pages[0]->pageTitle ?: $pages[0]->titletrue),
  135.                 'link'     => $pages[0]->title,
  136.                 'data'     => $pages[0]->row(),
  137.                 'class'    => ''
  138.             );
  139.             list($strSection$strArticle) = explode(':'Input::get('articles')) + array(nullnull);
  140.             if ($strArticle === null)
  141.             {
  142.                 $strArticle $strSection;
  143.             }
  144.             $objArticle ArticleModel::findByIdOrAlias($strArticle);
  145.             $strAlias $objArticle->alias ?: $objArticle->id;
  146.             if ($objArticle->inColumn != 'main')
  147.             {
  148.                 $strAlias $objArticle->inColumn ':' $strAlias;
  149.             }
  150.             if ($objArticle !== null)
  151.             {
  152.                 $items[] = array
  153.                 (
  154.                     'isRoot'   => false,
  155.                     'isActive' => true,
  156.                     'href'     => $this->getPageFrontendUrl($pages[0], '/articles/' $strAlias),
  157.                     'title'    => StringUtil::specialchars($objArticle->titletrue),
  158.                     'link'     => $objArticle->title,
  159.                     'data'     => $objArticle->row(),
  160.                     'class'    => ''
  161.                 );
  162.             }
  163.         }
  164.         // Active page
  165.         else
  166.         {
  167.             $items[] = array
  168.             (
  169.                 'isRoot'   => false,
  170.                 'isActive' => true,
  171.                 'href'     => $this->getPageFrontendUrl($pages[0]),
  172.                 'title'    => StringUtil::specialchars($pages[0]->pageTitle ?: $pages[0]->title),
  173.                 'link'     => $pages[0]->title,
  174.                 'data'     => $pages[0]->row(),
  175.                 'class'    => ''
  176.             );
  177.         }
  178.         // Mark the first element (see #4833)
  179.         $items[0]['class'] = 'first';
  180.         // HOOK: add custom logic
  181.         if (isset($GLOBALS['TL_HOOKS']['generateBreadcrumb']) && \is_array($GLOBALS['TL_HOOKS']['generateBreadcrumb']))
  182.         {
  183.             foreach ($GLOBALS['TL_HOOKS']['generateBreadcrumb'] as $callback)
  184.             {
  185.                 $this->import($callback[0]);
  186.                 $items $this->{$callback[0]}->{$callback[1]}($items$this);
  187.             }
  188.         }
  189.         $this->Template->getSchemaOrgData = static function () use ($items): array
  190.         {
  191.             $jsonLd = array(
  192.                 '@type' => 'BreadcrumbList',
  193.                 'itemListElement' => array()
  194.             );
  195.             $position 0;
  196.             $htmlDecoder System::getContainer()->get('contao.string.html_decoder');
  197.             foreach ($items as $item)
  198.             {
  199.                 $jsonLd['itemListElement'][] = array(
  200.                     '@type' => 'ListItem',
  201.                     'position' => ++$position,
  202.                     'item' => array(
  203.                         '@id' => $item['href'] ?: './',
  204.                         'name' => $htmlDecoder->inputEncodedToPlainText($item['link'])
  205.                     )
  206.                 );
  207.             }
  208.             return $jsonLd;
  209.         };
  210.         $this->Template->items $items;
  211.     }
  212.     private function getPageFrontendUrl(PageModel $pageModel$strParams=null)
  213.     {
  214.         try
  215.         {
  216.             return $pageModel->getFrontendUrl($strParams);
  217.         }
  218.         catch (ExceptionInterface $exception)
  219.         {
  220.             return '';
  221.         }
  222.     }
  223. }
  224. class_alias(ModuleBreadcrumb::class, 'ModuleBreadcrumb');