Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Message Class Reference
Inheritance diagram for Message:
AbstractHelper

Public Member Functions

 __construct (\Magento\Framework\App\Helper\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Framework\View\LayoutFactory $layoutFactory, \Magento\GiftMessage\Model\MessageFactory $giftMessageFactory, \Magento\Framework\Escaper $escaper, $skipMessageCheck=[])
 
 getInline ($type, \Magento\Framework\DataObject $entity, $dontDisplayContainer=false)
 
 isMessagesAllowed ($type, \Magento\Framework\DataObject $entity, $store=null)
 
 getEscapedGiftMessage (\Magento\Framework\DataObject $entity)
 
 getGiftMessageForEntity (\Magento\Framework\DataObject $entity)
 
 getCached ($key)
 
 isCached ($key)
 
 setCached ($key, $value)
 
 getAvailableForQuoteItems ($quote, $store=null)
 
 getAvailableForAddressItems ($items, $store=null)
 
 getGiftMessage ($messageId=null)
 
- Public Member Functions inherited from AbstractHelper
 __construct (Context $context)
 
 isModuleOutputEnabled ($moduleName=null)
 

Data Fields

const XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS = 'sales/gift_options/allow_items'
 
const XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ORDER = 'sales/gift_options/allow_order'
 

Protected Member Functions

 skipPage ($pageType)
 
 _getDependenceFromStoreConfig ($productConfig, $store=null)
 
- Protected Member Functions inherited from AbstractHelper
 _getRequest ()
 
 _getModuleName ()
 
 _getUrl ($route, $params=[])
 

Protected Attributes

 $_nextId = 0
 
 $_innerCache = []
 
 $productRepository
 
 $_layoutFactory
 
 $_giftMessageFactory
 
 $_escaper
 
 $skipMessageCheck = []
 
- Protected Attributes inherited from AbstractHelper
 $_moduleName
 
 $_request
 
 $_moduleManager
 
 $_logger
 
 $_urlBuilder
 
 $_httpHeader
 
 $_eventManager
 
 $_remoteAddress
 
 $urlEncoder
 
 $urlDecoder
 
 $scopeConfig
 
 $_cacheConfig
 

Detailed Description

Gift Message helper @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 15 of file Message.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\App\Helper\Context  $context,
\Magento\Store\Model\StoreManagerInterface  $storeManager,
\Magento\Catalog\Api\ProductRepositoryInterface  $productRepository,
\Magento\Framework\View\LayoutFactory  $layoutFactory,
\Magento\GiftMessage\Model\MessageFactory  $giftMessageFactory,
\Magento\Framework\Escaper  $escaper,
  $skipMessageCheck = [] 
)
Parameters
\Magento\Framework\App\Helper\Context$context
\Magento\Store\Model\StoreManagerInterface$storeManager
\Magento\Catalog\Api\ProductRepositoryInterface$productRepository
\Magento\Framework\View\LayoutFactory$layoutFactory
\Magento\GiftMessage\Model\MessageFactory$giftMessageFactory
\Magento\Framework\Escaper$escaper
array$skipMessageCheck@SuppressWarnings(PHPMD.ExcessiveParameterList)

Definition at line 81 of file Message.php.

89  {
90  $this->_escaper = $escaper;
91  $this->productRepository = $productRepository;
92  $this->_layoutFactory = $layoutFactory;
93  $this->_giftMessageFactory = $giftMessageFactory;
94  $this->skipMessageCheck = $skipMessageCheck;
95  $this->_storeManager = $storeManager;
96 
97  parent::__construct(
98  $context
99  );
100  }
$storeManager

Member Function Documentation

◆ _getDependenceFromStoreConfig()

_getDependenceFromStoreConfig (   $productConfig,
  $store = null 
)
protected

Check availablity of gift messages from store config if flag eq 2.

Parameters
bool$productConfig
\Magento\Store\Model\Store | int | null$store
Returns
bool|string|null

Definition at line 200 of file Message.php.

201  {
202  $result = $this->scopeConfig->getValue(
203  self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS,
204  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
205  $store
206  );
207  if ($productConfig === null || '' === $productConfig || $productConfig == Boolean::VALUE_USE_CONFIG) {
208  return $result;
209  } else {
210  return $productConfig;
211  }
212  }

◆ getAvailableForAddressItems()

getAvailableForAddressItems (   $items,
  $store = null 
)

