Advertising sustains the DA. Ads are hidden for members. Join today

State API overview

Last updated on
11 April 2024

This documentation needs review. See "Help improve this page" in the sidebar.

Note: The State API is different from the FormAPI #states attribute. State API values are stored in the 'key_value" table.

Video: Complete Documentation of State API

The State API is a simple system for the storage of information about the system's state. The information is stored in the database and will be lost when the database is dropped or the site is re-installed from configuration. For storing data which needs to be edited by humans or needs to be shared between environments use the Configuration API. In Drupal 7 and earlier the variables system was used to store state information.

State information is stored in the database and has the following characteristics:

  • It is not meant to be exported.
  • It is specific to an individual environment.
  • It is not to be deployed to another environment.
  • All state information is lost when you reset the database.

Examples of state information are CSRF tokens and tracking the time something occurred, such as the last time cron was run ('system.cron_last'). All these are information that is specific to an environment and has no use in deployment.

Here is a list of common keys which might exist on a site:

$common_keys = [
  'comment.maintain_entity_statistics',
  'comment.node_comment_statistics_scale',
  'drupal_css_cache_files',
  'install_task',
  'install_time',
  'node.min_max_update_time',
  'node.type.locked',
  'router.path_roots',
  'routing.menu_masks.router',
  'routing.non_admin_routes',
  'system.cron_key',
  'system.cron_last',
  'system.css_js_query_string',
  'system.js_cache_files',
  'system.maintenance_mode',
  'system.module.files',
  'system.private_key',
  'system.theme.data',
  'system.theme.files',
  'system.theme_engine.files',
  'twig_extension_hash_prefix',
  'views.view_route_names',
];

Reading State

State is read using the get() method. To read a state value, just provide the key.

  • A single value.
    $val = \Drupal::state()->get('key');
  • Multiple key/value pairs.
    $pairs = \Drupal::state()->getMultiple($keys);

Writing State

State is written using the set() method. To write a state value, just provide the key and the value.

  • A single value.
    \Drupal::state()->set('key','value');
  • Multiple values.
    \Drupal::state()->setMultiple($keyvalues);

Removing State

A state is deleted using the delete() method. To delete a state value, just provide the key.

  • Delete a value.
    \Drupal::state()->delete('key');
  • Delete multiple values.
    \Drupal::state()->deleteMultiple($keyvalues);

Access state values with Drush

To get a list of state values from your site with Drush, run this command:

$ drush sql:query "SELECT name FROM key_value WHERE collection='state'";
comment.maintain_entity_statistics
comment.node_comment_statistics_scale
install_task
install_time
node.min_max_update_time
router.path_roots
[...]

You can get, set, or delete a state value:

$ drush state:get system.cron_key
lWQIvbTbJfYadOHh5Et5-OkgkhX4VTuiN9b5RMt_oPnFEIpcwTyTrWerlOthy2GnW1CJiQcilQ

For more, see:

References

Documentation for class \Drupal\Core\State\State, Drupal's default StateInterface implementation.

State API documentation.

Tags

Help improve this page

Page status: Needs review

You can: