Score:1

Hook webform options WEBFORM OPTIONS ID alter()

cn flag

[{"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);
}

Geat avatar
de flag
Just checking that you replaced CUSTOM_MODULE with the machine name of your module?
Dragan Petrovic FSD avatar
cn flag
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.
cn flag
@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.
Score:3
cn flag

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);
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.