Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Are update hooks a problem when the subprofile overrides configuration files? #32

Closed
mrkmiller opened this issue May 15, 2017 · 3 comments
Labels
Milestone

Comments

@mrkmiller
Copy link
Contributor

No description provided.

@mrkmiller
Copy link
Contributor Author

mrkmiller commented May 15, 2017

This is probably the most standard form of reverting configuration back to code:

/** @var \Drupal\config_update\ConfigRevertInterface $config_revert */
$config_revert = \Drupal::service('config_update.config_update');
$config_revert->revert('entity_view_display', 'node.sf_person.teaser'); // revert person teaser

Doing this will revert the configuration wherever it is stored. So it shouldn't matter if I subprofile is overriding config.

Manually setting config could be a little more tricky. What happens if the base profile tries to manually update the "contributor" user role with a new permission when the subprofile has removed it?

// Set Permissions config for contributor users
$config = \Drupal::service('config.factory')->getEditable('user.role.contributor');
$permissions = $config->get('permissions');
$permissions[] = 'new permission setting';
$config->set('permissions', $permissions)->save();

I'll try this out.

@mrkmiller
Copy link
Contributor Author

mrkmiller commented May 15, 2017

Trying to get config for a missing role or any missing config just returns NULL

$config = \Drupal::service('config.factory')->getEditable('user.role.missingrole');
$permissions = $config->get('permissions');
// Returns NULL

So a conditional should be applied in update hooks to see if there is config present before settings new config.

If a random config item is attempted to be fetched that Drupal knows nothing about then an Exception occurs when attempting to save. So a conditional/try-catch is needed.

$config = \Drupal::service('config.factory')->getEditable('randomconfigitems');
$someconfig = $config->get('someconfig');
$someconfig[] = 'new config setting';
$config->set('someconfig', $someconfig)->save();

Throws
Drupal\Core\Config\ConfigNameException: Missing namespace in Config object name randomconfigitems. in Drupal\Core\Config\ConfigBase::validateName() (line 97 of core/lib/Drupal/Core/Config/ConfigBase.php).

To fix this use:

$config = \Drupal::service('config.factory')->getEditable('randomconfigitems');
$someconfig = $config->get('someconfig');
if ($someconfig) {
  $someconfig[] = 'new config setting';
  $config->set('someconfig', $someconfig)->save();
}

or

try {
  $config = \Drupal::service('config.factory')->getEditable('randomconfigitems');
  $someconfig = $config->get('someconfig');
  $someconfig[] = 'new config setting';
  $config->set('someconfig', $someconfig)->save();
} catch (Exception $e) {
  // Do something with the "Missing namespace in Config object name randomconfigitems." error.
}

@mrkmiller
Copy link
Contributor Author

NO

I think config items need to be checked if manually altering config. Otherwise just reverting whole config files is fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant