0

[{"value_1":"Dragon fly"},{"value_1":"Agapostemon angelicus"},{"value_1":"Albuna oberthuri"},{"value_1":"Callohesma flavopicta"}]

This is a JSON data i need to use to import for radios in drupal 8 webforms. Iam using hook_webform_options_WEBFORM_OPTIONS_ID_alter() https://www.drupal.org/docs/8/modules/webform/webform-cookbook/how-to-use-options-from-an-external-webservice

I cant use value_1 as a key in radios, cause it is not unique, so I need to somehow copy the value into the key and get the same data on both sides, too look like this.

[{"Dragon fly":"Dragon fly"},{"Agapostemon angelicus":"Agapostemon angelicus"},{"Albuna oberthuri":"Albuna oberthuri"},{"Callohesma flavopicta":"Callohesma flavopicta"}] there are many lists and many data but the same problem.

This is the code from the module I made followed by instructions:

 * Implements hook_webform_options_WEBFORM_OPTIONS_ID_alter().
 */
function my_module_webform_options_external_countries_alter(array &$options, array &$element) {
  // Load cached options.
  if ($cache = \Drupal::cache()->get('external_countries')) {
    $options = $cache->data;
    return;
  }

  // Get data as associative array from the external webservice.
  $external_countries_url = 'https://gist.githubusercontent.com/mshafrir/2646763/raw/8b0dbb93521f5d6889502305335104218454c2bf/states_hash.json';
  $options = json_decode(file_get_contents($external_countries_url), TRUE);

  // Cache options so that we don't have to make continual requests
  // to the external webservice.
  \Drupal::cache()->set('external_countries', $options);
}

3
  • Just checking that you replaced CUSTOM_MODULE with the machine name of your module?
    – Geat
    Oct 1 at 23:27
  • That CUSTOM_MODULE is copied from drupal, but unfortunately, you got a point I said that I made followed by instructions, it's a typo. Oct 2 at 0:54
  • 1
    @DraganPetrovicFSD There's no need to edit your question to mark it as "SOLVED". Instead, you should wait a couple days and then check the green box to mark your answer as the accepted answer. Oct 2 at 5:26
1

I just found a solution. I probably didn't explain it well, so there was no answer. I hope that the solution to the problem I had will better explain the situation I was in.

function external_usa_list_webform_options_external_countries_alter(array &$options, array &$element) {
  // Load cached options.
  if ($cache = \Drupal::cache()->get('external_countries')) {
    $options = $cache->data;
    return;
  }

  // Get data as associative array from the external webservice.
  $external_countries_url = 'https://gist.githubusercontent.com/mshafrir/2646763/raw/8b0dbb93521f5d6889502305335104218454c2bf/states_hash.json';
  $options = json_decode(file_get_contents($external_countries_url), TRUE);

  $options = array_combine(array_values($options), array_values($options));

  // Cache options so that we don't have to make continual requests
  // to the external webservice.
  \Drupal::cache()->set('external_countries', $options);
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.