Last updated August 19, 2014. Created on September 17, 2012.
Edited by targoo, benjy, mpp, webchick. Log in to edit this page.

The State API provides a place for developers to store information about the system's state. State information differs from configuration in the following ways:

  • It is specific to an individual environment.
  • You will never want to deploy it between environments.
  • You can reset a system, losing all state. Its configuration remains.

A good example of state is the last time cron was run. This is specific to an environment and has no use in deployment. The state API is a simple system to store this information, which previously would have been stored in the variables system.

Typical usage:

  • 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');

Data which needs to be edited by humans or needs to be shared between environments should use \Drupal::config() instead.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

tedbow’s picture

Can state not used for thing that is "edited by humans"? Even if it satisfies the other conditions?