Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Title.php
Go to the documentation of this file.
1 <?php
8 
10 
17 class Title
18 {
22  const TITLE_GLUE = ' / ';
23 
27  private $scopeConfig;
28 
32  private $prependedValues = [];
33 
37  private $appendedValues = [];
38 
42  private $textValue;
43 
47  public function __construct(
48  App\Config\ScopeConfigInterface $scopeConfig
49  ) {
50  $this->scopeConfig = $scopeConfig;
51  }
52 
59  public function set($title)
60  {
61  $this->textValue = $title;
62  return $this;
63  }
64 
70  public function get()
71  {
72  return join(self::TITLE_GLUE, $this->build());
73  }
74 
80  public function getShort()
81  {
82  $title = $this->build();
83  return reset($title);
84  }
85 
90  public function getShortHeading()
91  {
92  $title = $this->build(false);
93  return reset($title);
94  }
95 
100  protected function build($withConfigValues = true)
101  {
102  return array_merge(
103  $this->prependedValues,
104  [$withConfigValues ? $this->addConfigValues($this->textValue) : $this->textValue],
105  $this->appendedValues
106  );
107  }
108 
113  protected function addConfigValues($title)
114  {
115  $preparedTitle = $this->scopeConfig->getValue(
116  'design/head/title_prefix',
117  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
118  ) . ' ' . $title . ' ' . $this->scopeConfig->getValue(
119  'design/head/title_suffix',
120  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
121  );
122  return trim($preparedTitle);
123  }
124 
130  public function getDefault()
131  {
132  $defaultTitle = $this->scopeConfig->getValue(
133  'design/head/default_title',
134  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
135  );
136  return $this->addConfigValues($defaultTitle);
137  }
138 
143  public function append($suffix)
144  {
145  $this->appendedValues[] = $suffix;
146  }
147 
152  public function prepend($prefix)
153  {
154  array_unshift($this->prependedValues, $prefix);
155  }
156 
162  public function unsetValue()
163  {
164  $this->textValue = null;
165  }
166 }
$title
Definition: default.phtml:14
$suffix
Definition: name.phtml:27
$prefix
Definition: name.phtml:25
build($withConfigValues=true)
Definition: Title.php:100
__construct(App\Config\ScopeConfigInterface $scopeConfig)
Definition: Title.php:47