Check availability for multishipping checkout items

Parameters
array$items
\Magento\Store\Model\Store | int | null$store
Returns
bool @SuppressWarnings(PHPMD.BooleanGetMethodName)

Definition at line 310 of file Message.php.

311  {
312  foreach ($items as $item) {
313  if ($this->isMessagesAllowed('address_item', $item, $store)) {
314  return true;
315  }
316  }
317  return false;
318  }
isMessagesAllowed($type, \Magento\Framework\DataObject $entity, $store=null)
Definition: Message.php:140
$items

◆ getAvailableForQuoteItems()

getAvailableForQuoteItems (   $quote,
  $store = null 
)

Check availability for onepage checkout items

Parameters
array$quote
\Magento\Store\Model\Store | int | null$store
Returns
bool @SuppressWarnings(PHPMD.BooleanGetMethodName)

Definition at line 292 of file Message.php.

293  {
294  foreach ($quote->getAllItems() as $item) {
295  if ($this->isMessagesAllowed('item', $item, $store)) {
296  return true;
297  }
298  }
299  return false;
300  }
$quote
isMessagesAllowed($type, \Magento\Framework\DataObject $entity, $store=null)
Definition: Message.php:140

◆ getCached()

getCached (   $key)

Retrieve internal cached data with specified key.

If cached data not found return null.

Parameters
string$key
Returns
mixed

Definition at line 252 of file Message.php.

253  {
254  if ($this->isCached($key)) {
255  return $this->_innerCache[$key];
256  }
257  return null;
258  }

◆ getEscapedGiftMessage()

getEscapedGiftMessage ( \Magento\Framework\DataObject  $entity)

Retrieve escaped and preformated gift message text for specified entity

Parameters
\Magento\Framework\DataObject$entity
Returns
string|null

Definition at line 220 of file Message.php.

221  {
223  if ($message) {
224  return nl2br($this->_escaper->escapeHtml($message->getMessage()));
225  }
226  return null;
227  }
$message
$entity
Definition: element.phtml:22
getGiftMessageForEntity(\Magento\Framework\DataObject $entity)
Definition: Message.php:235

◆ getGiftMessage()

getGiftMessage (   $messageId = null)

Retrieve gift message with specified id

Parameters
int$messageId
Returns
\Magento\GiftMessage\Model\Message

Definition at line 326 of file Message.php.

327  {
328  $message = $this->_giftMessageFactory->create();
329  if ($messageId !== null) {
330  $message->load($messageId);
331  }
332  return $message;
333  }
$message

◆ getGiftMessageForEntity()

getGiftMessageForEntity ( \Magento\Framework\DataObject  $entity)

Retrieve gift message for entity. If message not exists return null

Parameters
\Magento\Framework\DataObject$entity
Returns
\Magento\GiftMessage\Model\Message

Definition at line 235 of file Message.php.

236  {
237  if ($entity->getGiftMessageId() && !$entity->getGiftMessage()) {
238  $message = $this->getGiftMessage($entity->getGiftMessageId());
239  $entity->setGiftMessage($message);
240  }
241  return $entity->getGiftMessage();
242  }
$message
getGiftMessage($messageId=null)
Definition: Message.php:326
$entity
Definition: element.phtml:22

◆ getInline()

getInline (   $type,
\Magento\Framework\DataObject  $entity,
  $dontDisplayContainer = false 
)

Retrieve inline giftmessage edit form for specified entity

Parameters
string$type
\Magento\Framework\DataObject$entity
bool$dontDisplayContainer
Returns
string

Definition at line 110 of file Message.php.

111  {
112  if (!$this->skipPage($type) && !$this->isMessagesAllowed($type, $entity)) {
113  return '';
114  }
115  return $this->_layoutFactory->create()->createBlock(\Magento\GiftMessage\Block\Message\Inline::class)
116  ->setId('giftmessage_form_' . $this->_nextId++)
117  ->setDontDisplayContainer($dontDisplayContainer)
118  ->setEntity($entity)
119  ->setCheckoutType($type)->toHtml();
120  }
$type
Definition: item.phtml:13
isMessagesAllowed($type, \Magento\Framework\DataObject $entity, $store=null)
Definition: Message.php:140
$entity
Definition: element.phtml:22

◆ isCached()

isCached (   $key)

Check availability for internal cached data with specified key

