Commit e9618e78d6b24a3bfc603dba8b261fd5490beebb
1 parent
2138dbc4
Seo breadcrumbs
Showing
3 changed files
with
98 additions
and
10 deletions
Show diff stats
common/messages/ru/app.php
frontend/views/layouts/main.php
... | ... | @@ -19,7 +19,7 @@ |
19 | 19 | use yii\helpers\Json; |
20 | 20 | use yii\helpers\Url; |
21 | 21 | use yii\web\View; |
22 | - use yii\widgets\Breadcrumbs; | |
22 | + use frontend\widgets\SeoBreadcrumbs; | |
23 | 23 | use \artbox\core\models\Language; |
24 | 24 | |
25 | 25 | AppAsset::register($this); |
... | ... | @@ -426,15 +426,21 @@ _________________________________________________________ --> |
426 | 426 | <span class="main"><?= $seo->h1 ?></span> |
427 | 427 | </div> |
428 | 428 | <div class="col-md-5"> |
429 | - <?= Breadcrumbs::widget( | |
430 | - [ | |
431 | - 'links' => isset($this->params[ 'breadcrumbs' ]) ? $this->params[ 'breadcrumbs' ] : [], | |
432 | - 'homeLink' => [ | |
433 | - 'label' => \Yii::t('yii', 'Home'), | |
434 | - 'url' => \Yii::$app->homeUrl.Language::$current->url, | |
435 | - ] | |
436 | - ] | |
437 | - ) ?> | |
429 | + <?php | |
430 | + if( isset($this->params['breadcrumbs']) && is_array($this->params['breadcrumbs']) ){ | |
431 | + echo SeoBreadcrumbs::widget( | |
432 | + [ | |
433 | + 'links' => $this->params[ 'breadcrumbs' ], | |
434 | + 'encodeLabels' => false, | |
435 | + 'homeLink' => [ | |
436 | + 'label' => "<span itemprop='name'>". \Yii::t('app', "Home") ."</span>", | |
437 | + 'url' => "/ru", | |
438 | + 'itemprop' => 'item', | |
439 | + ] | |
440 | + ] | |
441 | + ); | |
442 | + } | |
443 | + ?> | |
438 | 444 | |
439 | 445 | </div> |
440 | 446 | </div> | ... | ... |
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 | ... | ... |