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

Public Member Functions

 __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=[])
 
 setStoreId ($storeId)
 
 getStoreId ()
 
 loadByCode ($code)
 
 getValue ($type=null)
 
 validate ()
 
 getVariablesOptionArray ($withGroup=false)
 
- Public Member Functions inherited from AbstractModel
 __construct (\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
 
 __sleep ()
 
 __wakeup ()
 
 setIdFieldName ($name)
 
 getIdFieldName ()
 
 getId ()
 
 setId ($value)
 
 isDeleted ($isDeleted=null)
 
 hasDataChanges ()
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 setDataChanges ($value)
 
 getOrigData ($key=null)
 
 setOrigData ($key=null, $data=null)
 
 dataHasChangedFor ($field)
 
 getResourceName ()
 
 getResourceCollection ()
 
 getCollection ()
 
 load ($modelId, $field=null)
 
 beforeLoad ($identifier, $field=null)
 
 afterLoad ()
 
 isSaveAllowed ()
 
 setHasDataChanges ($flag)
 
 save ()
 
 afterCommitCallback ()
 
 isObjectNew ($flag=null)
 
 beforeSave ()
 
 validateBeforeSave ()
 
 getCacheTags ()
 
 cleanModelCache ()
 
 afterSave ()
 
 delete ()
 
 beforeDelete ()
 
 afterDelete ()
 
 afterDeleteCommit ()
 
 getResource ()
 
 getEntityId ()
 
 setEntityId ($entityId)
 
 clearInstance ()
 
 getStoredData ()
 
 getEventPrefix ()
 
- Public Member Functions inherited from DataObject
 __construct (array $data=[])
 
 addData (array $arr)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 getDataByPath ($path)
 
 getDataByKey ($key)
 
 setDataUsingMethod ($key, $args=[])
 
 getDataUsingMethod ($key, $args=null)
 
 hasData ($key='')
 
 toArray (array $keys=[])
 
 convertToArray (array $keys=[])
 
 toXml (array $keys=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 convertToXml (array $arrAttributes=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 toJson (array $keys=[])
 
 convertToJson (array $keys=[])
 
 toString ($format='')
 
 __call ($method, $args)
 
 isEmpty ()
 
 serialize ($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
 
 debug ($data=null, &$objects=[])
 
 offsetSet ($offset, $value)
 
 offsetExists ($offset)
 
 offsetUnset ($offset)
 
 offsetGet ($offset)
 

Data Fields

const TYPE_TEXT = 'text'
 
const TYPE_HTML = 'html'
 

Protected Member Functions

 _construct ()
 
- Protected Member Functions inherited from AbstractModel
 _construct ()
 
 _init ($resourceModel)
 
 _setResourceModel ($resourceName, $collectionName=null)
 
 _getResource ()
 
 _getEventData ()
 
 _beforeLoad ($modelId, $field=null)
 
 _afterLoad ()
 
 _hasModelChanged ()
 
 _getValidatorBeforeSave ()
 
 _createValidatorBeforeSave ()
 
 _getValidationRulesBeforeSave ()
 
 _clearReferences ()
 
 _clearData ()
 
- Protected Member Functions inherited from DataObject
 _getData ($key)
 
 _underscore ($name)
 

Protected Attributes

 $_storeId = 0
 
 $_escaper = null
 
- Protected Attributes inherited from AbstractModel
 $_eventPrefix = 'core_abstract'
 
 $_eventObject = 'object'
 
 $_idFieldName = 'id'
 
 $_hasDataChanges = false
 
 $_origData
 
 $_isDeleted = false
 
 $_resource
 
 $_resourceCollection
 
 $_resourceName
 
 $_collectionName
 
 $_cacheTag = false
 
 $_dataSaveAllowed = true
 
 $_isObjectNew = null
 
 $_validatorBeforeSave = null
 
 $_eventManager
 
 $_cacheManager
 
 $_registry
 
 $_logger
 
 $_appState
 
 $_actionValidator
 
 $storedData = []
 
- Protected Attributes inherited from DataObject
 $_data = []
 

Additional Inherited Members

- Static Protected Attributes inherited from DataObject
static $_underscoreCache = []
 

Detailed Description

Custom variable model

@method string getCode() @method \Magento\Variable\Model\Variable setCode(string $value) @method string getName() @method \Magento\Variable\Model\Variable setName(string $value)

@api

Since
100.0.2

Definition at line 19 of file Variable.php.

Constructor & Destructor Documentation

◆ __construct()

__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 = [] 
)
Parameters
\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
array$data

Definition at line 43 of file Variable.php.

50  {
51  $this->_escaper = $escaper;
52  parent::__construct($context, $registry, $resource, $resourceCollection, $data);
53  }
$resource
Definition: bulk.php:12

Member Function Documentation

◆ _construct()

_construct ( )
protected

Internal Constructor

Returns
void

Definition at line 60 of file Variable.php.

61  {
62  parent::_construct();
63  $this->_init(\Magento\Variable\Model\ResourceModel\Variable::class);
64  }

◆ getStoreId()

getStoreId ( )

Getter

Returns
integer @codeCoverageIgnore

Definition at line 85 of file Variable.php.

86  {
87  return $this->_storeId;
88  }

◆ getValue()

getValue (   $type = null)

Return variable value depend on given type

Parameters
string$type
Returns
string

Definition at line 109 of file Variable.php.

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  }
getData($key='', $index=null)
Definition: DataObject.php:119
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16

◆ getVariablesOptionArray()

getVariablesOptionArray (   $withGroup = false)

Retrieve variables option array

Todo:
: extract method as separate class
Parameters
bool$withGroup
Returns
array

Definition at line 148 of file Variable.php.

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  }
__()
Definition: __.php:13
$variable
Definition: variable.php:7

◆ loadByCode()

loadByCode (   $code)

Load variable by code

Parameters
string$code
Returns
$this @codeCoverageIgnore

Definition at line 97 of file Variable.php.

98  {
99  $this->getResource()->loadByCode($this, $code);
100  return $this;
101  }
$code
Definition: info.phtml:12

◆ setStoreId()

setStoreId (   $storeId)

Setter

Parameters
integer$storeId
Returns
$this @codeCoverageIgnore

Definition at line 73 of file Variable.php.

74  {
75  $this->_storeId = $storeId;
76  return $this;
77  }

◆ validate()

validate ( )

Validation of object data. Checking for unique variable code

Returns
\Magento\Framework\Phrase|bool

Definition at line 130 of file Variable.php.

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  }
__()
Definition: __.php:13
$variable
Definition: variable.php:7

Field Documentation

◆ $_escaper

$_escaper = null
protected

Definition at line 33 of file Variable.php.

◆ $_storeId

$_storeId = 0
protected

Definition at line 28 of file Variable.php.

◆ TYPE_HTML

const TYPE_HTML = 'html'

Definition at line 23 of file Variable.php.

◆ TYPE_TEXT

const TYPE_TEXT = 'text'

Definition at line 21 of file Variable.php.


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