We have a requirement that when we save a node that nodes title should be the site name. We used to do it in Drupal 7. But I was not able to find the proper method for it in Drupal 8.

This is what we tried:

$config = \Drupal::service('config.factory')->getEditable('system.site');
$config->set('system.site.name', "Demo Site");

Got it from here: https://www.drupal.org/docs/8/api/configuration-api/simple-configuration-api

Any suggestions?

  • Where, in what hook, are you executing that code? It needs to be just name in $config->set('name', "Demo Site");. – leymannx Jan 3 '19 at 9:00

Nearly :

$config = \Drupal::service('config.factory')->getEditable('system.site');
$config->set('name', "XXXXXXX"); 

Just have to implement hook_node_update to fit your needs.

  • Please don't comment your own answer. Please just update it with a little bit of explanation. – leymannx Jan 3 '19 at 11:22
  • hook_node_presave would be better as hook_node_update does not run when a new node is added. – No Sssweat Jan 3 '19 at 14:49

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.