From 529b81c207e6da5eda2afdb70de1d3196b195837 Mon Sep 17 00:00:00 2001 From: Jan Walther Date: Fri, 28 Apr 2023 08:35:50 +0200 Subject: [PATCH] support field type "store" in grid --- .../StoreBundle/CoreExtension/Store.php | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/src/CoreShop/Bundle/StoreBundle/CoreExtension/Store.php b/src/CoreShop/Bundle/StoreBundle/CoreExtension/Store.php index 9a702eef66..8b788d61fa 100644 --- a/src/CoreShop/Bundle/StoreBundle/CoreExtension/Store.php +++ b/src/CoreShop/Bundle/StoreBundle/CoreExtension/Store.php @@ -20,6 +20,8 @@ use CoreShop\Bundle\ResourceBundle\CoreExtension\Select; use CoreShop\Component\Store\Model\StoreInterface; +use InvalidArgumentException; +use Pimcore\Model\DataObject\ClassDefinition\Helper\OptionsProviderResolver; /** * @psalm-suppress InvalidReturnType, InvalidReturnStatement @@ -33,6 +35,12 @@ class Store extends Select */ public $fieldtype = 'coreShopStore'; + /** @var array */ + public $options = []; + + /** @var string */ + public $optionsProviderClass = '@'.StoreOptionProvider::class; + protected function getRepository() { return \Pimcore::getContainer()->get('coreshop.repository.store'); @@ -57,4 +65,66 @@ public function getOptionsProviderClass() { return '@' . StoreOptionProvider::class; } + + public function getDataForGrid($data, $object = null, $params = []) + { + $optionsProvider = OptionsProviderResolver::resolveProvider( + $this->getOptionsProviderClass(), + OptionsProviderResolver::MODE_SELECT + ); + + if ($optionsProvider) { + $context = $params['context'] ?? []; + $context['object'] = $object; + if ($object) { + $context['class'] = $object->getClass(); + } + + $context['fieldname'] = $this->getName(); + $options = $optionsProvider->{'getOptions'}($context, $this); + $this->setOptions($options); + + if (isset($params['purpose']) && $params['purpose'] === 'editmode') { + $result = $data?->getId(); + } else { + $result = ['value' => $data?->getId(), 'options' => $this->getOptions()]; + } + + return $result; + } + + return $data?->getId(); + } + + /** + * @param array|null $options + * + * @return $this + */ + public function setOptions(?array $options) + { + if (is_array($options)) { + $this->options = []; + foreach ($options as $option) { + $option = (array)$option; + if (!array_key_exists('key', $option) || !array_key_exists('value', $option)) { + throw new InvalidArgumentException('Please provide select options as associative array with fields "key" and "value"'); + } + + $this->options[] = $option; + } + } else { + $this->options = null; + } + + return $this; + } + + /** + * @return array + */ + public function getOptions() + { + return $this->options; + } }