1

I have to create a module that return a list of my language, only the list without template,and I have to call this url via curl

I create something like this ( inside my module )

function my_module_menu() {
  $items['my_module'] = array( //URL
    'page callback' => 'get_all_languages',
    'access arguments' => array(''),
  );
  return $items;
}

/**
 * @return string
 */
function get_all_languages() {
    $out = array();
    $result = language_list('enabled');
    foreach ($result as $key => $value) {
      foreach ($value as $k => $v) {
        $out[$v->language] = $v->name;
      }
    }

    $p = json_encode($out);
    print $p;
    drupal_exit();
}

I tried also

  drupal_add_http_header('Content-Type', 'application/json');
  $p = drupal_json_encode($out);

But this url is not Json infact if call this url by Curl, I can see all the template. I want to have the result like using this module, or can i Create a view that use my module result?

I have another problem when use curl, my content is

You are not authorized to access this page.

1 Answer 1

2

Pretty close; here's the correct way:

/**
 * Implements hook_menu().
 */
function YOURMODULE_menu() {
  $items['languages'] = array(
    'page callback' => 'YOURMODULE_languages_enabled_json',
    'access callback' => TRUE,
  );
  return $items;
}

/**
 * Render enabled languages in JSON.
 */
function YOURMODULE_languages_enabled_json() {
  $out = array();
  foreach (language_list('enabled') as $languages) {
    foreach ($languages as $language) {
      $out[$language->language] = $language->name;
    }
  }
  drupal_json_output($out);
}

The important piece is https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_json_output/7 which should be used for JavaScript callback functions returning data in JSON format, and it sets the header for the JavaScript output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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