Skip to content

Instantly share code, notes, and snippets.

@fazni
Forked from WengerK/README.md
Created November 29, 2017 14:25
Show Gist options
  • Save fazni/012f80ee551b8c42e4b18a0325736e47 to your computer and use it in GitHub Desktop.
Save fazni/012f80ee551b8c42e4b18a0325736e47 to your computer and use it in GitHub Desktop.
Drupal 8  —  Differences between Configuration API & State API

Article Ressources - Drupal 8  —  Differences between Configuration API & State API

This is the Gist repository for my article Drupal 8  —  Differences between Configuration API & State API.

Be aware that this article has been wrote for the Blog of Antistatique — Web Agency in Lausanne, Switzerland. A place where I work as Full Stack Web Developer.

Feel free to read it the full article on Medium or check it out on Antistatique.

<?php
// Typical usage of Configuration API
// Retrieve configuration as readonly
$config = \Drupal::config(‘mymodule.foo’);
// Get a value
$val = $config->get(‘key’);
/* The configuration object that was obtained and used in the previous examples
* does not allow you to change configuration. If you want to change configuration,
* you will instead need to get the configuration object
* by making a call to getEditable() on the configuration factory:
*/
$config =\Drupal::service(‘config.factory’)->getEditable(‘mymodule.foo’);
// Set a value
$config->set(‘enabled’, 1);
// Save the configuration
$config->save();
// Delete a value
$config->clear(‘bar.boo’)->save();
<?php
// Typical usage of State API
// Get a value
$val = \Drupal::state()->get(‘key’);
// Get multiple key/value pairs
$pairs = \Drupal::state()->getMultiple($keys);
// Set a value
\Drupal::state()->set(‘key’,’value’);
// Set multiple values
\Drupal::state()->setMultiple($keyvalues);
// Delete a value
\Drupal::state()->delete(‘key’);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment