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
9 
10 class Multiline extends Text
11 {
15  public function extractValue(\Magento\Framework\App\RequestInterface $request)
16  {
17  $value = $this->_getRequestValue($request);
18  if (!is_array($value)) {
19  $value = false;
20  } else {
21  $value = array_map([$this, '_applyInputFilter'], $value);
22  }
23  return $value;
24  }
25 
30  public function validateValue($value)
31  {
32  $errors = [];
33  $attribute = $this->getAttribute();
34 
35  if ($value === false) {
36  // try to load original value and validate it
38  if (!is_array($value)) {
39  $value = explode("\n", $value);
40  }
41  }
42 
43  if (!is_array($value)) {
44  $value = [$value];
45  }
46  for ($i = 0; $i < $attribute->getMultilineCount(); $i++) {
47  if (!isset($value[$i])) {
48  $value[$i] = null;
49  }
50  // validate first line
51  if ($i == 0) {
52  $result = parent::validateValue($value[$i]);
53  if ($result !== true) {
54  $errors = $result;
55  }
56  } else {
57  if (!empty($value[$i])) {
58  $result = parent::validateValue($value[$i]);
59  if ($result !== true) {
60  $errors = array_merge($errors, $result);
61  }
62  }
63  }
64  }
65 
66  if (count($errors) == 0) {
67  return true;
68  }
69  return $errors;
70  }
71 
75  public function compactValue($value)
76  {
77  if (!is_array($value)) {
78  $value = [$value];
79  }
80  return parent::compactValue($value);
81  }
82 
86  public function restoreValue($value)
87  {
88  return $this->compactValue($value);
89  }
90 
95  {
97  if (!is_array($values)) {
98  $values = explode("\n", $values);
99  }
100  $values = array_map([$this, '_applyOutputFilter'], $values);
101  switch ($format) {
102  case \Magento\Customer\Model\Metadata\ElementFactory::OUTPUT_FORMAT_ARRAY:
103  $output = $values;
104  break;
105  case \Magento\Customer\Model\Metadata\ElementFactory::OUTPUT_FORMAT_HTML:
106  $output = implode("<br />", $values);
107  break;
108  case \Magento\Customer\Model\Metadata\ElementFactory::OUTPUT_FORMAT_ONELINE:
109  $output = implode(" ", $values);
110  break;
111  default:
112  $output = implode("\n", $values);
113  break;
114  }
115  return $output;
116  }
117 }
outputValue($format=\Magento\Customer\Model\Metadata\ElementFactory::OUTPUT_FORMAT_TEXT)
Definition: Multiline.php:94
$values
Definition: options.phtml:88
_getRequestValue(\Magento\Framework\App\RequestInterface $request)
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
extractValue(\Magento\Framework\App\RequestInterface $request)
Definition: Multiline.php:15
$i
Definition: gallery.phtml:31
$errors
Definition: overview.phtml:9