Skip to content

Commit

Permalink
support field type "store" in grid
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackbitDevs committed Apr 28, 2023
1 parent c14622c commit 529b81c
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions 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 */
public $options = [];

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

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

0 comments on commit 529b81c

Please sign in to comment.