Drupal - Drupal 8 variable_get

In Drupal 7

 $data =  variable_get('test_content_types');

In Drupal 8

 $data = \Drupal::state()->get('test_content_types'); 

For more information about about "get", "set", "delete" visit - Step 5: How to upgrade D7 variables to D8's state system.


The accepted answer is half the answer. As marcvangend notes, there are two options in Drupal 8 for what used to be stored in the variables table and was stored and retrieved with variable_set() and variable_get(). The first, documented in darol100's answer, is the State API.

The second is the Configuration API and should be used in most cases where you have a configuration form. It requires at minimum in your module a configuration installation file, for example config/install/example.settings.yml. For a single piece of configuration (with multiple potential values), that file could just contain for example:

test_content_types: - article

And then use the value with:

$types = \Drupal::config('example.settings')->get('test_content_types');

Or change the stored values with:

\Drupal::service('config.factory')
  ->getEditable('example.settings')
  ->set('test_content_types', ['article', 'page'])
  ->save();

See also the the Drupal 8 documentation for D7 to D8 configuration upgrades and using configuration in modules.