Multiselect.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model\Attribute\Data;
  7. use Magento\Framework\App\RequestInterface;
  8. /**
  9. * EAV Entity Attribute Multiply select Data Model
  10. *
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. */
  13. class Multiselect extends \Magento\Eav\Model\Attribute\Data\Select
  14. {
  15. /**
  16. * Extract data from request and return value
  17. *
  18. * @param RequestInterface $request
  19. * @return array|string
  20. */
  21. public function extractValue(RequestInterface $request)
  22. {
  23. $values = $this->_getRequestValue($request);
  24. if ($values !== false && !is_array($values)) {
  25. $values = [$values];
  26. }
  27. return $values;
  28. }
  29. /**
  30. * Export attribute value to entity model
  31. *
  32. * @param array|string $value
  33. * @return $this
  34. */
  35. public function compactValue($value)
  36. {
  37. if (is_array($value)) {
  38. $value = implode(',', $value);
  39. }
  40. return parent::compactValue($value);
  41. }
  42. /**
  43. * Return formatted attribute value from entity model
  44. *
  45. * @param string $format
  46. * @return array|string
  47. */
  48. public function outputValue($format = \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_TEXT)
  49. {
  50. $values = $this->getEntity()->getData($this->getAttribute()->getAttributeCode());
  51. if (!is_array($values)) {
  52. $values = explode(',', $values);
  53. }
  54. switch ($format) {
  55. case \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_JSON:
  56. case \Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_ARRAY:
  57. $output = $values;
  58. // fall-through intentional
  59. default:
  60. $output = [];
  61. foreach ($values as $value) {
  62. if (!$value) {
  63. continue;
  64. }
  65. $output[] = $this->getAttribute()->getSource()->getOptionText($value);
  66. }
  67. $output = implode(', ', $output);
  68. break;
  69. }
  70. return $output;
  71. }
  72. }