5 * Provides State commands.
9 * Implementation of hook_drush_help().
11 function state_drush_help($section) {
13 case 'meta:state:title':
14 return dt('State commands');
15 case 'meta:state:summary':
16 return dt('Interact with the State system.');
21 * Implementation of hook_drush_command().
23 function state_drush_command() {
24 $items['state-get'] = array(
25 'description' => 'Display a state value.',
27 'key' => 'The key name.',
29 'required-arguments' => 1,
31 'drush state-get system.cron_last' => 'Displays last cron run timestamp',
33 'outputformat' => array(
35 'pipe-format' => 'json',
37 'aliases' => array('sget'),
38 'core' => array('8+'),
41 $items['state-set'] = array(
42 'description' => 'Set a state value.',
44 'key' => 'The state key, for example "system.cron_last".',
45 'value' => 'The value to assign to the state key. Use \'-\' to read from STDIN.',
47 'required arguments' => 2,
50 'description' => 'Deprecated. See input-format option.',
51 'example-value' => 'boolean',
52 'value' => 'required',
54 'input-format' => array(
55 'description' => 'Type for the value. Use "auto" to detect format from value. Other recognized values are string, integer float, or boolean for corresponding primitive type, or json, yaml for complex types.',
56 'example-value' => 'boolean',
57 'value' => 'required',
59 // A convenient way to pass a multiline value within a backend request.
61 'description' => 'The value to assign to the state key (if any).',
66 'drush state-set system.cron_last 1406682882 --format=integer' => 'Sets a timestamp for last cron run.',
67 'php -r "print json_encode(array(\'drupal\', \'simpletest\'));" | drush state-set --format=json foo.name -'=> 'Set a key to a complex value (e.g. array)',
69 'aliases' => array('sset'),
70 'core' => array('8+'),
73 $items['state-delete'] = array(
74 'description' => 'Delete a state value.',
76 'key' => 'The state key, for example "system.cron_last".',
78 'required arguments' => 1,
80 'drush state-del system.cron_last' => 'Delete state entry for system.cron_last.',
82 'aliases' => array('sdel'),
83 'core' => array('8+'),
90 * State get command callback.
95 function drush_state_get($key = NULL) {
96 return \Drupal::state()->get($key);
100 * State set command callback.
105 * The data to save to state.
107 function drush_state_set($key = NULL, $value = NULL) {
108 // This hidden option is a convenient way to pass a value without passing a key.
109 $value = drush_get_option('value', $value);
111 if (!isset($value)) {
112 return drush_set_error('DRUSH_STATE_ERROR', dt('No state value specified.'));
115 // Special flag indicating that the value has been passed via STDIN.
116 if ($value === '-') {
117 $value = stream_get_contents(STDIN);
120 // If the value is a string (usual case, unless we are called from code),
121 // then format the input.
122 if (is_string($value)) {
123 $value = drush_value_format($value, drush_get_option('format', 'auto'));
126 \Drupal::state()->set($key, $value);
130 * State delete command callback.
135 function drush_state_delete($key = NULL) {
136 \Drupal::state()->delete($key);