Skip to content

Commit

Permalink
show stores in grid
Browse files Browse the repository at this point in the history
  • Loading branch information
triongroup committed Apr 27, 2023
1 parent c14622c commit bb483e7
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/CoreShop/Bundle/StoreBundle/CoreExtension/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

namespace CoreShop\Bundle\StoreBundle\CoreExtension;

use InvalidArgumentException;
use CoreShop\Bundle\ResourceBundle\CoreExtension\Select;
use CoreShop\Component\Store\Model\StoreInterface;
use Pimcore\Model\DataObject\ClassDefinition\Helper\OptionsProviderResolver;

/**
* @psalm-suppress InvalidReturnType, InvalidReturnStatement
Expand All @@ -33,6 +35,10 @@ class Store extends Select
*/
public $fieldtype = 'coreShopStore';

public $options = [];

public $optionsProviderClass = '@' . StoreOptionProvider::class;

protected function getRepository()
{
return \Pimcore::getContainer()->get('coreshop.repository.store');
Expand All @@ -57,4 +63,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;
}
}

0 comments on commit bb483e7

Please sign in to comment.