logo

Get or Set config value programmatically Drupal 8

21
September

Get or Set config value programmatically Drupal 8
By: Anonymous | Published On: Wed, 09/21/2022 - 13:56

Get value of variable from configuration form

You can get config value in any custom code using following method

Example 1

$phone = \Drupal::config('socialmedia.settings')->get('phone');

In above example "socialmedia.settings" is const which has been defined in configuration form and "phone" is variable where stored value

Will get value which have beed set through configuration form.

Example 2

$slogan = \Drupal::config('system.site')->get('slogan');

Set value in variable of configuration form

		$config = \Drupal::service('config.factory')->getEditable('socialmedia.settings');
		$config->set('phone', '9999999999')->save();

In above example "socialmedia.settings" is const which has been defined in configuration form and "phone" is variable where will store value

Need Help ?