Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Multiline.php
Go to the documentation of this file.
1 <?php
7 
9 
15 class Multiline extends \Magento\Eav\Model\Attribute\Data\Text
16 {
24  {
25  $value = $this->_getRequestValue($request);
26  if (!is_array($value)) {
27  $value = false;
28  } else {
29  $value = array_map([$this, '_applyInputFilter'], $value);
30  }
31  return $value;
32  }
33 
41  public function validateValue($value)
42  {
43  $errors = [];
44  $lines = $this->processValue($value);
45  $attribute = $this->getAttribute();
46 
47  if ($attribute->getIsRequired() && empty($lines)) {
48  $attributeLabel = __($attribute->getStoreLabel());
49  $errors[] = __('"%1" is a required value.', $attributeLabel);
50  }
51 
52  $maxAllowedLineCount = $attribute->getMultilineCount();
53  if (count($lines) > $maxAllowedLineCount) {
54  $attributeLabel = __($attribute->getStoreLabel());
55  $errors[] = __('"%1" cannot contain more than %2 lines.', $attributeLabel, $maxAllowedLineCount);
56  }
57 
58  foreach ($lines as $lineIndex => $line) {
59  // First line must be always validated
60  if ($lineIndex == 0 || !empty($line)) {
61  $result = parent::validateValue($line);
62  if ($result !== true) {
63  $errors = array_merge($errors, $result);
64  }
65  }
66  }
67 
68  return (count($errors) == 0) ? true : $errors;
69  }
70 
77  protected function processValue($value)
78  {
79  if ($value === false) {
80  // try to load original value and validate it
81  $attribute = $this->getAttribute();
82  $entity = $this->getEntity();
83  $value = $entity->getDataUsingMethod($attribute->getAttributeCode());
84  }
85  if (!is_array($value)) {
86  $value = explode("\n", $value);
87  }
88  return $value;
89  }
90 
97  public function compactValue($value)
98  {
99  if (is_array($value)) {
100  $value = trim(implode("\n", $value));
101  }
102  return parent::compactValue($value);
103  }
104 
112  public function restoreValue($value)
113  {
114  return $this->compactValue($value);
115  }
116 
124  {
125  $values = $this->getEntity()->getData($this->getAttribute()->getAttributeCode());
126  if (!is_array($values)) {
127  $values = explode("\n", $values);
128  }
129  $values = array_map([$this, '_applyOutputFilter'], $values);
130  switch ($format) {
131  case \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_ARRAY:
132  $output = $values;
133  break;
134  case \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_HTML:
135  $output = implode("<br />", $values);
136  break;
137  case \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_ONELINE:
138  $output = implode(" ", $values);
139  break;
140  default:
141  $output = implode("\n", $values);
142  break;
143  }
144  return $output;
145  }
146 }
extractValue(RequestInterface $request)
Definition: Multiline.php:23
_getRequestValue(RequestInterface $request)
$values
Definition: options.phtml:88
__()
Definition: __.php:13
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
$entity
Definition: element.phtml:22
outputValue($format=\Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_TEXT)
Definition: Multiline.php:123
$errors
Definition: overview.phtml:9