Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Observer.php
Go to the documentation of this file.
1 <?php
7 
13 class Observer
14 {
21 
27  protected $_layout;
28 
34  protected $_url;
35 
39  protected $_escaper;
40 
47 
54 
65  public function __construct(
66  \Magento\Persistent\Helper\Session $persistentSession,
67  \Magento\Framework\UrlInterface $url,
68  \Magento\Framework\View\LayoutInterface $layout,
69  \Magento\Framework\Escaper $escaper,
70  \Magento\Customer\Helper\View $customerViewHelper,
71  \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
72  ) {
73  $this->_persistentSession = $persistentSession;
74  $this->_url = $url;
75  $this->_layout = $layout;
76  $this->_escaper = $escaper;
77  $this->_customerViewHelper = $customerViewHelper;
78  $this->customerRepository = $customerRepository;
79  }
80 
87  public function emulateWelcomeBlock($block)
88  {
89  $customerName = $this->_customerViewHelper->getCustomerName(
90  $this->customerRepository->getById($this->_persistentSession->getSession()->getCustomerId())
91  );
92 
94  $welcomeMessage = __('Welcome, %1!', $customerName);
95  $block->setWelcome($welcomeMessage);
96  return $this;
97  }
98 
104  protected function _applyAccountLinksPersistentData()
105  {
106  if (!$this->_layout->getBlock('header.additional')) {
107  $this->_layout->addBlock(\Magento\Persistent\Block\Header\Additional::class, 'header.additional');
108  }
109  }
110 
117  public function emulateTopLinks($block)
118  {
121  $links = $block->getLinks();
122  $removeLink = $this->_url->getUrl('customer/account/login');
123  foreach ($links as $link) {
124  if ($link->getHref() == $removeLink) {
125  $this->_layout->unsetChild($block->getNameInLayout(), $link->getNameInLayout());
126  }
127  }
128  }
129 }
$welcomeMessage
Definition: header.phtml:12
__()
Definition: __.php:13
$block
Definition: block.php:8
__construct(\Magento\Persistent\Helper\Session $persistentSession, \Magento\Framework\UrlInterface $url, \Magento\Framework\View\LayoutInterface $layout, \Magento\Framework\Escaper $escaper, \Magento\Customer\Helper\View $customerViewHelper, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository)
Definition: Observer.php:65
$persistentSession
Definition: persistent.php:11