0

I have a standard config entity in Drupal 8. I'm trying to save multiple values in the form of checkboxes.

The form itself looks like this: enter image description here

As you can see it's nothing to special about it.

If I try to save it, the following exception occurs:

InvalidArgumentException: The configuration property user_role.anonymous doesn't exist. in Drupal\Core\Config\Schema\ArrayElement->get() (line 74 of core/lib/Drupal/Core/Config/Schema/ArrayElement.php).


Drupal\Core\Config\StorableConfigBase->castValue('user_role.anonymous', 'anonymous') (Line: 211)
Drupal\Core\Config\StorableConfigBase->castValue('user_role', Array) (Line: 212)
Drupal\Core\Config\Config->save() (Line: 280)
Drupal\Core\Config\Entity\ConfigEntityStorage->doSave('pl_n_6467f20669ddf097337c47aab87e3c', Object) (Line: 392)
Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 259)
Drupal\Core\Config\Entity\ConfigEntityStorage->save(Object) (Line: 364)
Drupal\Core\Entity\Entity->save() (Line: 637)
Drupal\Core\Config\Entity\ConfigEntityBase->save() (Line: 130)
Drupal\pl_notifications\Form\PLNotificationsConfigurationForm->save(Array, Object)
call_user_func_array(Array, Array) (Line: 111)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 51)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 585)
Drupal\Core\Form\FormBuilder->processForm('p_l_notifications_configuration_add_form', Array, Object) (Line: 314)
Drupal\Core\Form\FormBuilder->buildForm('p_l_notifications_configuration_add_form', Object) (Line: 74)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 574)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
call_user_func_array(Object, Array) (Line: 144)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 64)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 656)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Which makes perfect sense since I haven't defined that field in the ConfigEntityType.

However I can't defined it since there may be other roles added later on or rather some other roles removed.

My question is: How can I save multiple values in a config entity?

2

Config (schema) defines two basic types of complex/array definitions.

Mapping: When you have limited, known set of keys, each with a specific type, e.g. a few strings, integers, booleans and so on.

Sequence: When you have a unlimited list of values that all have the same type but with unknown or numeric keys. Each element must have the same type. The top level in a config entity and simple config is always a mapping (the type there is a config_object or config_entity or so, but those all extend from mapping)

In your example, you would define the user roles as type sequence in config schema, and as a standard php array in your config entity class.

You can also combine this in any way you can imagine, you have a mapping, one key is a list, each element is a mapping, and so on.

0

In case anybody has this problem too.

I ended up serializing the values and saving them as a single field.

I don't know if that is the best approach for it but it works for now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.