Skip to content

Instantly share code, notes, and snippets.

@WengerK
Last active June 20, 2019 15:21
Show Gist options
  • Save WengerK/822510d2a069331a4bbe84ef55570705 to your computer and use it in GitHub Desktop.
Save WengerK/822510d2a069331a4bbe84ef55570705 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’);
Copy link

ghost commented Dec 28, 2018

What is the main idea behind using this kind of quotes?

$val = $config->get(‘key‘);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment