1

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 too 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 define it since there may be other roles added later on or rather some other roles removed.

3 Answers 3

4

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.

1

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.

1

I just ran into the same situation with a similar error. The correct resolution for this problem is, that you have to create a config/schema/YOURMODULE.schema.yml file for (especially!) multi-value settings first.

Drupal will otherwise fall back to a "wrong" way of storing these settings if it doesn't know how to handle them. Other scalar settings seem to be handled with a better fallback if a schema file doesn't (yet) exist.

Typically I created the schema file after I had successfully implemented the module, that's not possible in this case.

Here's my code example for a filter with a "checkboxes" setting:

@Filter(
 *   id = "cookie_content_blocker_extras_element_filter",
 *   title = @Translation("Cookie content blocker filter: Wrap elements automatically"),
 *   description = @Translation("Place this filter before 'Cookie content blocker filter'. Wraps selected HTML elements with external content like iframes, embed, object, link, img in <cookiecontentblocker> automatically."),
 *   type = Drupal\filter\Plugin\FilterInterface::TYPE_MARKUP_LANGUAGE,
 *   settings = {
 *     "handle_dom_elements" = {},
 *     "handle_dom_elements_exclude_classes" = "no-cookie-content-blocker",
 *     "src_allowed_local" = 1,
 *     "src_allowed_whitelist" = "",
 *   },
 * )

/config/schema/cookie_content_blocker_extras_element_filter.schema.yml:

filter_settings.cookie_content_blocker_extras_element_filter:
  type: filter
  label: 'Cookie content blocker element filter'
  mapping:
    handle_dom_elements:
      type: sequence
      label: 'HTML element types to auto-block'
      sequence:
        type: string
        label: 'HTML element type'
    handle_dom_elements_exclude_classes:
      type: string
      label: 'Exclude elements by class'
    src_allowed_whitelist:
      type: string
      label: 'Whitelist external sources (Domains)'
    src_allowed_local:
      type: boolean
      label: 'Whitelist local source'

After clearing the cache with this schema.yml the error is gone. Horrible story :D

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.