Tamer Zoubi @ Drupal 8


Drupal expert | Programmer

variable_get

variable_get and variable_set in Drupal 8

variable_get()/variable_set() and variable_del() API was removed from Drupal 8 in favor of Configuration API.

So in order to save a simple variable in your module firstly you need to decide on a config object name and a name for the settings. Simple settings can be stored in a config object named <module_name>.settings, while for more complex settings it is advisable to use sub-keys.

To get a variable in Drupal 7 you would use:

Tags
variable_get
variable_set
Drupal 8

How to retrieve site wide email address in Drupal 8

In Drupal 7 you would easily retrieve site wide email address through variable:

<?php 
$site_email = variable_get('site_mail', '');
?>

However in Drupal 8 variables are gone in favor of configuration objects. So the same functionality in Drupal 8 would be: 

<?php 
 $system_site_config = \Drupal::config('system.site');
 $site_email = $system_site_config->get('mail');
?>

 

Tags
Drupal 8
config
mail
variable_get