Title.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Page;
  7. use Magento\Framework\App;
  8. /**
  9. * Page title
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Title
  15. {
  16. /**
  17. * Default title glue
  18. */
  19. const TITLE_GLUE = ' / ';
  20. /**
  21. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  22. */
  23. private $scopeConfig;
  24. /**
  25. * @var string[]
  26. */
  27. private $prependedValues = [];
  28. /**
  29. * @var string[]
  30. */
  31. private $appendedValues = [];
  32. /**
  33. * @var string
  34. */
  35. private $textValue;
  36. /**
  37. * @param App\Config\ScopeConfigInterface $scopeConfig
  38. */
  39. public function __construct(
  40. App\Config\ScopeConfigInterface $scopeConfig
  41. ) {
  42. $this->scopeConfig = $scopeConfig;
  43. }
  44. /**
  45. * Set page title
  46. *
  47. * @param string $title
  48. * @return $this
  49. */
  50. public function set($title)
  51. {
  52. $this->textValue = $title;
  53. return $this;
  54. }
  55. /**
  56. * Retrieve title element text (encoded)
  57. *
  58. * @return string
  59. */
  60. public function get()
  61. {
  62. return join(self::TITLE_GLUE, $this->build());
  63. }
  64. /**
  65. * Same as getTitle(), but return only first item from chunk
  66. *
  67. * @return mixed
  68. */
  69. public function getShort()
  70. {
  71. $title = $this->build();
  72. return reset($title);
  73. }
  74. /**
  75. * Same as getShort(), but return title without prefix and suffix
  76. * @return mixed
  77. */
  78. public function getShortHeading()
  79. {
  80. $title = $this->build(false);
  81. return reset($title);
  82. }
  83. /**
  84. * @param bool $withConfigValues
  85. * @return array
  86. */
  87. protected function build($withConfigValues = true)
  88. {
  89. return array_merge(
  90. $this->prependedValues,
  91. [$withConfigValues ? $this->addConfigValues($this->textValue) : $this->textValue],
  92. $this->appendedValues
  93. );
  94. }
  95. /**
  96. * @param string $title
  97. * @return string
  98. */
  99. protected function addConfigValues($title)
  100. {
  101. $preparedTitle = $this->scopeConfig->getValue(
  102. 'design/head/title_prefix',
  103. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  104. ) . ' ' . $title . ' ' . $this->scopeConfig->getValue(
  105. 'design/head/title_suffix',
  106. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  107. );
  108. return trim($preparedTitle);
  109. }
  110. /**
  111. * Retrieve default title text
  112. *
  113. * @return string
  114. */
  115. public function getDefault()
  116. {
  117. $defaultTitle = $this->scopeConfig->getValue(
  118. 'design/head/default_title',
  119. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  120. );
  121. return $this->addConfigValues($defaultTitle);
  122. }
  123. /**
  124. * @param string $suffix
  125. * @return void
  126. */
  127. public function append($suffix)
  128. {
  129. $this->appendedValues[] = $suffix;
  130. }
  131. /**
  132. * @param string $prefix
  133. * @return void
  134. */
  135. public function prepend($prefix)
  136. {
  137. array_unshift($this->prependedValues, $prefix);
  138. }
  139. /**
  140. * Unset title
  141. *
  142. * @return void
  143. */
  144. public function unsetValue()
  145. {
  146. $this->textValue = null;
  147. }
  148. }