/
Observer.php
126 lines (115 loc) · 3.4 KB
/
Observer.php
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Persistent\Model;
/**
* Persistent Observer
*
* @SuppressWarnings(PHPMD.LongVariable)
*/
class Observer
{
/**
* Persistent session
*
* @var \Magento\Persistent\Helper\Session
*/
protected $_persistentSession;
/**
* Layout model
*
* @var \Magento\Framework\View\LayoutInterface
*/
protected $_layout;
/**
* Url model
*
* @var \Magento\Framework\UrlInterface
*/
protected $_url;
/**
* @var \Magento\Framework\Escaper
*/
protected $_escaper;
/**
* Customer view helper
*
* @var \Magento\Customer\Helper\View
*/
protected $_customerViewHelper;
/**
* Customer repository
*
* @var \Magento\Customer\Api\CustomerRepositoryInterface
*/
protected $customerRepository;
/**
* Constructor
*
* @param \Magento\Persistent\Helper\Session $persistentSession
* @param \Magento\Framework\UrlInterface $url
* @param \Magento\Framework\View\LayoutInterface $layout
* @param \Magento\Framework\Escaper $escaper
* @param \Magento\Customer\Helper\View $customerViewHelper
* @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
*/
public function __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
) {
$this->_persistentSession = $persistentSession;
$this->_url = $url;
$this->_layout = $layout;
$this->_escaper = $escaper;
$this->_customerViewHelper = $customerViewHelper;
$this->customerRepository = $customerRepository;
}
/**
* Emulate 'welcome' block with persistent data
*
* @param \Magento\Framework\View\Element\AbstractBlock $block
* @return $this
*/
public function emulateWelcomeBlock($block)
{
$escapedName = $this->_escaper->escapeHtml(
$this->_customerViewHelper->getCustomerName(
$this->customerRepository->getById($this->_persistentSession->getSession()->getCustomerId())
),
null
);
$this->_applyAccountLinksPersistentData();
$welcomeMessage = __('Welcome, %1!', $escapedName)
. ' ' . $this->_layout->getBlock('header.additional')->toHtml();
$block->setWelcome($welcomeMessage);
return $this;
}
/**
* Emulate 'account links' block with persistent data
*
* @return void
*/
protected function _applyAccountLinksPersistentData()
{
if (!$this->_layout->getBlock('header.additional')) {
$this->_layout->addBlock('Magento\Persistent\Block\Header\Additional', 'header.additional');
}
}
/**
* Emulate 'top links' block with persistent data
*
* @param \Magento\Framework\View\Element\AbstractBlock $block
* @return void
*/
public function emulateTopLinks($block)
{
$this->_applyAccountLinksPersistentData();
$block->removeLinkByUrl($this->_url->getUrl('customer/account/login'));
}
}