0

I've an API service callback function which ties up with its own module template to give an HTML output. As per my understanding, default content type of API output is application/json, so I had to override it manually to text/html.

However, I'm still getting a null output always no matter whatever I try. How can I suppress this unwanted null output?


custom_module.module

function api_callback_function($a) {
  if (!headers_sent()) {
    drupal_add_http_header('Content-Type', 'text/html');
  }
  print theme('custom_template_name_alias', array(
    'b' => $a
  ));
  return;
}

function custom_module_theme() {
  $themes = array(
    'custom_template_name_alias' => array(
      'template' => 'something-only', // name of template file, sans file extension
      'variables' => array(
        'b' => NULL
      ),
    )
  );
  return $themes;
}

something-only.tpl.php

<?php
$c = $variables['b'];
$path = drupal_get_path('module', 'custom_module');
global $base_url;
?>
<!doctype html><html class="no-js" lang="en">
  <head>
    <!-- HEAD related HTML code comes here -->
  </head>
  <body>
    <div>Current value of variable 'b' = <?php echo $c; ?></div>
  </body>
</html>

Output

Output

halfer
  • 19,824
  • 17
  • 99
  • 186
Knowledge Craving
  • 7,955
  • 13
  • 49
  • 92
  • How many times `api_callback_function` is calling? I suppose, it prints your template twice. – A.Mikhailov Nov 28 '16 at 20:20
  • @A.Mikhailov - nope, it's called only once, when I'm initiating the API call. Had I returned the `theme` instead of printing it in `api_callback_function`, then it would have called twice. – Knowledge Craving Nov 28 '16 at 20:22
  • 1) Is anything changed if you replace `return` by `die()`? 2) Try to dump $a value directly in api callback. – A.Mikhailov Nov 28 '16 at 20:32
  • @A.Mikhailov - Well, well, seems like you nailed it, buddy, Thank you! 1. Yes, it works just like the way I would've wanted. 2. I wouldn't do that since I'm anyways using the `$a` value in the theme via the template variable `b`. Can you please post your answer right away? I want to mark you who gave the correct answer. Also just a thought - is it possible to end the function definition in a proper way instead of just putting a `die()` or `exit`? – Knowledge Craving Nov 28 '16 at 20:41
  • If I want to print result for AJAX requests I using `die()` instead of `drupal_exit()`. But idk is it proper way to Your case) – A.Mikhailov Nov 28 '16 at 20:47

1 Answers1

1

Try to replace return to die() in api_callback_function

A.Mikhailov
  • 466
  • 4
  • 9