Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractFrontend.php
Go to the documentation of this file.
1 <?php
13 
18 use Magento\Eav\Model\Cache\Type as CacheType;
20 use Magento\Eav\Model\Entity\Attribute\Source\BooleanFactory;
21 
26 abstract class AbstractFrontend implements \Magento\Eav\Model\Entity\Attribute\Frontend\FrontendInterface
27 {
33  private static $defaultCacheTags = [CacheType::CACHE_TAG, Attribute::CACHE_TAG];
34 
38  private $cache;
39 
43  private $storeManager;
44 
48  private $serializer;
49 
53  private $cacheTags;
54 
60  protected $_attribute;
61 
66 
77  public function __construct(
78  BooleanFactory $attrBooleanFactory,
79  CacheInterface $cache = null,
80  $storeResolver = null,
81  array $cacheTags = null,
82  StoreManagerInterface $storeManager = null,
83  Serializer $serializer = null
84  ) {
85  $this->_attrBooleanFactory = $attrBooleanFactory;
86  $this->cache = $cache ?: ObjectManager::getInstance()->get(CacheInterface::class);
87  $this->cacheTags = $cacheTags ?: self::$defaultCacheTags;
88  $this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(StoreManagerInterface::class);
89  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Serializer::class);
90  }
91 
99  public function setAttribute($attribute)
100  {
101  $this->_attribute = $attribute;
102  return $this;
103  }
104 
111  public function getAttribute()
112  {
113  return $this->_attribute;
114  }
115 
122  public function getInputType()
123  {
124  return $this->getAttribute()->getFrontendInput();
125  }
126 
132  public function getLabel()
133  {
134  $label = $this->getAttribute()->getFrontendLabel();
135  if ($label === null || $label == '') {
136  $label = $this->getAttribute()->getAttributeCode();
137  }
138 
139  return $label;
140  }
141 
147  public function getLocalizedLabel()
148  {
149  return __($this->getLabel());
150  }
151 
158  public function getValue(\Magento\Framework\DataObject $object)
159  {
160  $value = $object->getData($this->getAttribute()->getAttributeCode());
161  if (in_array($this->getConfigField('input'), ['select', 'boolean'])) {
162  $valueOption = $this->getOption($value);
163  if (!$valueOption) {
164  $opt = $this->_attrBooleanFactory->create();
165  $options = $opt->getAllOptions();
166  if ($options) {
167  foreach ($options as $option) {
168  if ($option['value'] === $value) {
169  $valueOption = $option['label'];
170  }
171  }
172  }
173  }
174  $value = $valueOption;
175  } elseif ($this->getConfigField('input') == 'multiselect') {
176  $value = $this->getOption($value);
177  if (is_array($value)) {
178  $value = implode(', ', $value);
179  }
180  }
181 
182  return $value;
183  }
184 
191  public function isVisible()
192  {
193  return $this->getConfigField('frontend_visible');
194  }
195 
201  public function getClass()
202  {
203  $out = [];
204  $out[] = $this->getAttribute()->getFrontendClass();
205  if ($this->getAttribute()->getIsRequired()) {
206  $out[] = 'required-entry';
207  }
208 
209  $inputRuleClass = $this->_getInputValidateClass();
210  if ($inputRuleClass) {
211  $out[] = $inputRuleClass;
212  }
213 
214  $textLengthValidateClasses = $this->getTextLengthValidateClasses();
215  if (!empty($textLengthValidateClasses)) {
216  $out = array_merge($out, $textLengthValidateClasses);
217  }
218 
219  $out = !empty($out) ? implode(' ', array_unique(array_filter($out))) : '';
220  return $out;
221  }
222 
228  protected function _getInputValidateClass()
229  {
230  $class = false;
231  $validateRules = $this->getAttribute()->getValidateRules();
232  if (!empty($validateRules['input_validation'])) {
233  switch ($validateRules['input_validation']) {
234  case 'alphanumeric':
235  $class = 'validate-alphanum';
236  break;
237  case 'numeric':
238  $class = 'validate-digits';
239  break;
240  case 'alpha':
241  $class = 'validate-alpha';
242  break;
243  case 'email':
244  $class = 'validate-email';
245  break;
246  case 'url':
247  $class = 'validate-url';
248  break;
249  case 'length':
250  $class = 'validate-length';
251  break;
252  default:
253  $class = false;
254  break;
255  }
256  }
257  return $class;
258  }
259 
265  private function getTextLengthValidateClasses()
266  {
267  $classes = [];
268 
269  if ($this->_getInputValidateClass()) {
270  $validateRules = $this->getAttribute()->getValidateRules();
271  if (!empty($validateRules['min_text_length'])) {
272  $classes[] = 'minimum-length-' . $validateRules['min_text_length'];
273  }
274  if (!empty($validateRules['max_text_length'])) {
275  $classes[] = 'maximum-length-' . $validateRules['max_text_length'];
276  }
277  if (!empty($classes)) {
278  $classes[] = 'validate-length';
279  }
280  }
281 
282  return $classes;
283  }
284 
292  public function getConfigField($fieldName)
293  {
294  return $this->getAttribute()->getData('frontend_' . $fieldName);
295  }
296 
302  public function getSelectOptions()
303  {
304  $cacheKey = 'attribute-navigation-option-' .
305  $this->getAttribute()->getAttributeCode() . '-' .
306  $this->storeManager->getStore()->getId();
307  $optionString = $this->cache->load($cacheKey);
308  if (false === $optionString) {
309  $options = $this->getAttribute()->getSource()->getAllOptions();
310  $this->cache->save(
311  $this->serializer->serialize($options),
312  $cacheKey,
313  $this->cacheTags
314  );
315  } else {
316  $options = $this->serializer->unserialize($optionString);
317  }
318  return $options;
319  }
320 
327  public function getOption($optionId)
328  {
329  $source = $this->getAttribute()->getSource();
330  if ($source) {
331  return $source->getOptionText($optionId);
332  }
333  return false;
334  }
335 
342  public function getInputRendererClass()
343  {
344  return $this->getAttribute()->getData('frontend_input_renderer');
345  }
346 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$source
Definition: source.php:23
__()
Definition: __.php:13
__construct(BooleanFactory $attrBooleanFactory, CacheInterface $cache=null, $storeResolver=null, array $cacheTags=null, StoreManagerInterface $storeManager=null, Serializer $serializer=null)
$label
Definition: details.phtml:21
$_option $_optionId $class
Definition: date.phtml:13
$value
Definition: gender.phtml:16