Optional config voodoo in Drupal 8

’Cause it’s *optional* config, get it? Tune.

Optional config

- views.view.content

Optional config whilst installing Drupal

* Implements hook_install().
function fubar_install() {
$view = entity_load('view', 'fubar_view');
$view->setDescription('The force will be with you, always.');

Dynamically altering optional config

<?phpuse Drupal\views\ViewEntityInterface;/**
* Implements hook_ENTITY_TYPE_presave().
function fubar_view_presave(ViewEntityInterface $view) {
if ($view->isNew() && $view->id() == 'fubar_view') {
$view->setDescription('Do, or do not. There is no try.');

Moar things for your brain

  • hook_install() can never assume the presence of optional config…but it can assume the presence of default config (i.e., the stuff in the config/install directories). That is always imported before hook_install() is invoked. EDIT: This is not true during config sync! See these comments.
  • In fact, you can never depend on the presence of optional config. That’s why it’s optional: it might exist, and it might not. That’s its nature! Remember this, and code defensively.
  • The config_rewrite module, though useful, can throw a monkey wrench into this. Due to the way it works, it might create config entities, even optional ones, before hook_install() is invoked. Even during the Drupal installation process. Beware! (They are, however, working to fix this.)
  • The config system is well-documented. Start here and here. This post is only one of a shitzillion other blog posts about config in D8.
  • This blog post came about because of this Lightning issue. So hats off to Messrs. mortenson and balsama.
  • Thanks to dawehner for reviewing this post for technical accuracy.
  • “NP-complete”, as I understand it, is CompSci-ese for “unbelievably fucking hard to solve”. Linking to the Wikipedia article makes me seem smarter than I really am.




Dispatches from the dimension of demented Drupalists. Also, assorted bullshit.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Phéna Proxima

Phéna Proxima

Dispatches from the dimension of demented Drupalists. Also, assorted bullshit.

More from Medium

File Structure & Organized Code

2.1 Program structure

How to fix “The provided certificate file is not a valid PFX file” with dotnet dev-certs https…

Interface in OOPs -II