You are here

function hook_shs_term_data_response_alter in Simple hierarchical select 2.0.x

Same name and namespace in other branches
  1. 8 shs.api.php \hook_shs_term_data_response_alter()

Alter the response of SHS sent to the browser for all bundles and fields.

Note: this hook allows you to alter the cached data. It is called on every single request SHS makes to fetch the data!

Parameters

string $content: Json encoded string with data from ShsController::getTermData().

array $context: Associativ array containing information about the current context:

  • bundle: Name of vocabulary the data is fetched from
  • identifier: Identifier of field to fetch the data for
  • parent: Term Id of parent term (0 for first level)
  • encodingOptions: encoding options used by json_encode(). Do not change.

File

./shs.api.php, line 150
Hooks for the shs module.

Code

function hook_shs_term_data_response_alter(&$content, array $context) {
  $data = json_decode($content);

  // Prepend the term name (rendered option label) with its key (option value).
  array_walk($data, function (&$term, &$key) {
    $term->name = $key . ': ' . $term->name;
  });
  $options = isset($context['encodingOptions']) ? $context['encodingOptions'] : 0;
  $content = json_encode($data, $options);
}