forked from pimcore/pimcore
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature]: Manage select options via interface
Add Select Options management interface via Settings > Data Objects submenu. Add selectoptions permission to allow access to management interface. Add options source selection to select fields definition to switch between direct configuration, select options or class / service configuration. Generate configuration file and enum to store select options. Introduce OptionsProviderInterface to detect field definitions using provider. Introduce OptionsProviderTrait to share properties between select types. Update classes rebuild command to also rebuild select options. Add PHP and JavaScript reserved words helper classes. Add and update documentation. Relates to pimcore#3409
- Loading branch information
1 parent
e7c5448
commit 887b2d5
Showing
45 changed files
with
1,665 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
bundles/CoreBundle/src/Migrations/Version20230221073317.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\CoreBundle\Migrations; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\Migrations\AbstractMigration; | ||
|
||
/** | ||
* Auto-generated Migration: Please modify to your needs! | ||
*/ | ||
final class Version20230221073317 extends AbstractMigration | ||
{ | ||
public function getDescription(): string | ||
{ | ||
return 'Add selectoptions user permission'; | ||
} | ||
|
||
public function up(Schema $schema): void | ||
{ | ||
$this->addSql("INSERT IGNORE INTO users_permission_definitions (`key`) VALUES('selectoptions');"); | ||
} | ||
|
||
public function down(Schema $schema): void | ||
{ | ||
$this->addSql("DELETE FROM users_permission_definitions WHERE `key` = 'selectoptions'"); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
bundles/CoreBundle/src/OptionsProvider/SelectOptionsOptionsProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\CoreBundle\OptionsProvider; | ||
|
||
use Pimcore\Model\DataObject\ClassDefinition\Data; | ||
use Pimcore\Model\DataObject\ClassDefinition\DynamicOptionsProvider\SelectOptionsProviderInterface; | ||
use Pimcore\Model\DataObject\SelectOptions\Config; | ||
use Pimcore\Model\DataObject\SelectOptions\Data\SelectOption; | ||
use Pimcore\Model\DataObject\Service; | ||
|
||
class SelectOptionsOptionsProvider implements SelectOptionsProviderInterface | ||
{ | ||
public function getOptions(array $context, Data $fieldDefinition): array | ||
{ | ||
if (!$fieldDefinition instanceof Data\OptionsProviderInterface) { | ||
return []; | ||
} | ||
|
||
$configurationId = $fieldDefinition->getOptionsProviderData(); | ||
$selectOptionsConfiguration = Config::getById($configurationId); | ||
if ($selectOptionsConfiguration === null) { | ||
throw new \Exception('Missing select options configuration ' . $configurationId, 1677137682677); | ||
} | ||
|
||
return array_map( | ||
fn (SelectOption $selectOption) => [ | ||
'value' => $selectOption->getValue(), | ||
'key' => $selectOption->getLabel(), | ||
], | ||
$selectOptionsConfiguration->getSelectOptions(), | ||
); | ||
} | ||
|
||
public function hasStaticOptions(array $context, Data $fieldDefinition): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function getDefaultValue(array $context, Data $fieldDefinition): ?string | ||
{ | ||
if ($fieldDefinition instanceof Data\Select) { | ||
return $fieldDefinition->getDefaultValue(); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
doc/05_Objects/01_Object_Classes/01_Data_Types/77_Select_Options.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Select Options | ||
|
||
Select options are predefined sets of options which may be used for (multi)select fields. | ||
|
||
![Fieldcollection Configuration](../../../img/classes-datatypes-selectoptions-editor.png) | ||
|
||
The 'Name' column is optional, unless the value can't be converted to a valid PHP enum case. | ||
This applies to values starting with a number or certain symbols. | ||
A name may contain alphanumeric characters and underscores. | ||
|
||
## Field configuration | ||
|
||
Set the options source to 'Select Options' and select one of the select options sets. | ||
|
||
![Fieldcollection Configuration](../../../img/classes-datatypes-selectoptions-selector.png) | ||
|
||
## Working with PHP API | ||
|
||
The configuration in the first screenshot generates the [backed enum](https://www.php.net/manual/en/language.enumerations.backed.php) below. | ||
|
||
```php | ||
<?php | ||
|
||
namespace Pimcore\Model\DataObject\SelectOptions; | ||
|
||
enum TestOptions: string implements \App\SelectOptions\DataInterface, \App\SelectOptions\NameInterface | ||
{ | ||
use \Pimcore\Model\DataObject\SelectOptions\Traits\EnumGetValuesTrait; | ||
use \Pimcore\Model\DataObject\SelectOptions\Traits\EnumTryFromNullableTrait; | ||
use \App\SelectOptions\DataTrait; | ||
use \App\SelectOptions\NameTrait; | ||
|
||
case Ten = 'ten'; | ||
case Twenty = '20'; | ||
case Check = '√'; | ||
case C = '©'; | ||
case Multiple_Word_Value = 'Multiple Word Value'; | ||
|
||
public function getLabel(): string | ||
{ | ||
return match ($this) { | ||
self::Ten => '10', | ||
self::Twenty => 'Twenty', | ||
self::Check => '√', | ||
self::C => '©', | ||
self::Multiple_Word_Value => 'Multiple Word Value', | ||
}; | ||
} | ||
} | ||
``` | ||
|
||
### Retrieve available option values | ||
|
||
Provided by the `EnumGetValuesTrait`. | ||
|
||
```php | ||
TestOptions::getValues(); | ||
``` | ||
|
||
### Map select value to enum | ||
|
||
Provided by the `EnumTryFromNullableTrait`. | ||
|
||
```php | ||
$value = $product->getSelectField(); | ||
$testOption = TestOptions::tryFromNullable($value); | ||
if ($testOption !== null) { | ||
$label = $testOption->getLabel(); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.