Parameters
string$key
Returns
bool

Definition at line 266 of file Message.php.

267  {
268  return isset($this->_innerCache[$key]);
269  }

◆ isMessagesAllowed()

isMessagesAllowed (   $type,
\Magento\Framework\DataObject  $entity,
  $store = null 
)

Check if giftmessages is allowed for specified entity.

Parameters
string$type
\Magento\Framework\DataObject$entity
\Magento\Store\Model\Store | int | null$store
Returns
bool|string|null @SuppressWarnings(PHPMD.CyclomaticComplexity)

Definition at line 140 of file Message.php.

141  {
142  if ($type == 'items') {
143  $items = $entity->getAllItems();
144  if (!is_array($items) || empty($items)) {
145  return $this->scopeConfig->getValue(
146  self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS,
147  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
148  $store
149  );
150  }
151  if ($entity instanceof \Magento\Quote\Model\Quote) {
152  $_type = $entity->getIsMultiShipping() ? 'address_item' : 'item';
153  } else {
154  $_type = 'order_item';
155  }
156  foreach ($items as $item) {
157  if ($item->getParentItem()) {
158  continue;
159  }
160  if ($this->isMessagesAllowed($_type, $item, $store)) {
161  return true;
162  }
163  }
164  } elseif ($type == 'item') {
165  return $this->_getDependenceFromStoreConfig($entity->getProduct()->getGiftMessageAvailable(), $store);
166  } elseif ($type == 'order_item') {
167  return $this->_getDependenceFromStoreConfig($entity->getGiftMessageAvailable(), $store);
168  } elseif ($type == 'address_item') {
169  $storeId = is_numeric($store) ? $store : $this->_storeManager->getStore($store)->getId();
170  if (!$this->isCached('address_item_' . $entity->getProductId())) {
171  try {
172  $giftMessageAvailable = $this->productRepository->getById($entity->getProductId(), false, $storeId)
173  ->getGiftMessageAvailable();
174  } catch (\Magento\Framework\Exception\NoSuchEntityException $noEntityException) {
175  $giftMessageAvailable = null;
176  }
177  $this->setCached('address_item_' . $entity->getProductId(), $giftMessageAvailable);
178  }
179  return $this->_getDependenceFromStoreConfig(
180  $this->getCached('address_item_' . $entity->getProductId()),
181  $store
182  );
183  } else {
184  return $this->scopeConfig->getValue(
185  self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ORDER,
186  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
187  $store
188  );
189  }
190  return false;
191  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$type
Definition: item.phtml:13
isMessagesAllowed($type, \Magento\Framework\DataObject $entity, $store=null)
Definition: Message.php:140
_getDependenceFromStoreConfig($productConfig, $store=null)
Definition: Message.php:200
$entity
Definition: element.phtml:22
$items

◆ setCached()

setCached (   $key,
  $value 
)

Set internal cache data with specified key

Parameters
string$key
mixed$value
Returns
$this

Definition at line 278 of file Message.php.

279  {
280  $this->_innerCache[$key] = $value;
281  return $this;
282  }
$value
Definition: gender.phtml:16

◆ skipPage()

skipPage (   $pageType)
protected
Parameters
string$pageType
Returns
bool

Definition at line 126 of file Message.php.

127  {
128  return in_array($pageType, $this->skipMessageCheck);
129  }

Field Documentation

◆ $_escaper

$_escaper
protected

Definition at line 57 of file Message.php.

◆ $_giftMessageFactory

$_giftMessageFactory
protected

Definition at line 52 of file Message.php.

◆ $_innerCache

$_innerCache = []
protected

Definition at line 37 of file Message.php.

◆ $_layoutFactory

$_layoutFactory
protected

Definition at line 47 of file Message.php.

◆ $_nextId

$_nextId = 0
protected

Definition at line 30 of file Message.php.

◆ $productRepository

$productRepository
protected

Definition at line 42 of file Message.php.

◆ $skipMessageCheck

$skipMessageCheck = []
protected

Definition at line 64 of file Message.php.

◆ XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS

const XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS = 'sales/gift_options/allow_items'

Gift messages allow section in configuration

Definition at line 21 of file Message.php.

◆ XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ORDER

const XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ORDER = 'sales/gift_options/allow_order'

Definition at line 23 of file Message.php.


The documentation for this class was generated from the following file: