You are here

function key_save_key in Key 7.3

Save a key configuration.

Parameters

array $fields: The fields of the configuration to save.

array $original_config: The original configuration, if there was one.

bool $messages: TRUE if messages should be displayed.

Return value

array|null The saved configuration or NULL if the save was unsuccessful.

3 calls to key_save_key()
key_config_features_rebuild in includes/key_config.features.inc
Implements hook_features_rebuild().
key_config_form_submit in includes/key.admin.inc
Form submission handler for key_config_form().
_drush_key_save in drush/key_save.inc
Save a key.

File

./key.module, line 539
Main Key functionality and hook implementations.

Code

function key_save_key($fields, $original_config = array(), $messages = TRUE) {

  // Serialize any field that is an array.
  foreach ($fields as $index => $field) {
    if (is_array($field)) {
      $fields[$index] = serialize($field);
    }
  }

  // Save the configuration.
  $merge_status = db_merge('key_config')
    ->key(array(
    'id' => $fields['id'],
  ))
    ->fields($fields)
    ->execute();

  // Load the configuration to make sure it was saved.
  $key_config = key_get_key($fields['id'], TRUE);
  if (empty($key_config)) {
    $key_config = NULL;
  }

  // If the save was not successful, display an error and bail.
  if (!$key_config) {
    if ($messages) {
      $t_args = array(
        '%label' => $fields['label'],
      );
      drupal_set_message(t('The key %label could not be saved.', $t_args), 'error');
    }
    return FALSE;
  }

  // Display success message and log to watchdog.
  if ($messages) {
    $t_args = array(
      '%label' => $fields['label'],
    );
    switch ($merge_status) {
      case MergeQuery::STATUS_INSERT:
        drupal_set_message(t('The key %label has been added.', $t_args));
        watchdog('key', 'Added key %label.', $t_args, WATCHDOG_NOTICE, l(t('view'), KEY_MENU_PATH . '/list'));
        break;
      case MergeQuery::STATUS_UPDATE:
        drupal_set_message(t('The key %label has been updated.', $t_args));
        watchdog('key', 'Updated key %label.', $t_args, WATCHDOG_NOTICE, l(t('view'), KEY_MENU_PATH . '/list'));
        break;
    }
  }

  // If an original key configuration exists.
  if (!empty($original_config)) {
    $original_key_provider = key_get_plugin('key_provider', $original_config['key_provider']);

    // If the original key's provider allows setting a key value and
    // the plugin ID is different from the one that was just saved.
    if ($original_key_provider['key value']['accepted'] && $original_config['key_provider'] != $key_config['key_provider']) {

      // Allow the original key's provider to delete the key value.
      if ($delete_callback = ctools_plugin_get_function($original_key_provider, 'delete key value')) {
        call_user_func($delete_callback, $original_config);
      }
    }
  }

  // Return the saved configuration.
  return $key_config;
}