Commit 54f7313ec8f253bba635adfdc060a4b5becf2789
1 parent
68e0e7c9
breadcrumbs
Showing
2 changed files
with
83 additions
and
2 deletions
Show diff stats
frontend/views/layouts/main.php
| ... | ... | @@ -22,7 +22,7 @@ |
| 22 | 22 | use yii\db\ActiveQuery; |
| 23 | 23 | use yii\helpers\Url; |
| 24 | 24 | use yii\web\View; |
| 25 | - use yii\widgets\Breadcrumbs; | |
| 25 | + use frontend\widgets\SeoBreadcrumbs; | |
| 26 | 26 | |
| 27 | 27 | AppAsset::register($this); |
| 28 | 28 | $user = \Yii::$app->user->identity; |
| ... | ... | @@ -648,7 +648,7 @@ _________________________________________________________ --> |
| 648 | 648 | <h1><?= $seo->h1 ?></h1> |
| 649 | 649 | </div> |
| 650 | 650 | <div class="col-md-5"> |
| 651 | - <?= Breadcrumbs::widget( | |
| 651 | + <?= SeoBreadcrumbs::widget( | |
| 652 | 652 | [ |
| 653 | 653 | 'links' => isset($this->params[ 'breadcrumbs' ]) ? $this->params[ 'breadcrumbs' ] : [], |
| 654 | 654 | ] | ... | ... |
| 1 | +<?php | |
| 2 | + /** | |
| 3 | + * Created by PhpStorm. | |
| 4 | + * User: stes | |
| 5 | + * Date: 18.09.17 | |
| 6 | + * Time: 12:20 | |
| 7 | + * @var $this \frontend\widgets\SeoBreadcrumbs | |
| 8 | + */ | |
| 9 | + | |
| 10 | + namespace frontend\widgets; | |
| 11 | + use yii\base\InvalidConfigException; | |
| 12 | + use yii\helpers\ArrayHelper; | |
| 13 | + use yii\helpers\Html; | |
| 14 | + use yii\helpers\Url; | |
| 15 | + use yii\widgets\Breadcrumbs; | |
| 16 | + | |
| 17 | + class SeoBreadcrumbs extends Breadcrumbs | |
| 18 | + { | |
| 19 | + public $tag = 'ul'; | |
| 20 | + public $options = [ | |
| 21 | + 'class' => 'breadcrumb', | |
| 22 | + 'itemscope' => true, | |
| 23 | + 'itemtype' => " http://schema.org/BreadcrumbList" | |
| 24 | + ]; | |
| 25 | + public $encodeLabels = false; | |
| 26 | + public $itemTemplate = "<li itemprop=\"itemListElement\" itemscope itemtype=\"http://schema.org/ListItem\">{link}<meta itemprop=\"position\" content=\"{number}\" /></li>\n"; | |
| 27 | + public $homeLink = [ | |
| 28 | + 'label' => '<span itemprop="name">Главная</span>', | |
| 29 | + 'url' => '/', | |
| 30 | + 'itemprop' => 'item', | |
| 31 | + ]; | |
| 32 | + | |
| 33 | + public $activeItemTemplate = '<li class="active" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><span itemprop="name">{link}<meta itemprop="position" content="{number}" /></span></li>'; | |
| 34 | + | |
| 35 | + public function run() | |
| 36 | + { | |
| 37 | + if (empty($this->links)) { | |
| 38 | + return; | |
| 39 | + } | |
| 40 | + $links = []; | |
| 41 | + if ($this->homeLink === null) { | |
| 42 | + $links[] = $this->renderItem_([ | |
| 43 | + 'label' => Yii::t('yii', 'Home'), | |
| 44 | + 'url' => Yii::$app->homeUrl, | |
| 45 | + ], $this->itemTemplate,1); | |
| 46 | + } elseif ($this->homeLink !== false) { | |
| 47 | + $links[] = $this->renderItem_($this->homeLink, $this->itemTemplate, 1, true); | |
| 48 | + } | |
| 49 | + foreach ($this->links as $key => $link) { | |
| 50 | + if (!is_array($link)) { | |
| 51 | + $link = ['label' => $link]; | |
| 52 | + } | |
| 53 | + $links[] = $this->renderItem_($link, isset($link['url']) ? $this->itemTemplate : $this->activeItemTemplate, $key + 2); | |
| 54 | + } | |
| 55 | + echo Html::tag($this->tag, implode('', $links), $this->options); | |
| 56 | + } | |
| 57 | + | |
| 58 | + protected function renderItem_($link, $template, $number, $home = false) | |
| 59 | + { | |
| 60 | + $encodeLabel = ArrayHelper::remove($link, 'encode', $this->encodeLabels); | |
| 61 | + if (array_key_exists('label', $link)) { | |
| 62 | + $label = $encodeLabel ? Html::encode($link['label']) : $link['label']; | |
| 63 | + } else { | |
| 64 | + throw new InvalidConfigException('The "label" element is required for each link.'); | |
| 65 | + } | |
| 66 | + if (isset($link['template'])) { | |
| 67 | + $template = $link['template']; | |
| 68 | + } | |
| 69 | + if (isset($link['url'])) { | |
| 70 | + $options = $link; | |
| 71 | + unset($options['template'], $options['label'], $options['url']); | |
| 72 | + if (!$home){ | |
| 73 | + $label = "<span itemprop='name'>".$label."</span>"; | |
| 74 | + } | |
| 75 | + $link = Html::a($label, $link['url'], $options); | |
| 76 | + } else { | |
| 77 | + $link = $label; | |
| 78 | + } | |
| 79 | + return strtr($template, ['{link}' => $link, '{number}' => $number]); | |
| 80 | + } | |
| 81 | + } | |
| 0 | 82 | \ No newline at end of file | ... | ... |