Skip to content

Commit

Permalink
Merge pull request #2262 from BlackbitDevs/show-stores-in-grid
Browse files Browse the repository at this point in the history
Support "store" field type in grid
  • Loading branch information
dpfaffenbauer committed May 2, 2023
2 parents b32c68f + cc691d2 commit 7a6d012
Showing 1 changed file with 71 additions and 1 deletion.
72 changes: 71 additions & 1 deletion src/CoreShop/Bundle/StoreBundle/CoreExtension/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,6 +35,12 @@ class Store extends Select
*/
public $fieldtype = 'coreShopStore';

/** @var array|null */
public $options = [];

/** @var string */
public $optionsProviderClass = '@'.StoreOptionProvider::class;

protected function getRepository()
{
return \Pimcore::getContainer()->get('coreshop.repository.store');
Expand All @@ -55,6 +63,68 @@ protected function getNullable(): bool

public function getOptionsProviderClass()
{
return '@' . StoreOptionProvider::class;
return $this->optionsProviderClass;
}

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 7a6d012

Please sign in to comment.