Blame view

frontend/widgets/SeoBreadcrumbs.php 3.32 KB
54f7313e   Timur Kastemirov   breadcrumbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  <?php
      /**
       * Created by PhpStorm.
       * User: stes
       * Date: 18.09.17
       * Time: 12:20
       * @var $this \frontend\widgets\SeoBreadcrumbs
       */
      
      namespace frontend\widgets;
      use yii\base\InvalidConfigException;
      use yii\helpers\ArrayHelper;
      use yii\helpers\Html;
      use yii\helpers\Url;
      use yii\widgets\Breadcrumbs;
      
      class SeoBreadcrumbs extends Breadcrumbs
      {
          public $tag = 'ul';
          public $options =  [
                                  'class' => 'breadcrumb',
                                  'itemscope' => true,
                                  'itemtype' => " http://schema.org/BreadcrumbList"
                              ];
          public $encodeLabels = false;
          public $itemTemplate = "<li itemprop=\"itemListElement\" itemscope itemtype=\"http://schema.org/ListItem\">{link}<meta itemprop=\"position\" content=\"{number}\" /></li>\n";
          public $homeLink = [
dd8af2ad   Eugeny Galkovskiy   translates 2
28
              'label' => "<span itemprop='name'>Главная</span>",
54f7313e   Timur Kastemirov   breadcrumbs
29
30
31
32
33
34
35
36
37
38
39
40
41
42
              'url' => '/',
              'itemprop' => 'item',
          ];
          
          public $activeItemTemplate = '<li class="active" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><span itemprop="name">{link}<meta itemprop="position" content="{number}" /></span></li>';
      
          public function run()
          {
              if (empty($this->links)) {
                  return;
              }
              $links = [];
              if ($this->homeLink === null) {
                  $links[] = $this->renderItem_([
dd8af2ad   Eugeny Galkovskiy   translates 2
43
                                                   'label' => Yii::t('app', 'Home'),
54f7313e   Timur Kastemirov   breadcrumbs
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
                                                   'url' => Yii::$app->homeUrl,
                                               ], $this->itemTemplate,1);
              } elseif ($this->homeLink !== false) {
                  $links[] = $this->renderItem_($this->homeLink, $this->itemTemplate, 1, true);
              }
              foreach ($this->links as $key => $link) {
                  if (!is_array($link)) {
                      $link = ['label' => $link];
                  }
                  $links[] = $this->renderItem_($link, isset($link['url']) ? $this->itemTemplate : $this->activeItemTemplate, $key + 2);
              }
              echo Html::tag($this->tag, implode('', $links), $this->options);
          }
      
          protected function renderItem_($link, $template, $number, $home = false)
          {
              $encodeLabel = ArrayHelper::remove($link, 'encode', $this->encodeLabels);
              if (array_key_exists('label', $link)) {
                  $label = $encodeLabel ? Html::encode($link['label']) : $link['label'];
              } else {
                  throw new InvalidConfigException('The "label" element is required for each link.');
              }
              if (isset($link['template'])) {
                  $template = $link['template'];
              }
              if (isset($link['url'])) {
                  $options = $link;
                  unset($options['template'], $options['label'], $options['url']);
                  if (!$home){
                      $label = "<span itemprop='name'>".$label."</span>";
                  }
                  $link = Html::a($label, $link['url'], $options);
              } else {
                  $link = $label;
              }
              return strtr($template, ['{link}' => $link, '{number}' => $number]);
          }
      }