2

I have defined a config entity in my module (schema and class that extends ConfigEntityBase) - MyConfigEntity.

When I use config factory to get config, Drupal returns instance of Config or ImmutableConfig. How can I retrieve instance of MyConfigEntity instead?

$config = \Drupal::config('my_module.config.something');
// Now $config is instance of ImmutableConfig.
  • this is a duplicate: see drupal.stackexchange.com/questions/203371/… – oknate Jun 23 '16 at 20:38
  • @oknate No it's not. My question is specifically about config entities. – SiliconMind Jun 23 '16 at 22:38
  • OK, I stand corrected. I didn't know there was a config entity vs. a config. What are config entities used for? – oknate Jun 24 '16 at 14:31
  • Ah, after poking a round, the big advantage for config entities is they are stored in yml and can be exported, where other entities, such as nodes, cannot be. – oknate Jun 24 '16 at 15:18
  • @oknate for example node type is a config entity. Same as taxonomy vocabulary. You also get CRUD and you can basically handle them like any other entity, for example you can reference them in other entities (that's what node does) – SiliconMind Jun 24 '16 at 15:20
4

\Drupal::config()

Retrieves a configuration object.

This is the main entry point to the configuration API.

The configuration API is a different system than the entity API.

$entity = \Drupal::entityTypeManager()
  ->getStorage('my_config_entity')
  ->load($id);

Configuration entity types should be used to store configurable content like content types or other site builder-esque things that you want to deploy from environment to environment that is not managed content (Something a content author would write).

If you're looking for a complex configuration object, then you should define that in config/mymodule.settings.yml and use the configuration API like in your question.

  • Damn, that's yet another dependency. – SiliconMind Jun 23 '16 at 20:46
2

Check this out configFactory.

You just need to do:

\Drupal::configFactory()->getEditable('my_module.config.something');

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.