Skip to main content
Version: 2023.1

Select Types With Dynamic Options

For the select & multiselect datatype you can specify a dynamic options provider class. This allows you to generate a list of valid options on-the-fly instead of using a static list. The select datatype also allows you to define the default option at runtime.

You can also add some additional static data which will be passed to the data provider.

Note that there are two ways to define an options provider.

Either simply specify the class name ...

Select FieldSelect FieldSelect Field

... or the name of a Symfony service (notice the prefix). Select FieldSelect FieldSelect Field

The services.yaml would then look like this one ...

Select FieldSelect FieldSelect Field

Depending on your datatype you have to implement the appropriate interface.

  • Pimcore\Model\DataObject\ClassDefinition\DynamicOptionsProvider\SelectOptionsProviderInterface for the Select data type options,
  • Pimcore\Model\DataObject\ClassDefinition\DynamicOptionsProvider\MultiSelectOptionsProviderInterface for the Multiselect options

Implement the following methods:

  • getOptions should return a list of valid options in the format indicated below
  • getDefaultValue (Select data type only) returning the default value
  • hasStaticOptions should return whether your options are depending on the object context (i.e. different options for different objects) or not. This is especially important for the object grid. For object-context depending option there will be no batch assignment mode. Also, filtering can only be done through a text field instead of the options list.
<?php

namespace Website;

use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\ClassDefinition\DynamicOptionsProvider\SelectOptionsProviderInterface;


class OptionsProvider implements SelectOptionsProviderInterface
{
public function getOptions(array $context, Data $fieldDefinition): array
{
$object = isset($context["object"]) ? $context["object"] : null;
$fieldname = "id: " . ($object ? $object->getId() : "unknown") . " - " .$context["fieldname"];
$result = array(

array("key" => $fieldname .' == A', "value" => 2),
array("key" => $fieldname .' == C', "value" => 4),
array("key" => $fieldname .' == F', "value" => 5)

);
return $result;
}

/**
* Returns the value which is defined in the 'Default value' field
*/
public function getDefaultValue(array $context, Data $fieldDefinition): ?string
{
return $fieldDefinition->getDefaultValue();
}

public function hasStaticOptions(array $context, Data $fieldDefinition): bool
{
return true;
}

}

This will generate the following options.

Select FieldSelect FieldSelect Field

Context Information for the Provider Class

Note that depending the use case not all of the infos will be available. Especially the existence of the object paramater cannot be guaranteed because the provider class will also be called when a class is saved or if you programmatically call $class->getFieldDefinitions(). Layout definition calls can be distinguished from other ones by checking if the purpose parameter is set to layout

The purpose parameter can take the following values:

ValueDescription
nullunknown
layoutedit mode layout
gridconfiggrid configuration - usually no need to specify dynamic options
gridviewgrid view

Object (top-level)

NameDescription
objectthe "object"
fieldnamethe name of the select field (e.g. dynSelect)

Localizedfields

NameDescription
ownerType"localizedfield"
ownerNamethe name of the localized field ("localizedfields")
objectthe "object"
fieldnamethe name of the select field (e.g. dynSelect)

Objectbricks

NameDescription
containerType"objectbrick"
containerKeythe type of the object brick
outerFieldnamethe object's object brick attribute
objectthe "object"
fieldNamethe name of the attribute inside the object brick

Fieldcollections

NameDescription
containerType"fieldcollection"
containerKeythe type of the fieldcollection
subContainerTypesub container type (e.g. localized field inside a field collection)
outerFieldnamethe object's field collection attribute
objectthe "object"
fieldNamethe name of the attribute inside the fieldcollection

Classification Store

NameDescription
ownerType"classificationstore"
ownerNamethe name of the classificationstore attribute
fieldnamethe name of the attribute inside the fieldcollection
groupIdgroup id
keyIdkey id
keyDefinitionthe fielddefinition of the classificationstore attribute