Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Field.php
Go to the documentation of this file.
1 <?php
9 
14 class Field extends \Magento\Config\Model\Config\Structure\AbstractElement
15 {
20 
26  protected $_backendFactory;
27 
33  protected $_sourceFactory;
34 
40  protected $_commentFactory;
41 
46  protected $_dependencyMapper;
47 
53  protected $_blockFactory;
54 
64  public function __construct(
66  \Magento\Framework\Module\Manager $moduleManager,
67  \Magento\Config\Model\Config\BackendFactory $backendFactory,
69  \Magento\Config\Model\Config\CommentFactory $commentFactory,
70  \Magento\Framework\View\Element\BlockFactory $blockFactory,
71  \Magento\Config\Model\Config\Structure\Element\Dependency\Mapper $dependencyMapper
72  ) {
73  parent::__construct($storeManager, $moduleManager);
74  $this->_backendFactory = $backendFactory;
75  $this->_sourceFactory = $sourceFactory;
76  $this->_commentFactory = $commentFactory;
77  $this->_blockFactory = $blockFactory;
78  $this->_dependencyMapper = $dependencyMapper;
79  }
80 
87  public function getLabel($labelPrefix = '')
88  {
89  $label = '';
90  if ($labelPrefix) {
91  $label .= $this->_translateLabel($labelPrefix) . ' ';
92  }
93  $label .= parent::getLabel();
94  return $label;
95  }
96 
102  public function getHint()
103  {
104  return $this->_getTranslatedAttribute('hint');
105  }
106 
113  public function getComment($currentValue = '')
114  {
115  $comment = '';
116  if (isset($this->_data['comment'])) {
117  if (is_array($this->_data['comment'])) {
118  if (isset($this->_data['comment']['model'])) {
119  $model = $this->_commentFactory->create($this->_data['comment']['model']);
120  $comment = $model->getCommentText($currentValue);
121  }
122  } else {
123  $comment = parent::getComment();
124  }
125  }
126  return $comment;
127  }
128 
134  public function getTooltip()
135  {
136  if (isset($this->_data['tooltip'])) {
137  return $this->_getTranslatedAttribute('tooltip');
138  } elseif (isset($this->_data['tooltip_block'])) {
139  return $this->_blockFactory->createBlock($this->_data['tooltip_block'])->toHtml();
140  }
141  return '';
142  }
143 
149  public function getType()
150  {
151  return isset($this->_data['type']) ? $this->_data['type'] : 'text';
152  }
153 
161  protected function _getRequiredElements($fieldPrefix = '', $elementType = 'group')
162  {
163  $elements = [];
164  if (isset($this->_data['requires'][$elementType])) {
165  if (isset($this->_data['requires'][$elementType]['id'])) {
166  $elements[] = $this->_getPath($this->_data['requires'][$elementType]['id'], $fieldPrefix);
167  } else {
168  foreach ($this->_data['requires'][$elementType] as $element) {
169  $elements[] = $this->_getPath($element['id'], $fieldPrefix);
170  }
171  }
172  }
173  return $elements;
174  }
175 
182  public function getRequiredGroups($fieldPrefix = '')
183  {
184  return $this->_getRequiredElements($fieldPrefix, 'group');
185  }
186 
193  public function getRequiredFields($fieldPrefix = '')
194  {
195  return $this->_getRequiredElements($fieldPrefix, 'field');
196  }
197 
203  public function getFrontendClass()
204  {
205  return isset($this->_data['frontend_class']) ? $this->_data['frontend_class'] : '';
206  }
207 
213  public function hasBackendModel()
214  {
215  return array_key_exists('backend_model', $this->_data) && $this->_data['backend_model'];
216  }
217 
223  public function getBackendModel()
224  {
225  return $this->_backendFactory->create($this->_data['backend_model']);
226  }
227 
233  public function getSectionId()
234  {
235  $parts = explode('/', $this->getConfigPath() ?: $this->getPath());
236  return current($parts);
237  }
238 
244  public function getGroupPath()
245  {
246  return dirname($this->getConfigPath() ?: $this->getPath());
247  }
248 
254  public function getConfigPath()
255  {
256  return isset($this->_data['config_path']) ? $this->_data['config_path'] : null;
257  }
258 
264  public function showInDefault()
265  {
266  return isset($this->_data['showInDefault']) && (int)$this->_data['showInDefault'];
267  }
268 
274  public function showInWebsite()
275  {
276  return isset($this->_data['showInWebsite']) && (int)$this->_data['showInWebsite'];
277  }
278 
284  public function showInStore()
285  {
286  return isset($this->_data['showInStore']) && (int)$this->_data['showInStore'];
287  }
288 
295  public function canRestore()
296  {
297  return isset($this->_data['canRestore']) && (int)$this->_data['canRestore'];
298  }
299 
306  public function populateInput($formField)
307  {
308  $originalData = [];
309  foreach ($this->_data as $key => $value) {
310  if (!is_array($value)) {
311  $originalData[$key] = $value;
312  }
313  }
314  $formField->setOriginalData($originalData);
315  }
316 
322  public function hasValidation()
323  {
324  return isset($this->_data['validate']);
325  }
326 
332  public function getValidation()
333  {
334  return isset($this->_data['validate']) ? $this->_data['validate'] : null;
335  }
336 
342  public function canBeEmpty()
343  {
344  return isset($this->_data['can_be_empty']);
345  }
346 
352  public function hasSourceModel()
353  {
354  return isset($this->_data['source_model']);
355  }
356 
362  public function hasOptions()
363  {
364  return isset($this->_data['source_model']) || isset($this->_data['options']);
365  }
366 
372  public function getOptions()
373  {
374  if (isset($this->_data['source_model'])) {
375  $sourceModel = $this->_data['source_model'];
376  $optionArray = $this->_getOptionsFromSourceModel($sourceModel);
377  return $optionArray;
378  } elseif (isset($this->_data['options']) && isset($this->_data['options']['option'])) {
379  $options = $this->_data['options']['option'];
381  return $options;
382  }
383  return [];
384  }
385 
392  protected function _getStaticOptions(array $options)
393  {
394  foreach (array_keys($options) as $key) {
395  $options[$key]['label'] = $this->_translateLabel($options[$key]['label']);
396  $options[$key]['value'] = $this->_fillInConstantPlaceholders($options[$key]['value']);
397  }
398  return $options;
399  }
400 
407  private function _translateLabel($label)
408  {
409  return __($label);
410  }
411 
418  private function _fillInConstantPlaceholders($value)
419  {
420  if (is_string($value) && preg_match('/^{{(\\\\[A-Z][\\\\A-Za-z\d_]+::[A-Z\d_]+)}}$/', $value, $matches)) {
421  $value = constant($matches[1]);
422  }
423  return $value;
424  }
425 
432  protected function _getOptionsFromSourceModel($sourceModel)
433  {
434  $method = false;
435  if (preg_match('/^([^:]+?)::([^:]+?)$/', $sourceModel, $matches)) {
436  array_shift($matches);
437  list($sourceModel, $method) = array_values($matches);
438  }
439 
440  $sourceModel = $this->_sourceFactory->create($sourceModel);
441  if ($sourceModel instanceof \Magento\Framework\DataObject) {
442  $sourceModel->setPath($this->getPath());
443  }
444  if ($method) {
445  if ($this->getType() == 'multiselect') {
446  $optionArray = $sourceModel->{$method}();
447  } else {
448  $optionArray = [];
449  foreach ($sourceModel->{$method}() as $key => $value) {
450  if (is_array($value)) {
451  $optionArray[] = $value;
452  } else {
453  $optionArray[] = ['label' => $value, 'value' => $key];
454  }
455  }
456  }
457  } else {
458  $optionArray = $sourceModel->toOptionArray($this->getType() == 'multiselect');
459  }
460  return $optionArray;
461  }
462 
470  public function getDependencies($fieldPrefix, $storeCode)
471  {
472  $dependencies = [];
473  if (false == isset($this->_data['depends']['fields'])) {
474  return $dependencies;
475  }
476  $dependencies = $this->_dependencyMapper->getDependencies(
477  $this->_data['depends']['fields'],
478  $storeCode,
479  $fieldPrefix
480  );
481  return $dependencies;
482  }
483 
489  public function isAdvanced()
490  {
491  return isset($this->_data['advanced']) && $this->_data['advanced'];
492  }
493 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$storeManager
__()
Definition: __.php:13
$storeCode
Definition: indexer.php:15
__construct(\Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Module\Manager $moduleManager, \Magento\Config\Model\Config\BackendFactory $backendFactory, \Magento\Config\Model\Config\SourceFactory $sourceFactory, \Magento\Config\Model\Config\CommentFactory $commentFactory, \Magento\Framework\View\Element\BlockFactory $blockFactory, \Magento\Config\Model\Config\Structure\Element\Dependency\Mapper $dependencyMapper)
Definition: Field.php:64
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
$method
Definition: info.phtml:13
_getRequiredElements($fieldPrefix='', $elementType='group')
Definition: Field.php:161
$sourceFactory
Definition: source.php:16
$element
Definition: element.phtml:12