Multiselect.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * Form Element Multiselect Data Model
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Customer\Model\Metadata\Form;
  9. use Magento\Customer\Model\Metadata\ElementFactory;
  10. use Magento\Framework\App\RequestInterface;
  11. class Multiselect extends Select
  12. {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public function extractValue(RequestInterface $request)
  17. {
  18. $values = $this->_getRequestValue($request);
  19. if ($values !== false && !is_array($values)) {
  20. $values = [$values];
  21. }
  22. return $values;
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function compactValue($value)
  28. {
  29. if (is_array($value)) {
  30. foreach ($value as $key => $val) {
  31. $value[$key] = parent::compactValue($val);
  32. }
  33. $value = implode(',', $value);
  34. }
  35. return parent::compactValue($value);
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function outputValue($format = ElementFactory::OUTPUT_FORMAT_TEXT)
  41. {
  42. $values = $this->_value;
  43. if (!is_array($values)) {
  44. $values = explode(',', $values);
  45. }
  46. if (ElementFactory::OUTPUT_FORMAT_ARRAY === $format || ElementFactory::OUTPUT_FORMAT_JSON === $format) {
  47. return $values;
  48. }
  49. $output = [];
  50. foreach ($values as $value) {
  51. if (!$value) {
  52. continue;
  53. }
  54. $output[] = $this->_getOptionText($value);
  55. }
  56. $output = implode(', ', $output);
  57. return $output;
  58. }
  59. }