Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Variable.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Variable\Model;
7 
20 {
21  const TYPE_TEXT = 'text';
22 
23  const TYPE_HTML = 'html';
24 
28  protected $_storeId = 0;
29 
33  protected $_escaper = null;
34 
43  public function __construct(
44  \Magento\Framework\Model\Context $context,
45  \Magento\Framework\Registry $registry,
46  \Magento\Framework\Escaper $escaper,
48  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
49  array $data = []
50  ) {
51  $this->_escaper = $escaper;
52  parent::__construct($context, $registry, $resource, $resourceCollection, $data);
53  }
54 
60  protected function _construct()
61  {
62  parent::_construct();
63  $this->_init(\Magento\Variable\Model\ResourceModel\Variable::class);
64  }
65 
73  public function setStoreId($storeId)
74  {
75  $this->_storeId = $storeId;
76  return $this;
77  }
78 
85  public function getStoreId()
86  {
87  return $this->_storeId;
88  }
89 
97  public function loadByCode($code)
98  {
99  $this->getResource()->loadByCode($this, $code);
100  return $this;
101  }
102 
109  public function getValue($type = null)
110  {
111  if ($type === null) {
113  }
114  if ($type == self::TYPE_TEXT || !strlen((string)$this->getData('html_value'))) {
115  $value = $this->getData('plain_value');
116  //escape html if type is html, but html value is not defined
117  if ($type == self::TYPE_HTML) {
118  $value = nl2br($this->_escaper->escapeHtml($value));
119  }
120  return $value;
121  }
122  return $this->getData('html_value');
123  }
124 
130  public function validate()
131  {
132  if ($this->getCode() && $this->getName()) {
133  $variable = $this->getResource()->getVariableByCode($this->getCode());
134  if (!empty($variable) && $variable['variable_id'] != $this->getId()) {
135  return __('Variable Code must be unique.');
136  }
137  return true;
138  }
139  return __('Validation has failed.');
140  }
141 
148  public function getVariablesOptionArray($withGroup = false)
149  {
150  /* @var $collection \Magento\Variable\Model\ResourceModel\Variable\Collection */
151  $collection = $this->getCollection();
152  $variables = [];
153  foreach ($collection->toOptionArray() as $variable) {
154  $variables[] = [
155  'value' => '{{customVar code=' . $variable['value'] . '}}',
156  'label' => __('%1', $this->_escaper->escapeHtml($variable['label'])),
157  ];
158  }
159  if ($withGroup && $variables) {
160  $variables = [['label' => __('Custom Variables'), 'value' => $variables]];
161  }
162  return $variables;
163  }
164 }
getData($key='', $index=null)
Definition: DataObject.php:119
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$variable
Definition: variable.php:7
$type
Definition: item.phtml:13
getVariablesOptionArray($withGroup=false)
Definition: Variable.php:148
$value
Definition: gender.phtml:16
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Escaper $escaper, \Magento\Variable\Model\ResourceModel\Variable $resource, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Variable.php:43
$code
Definition: info.phtml:12