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.
- [Other better explanations]
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.
- Get a value:
$val = Drupal::state()->get('key');
- Get multiple key/value pairs:
$pairs = Drupal::state()->getMultiple($keys);
- Get all key/value pairs:
$collection = Drupal::state()->getAll();
- Set a value:
- Set multiple values:
- Set a value if not already set:
- Delete a value: