0

I am passing few parameters via ajax to custom module (fgs_api.module) and in returns getting JSON data ($results->data). I want to use/get that variable ($results->data) in node--xyz.tpl.php content template. How can I use that variable in node--xyz.tpl.php. I am doing in drupal 7.

In module fgs_api.module

function fgs_api_data_execute($parameter1, $parameter2)
{   
    if(!is_null($parameter1) && !is_null($parameter2))
    {
        // my custom code which is working
         $results = $api->{'getSpecialOfferHomes'}(intval($parameter1), intval($parameter2)); 

        if($results->success)
        {
            $return = fgs_api_format_return(false, $results->data);
        }else{
            $return = fgs_api_format_return(true, $results->message);
        }
    }else{
        $return = fgs_api_format_return(true, 'Incorrect parameters');
    }
    return $return;
}

In Js

  var url = '/fgs_api_data_execute/' + parameter1 + '/' + parameter2;    
                console.log(url);

                $.ajax({
                    url: url,
                    type: 'POST',
                    dataType: 'json',
                    success: function (result) {   
                        if (result.data) {
                             console.log(result['data']);  // use this data in Module           
                        }else {
                           console.log("error");
                        }
                    },

How can I use/pass return data variable from custom module to node--xyz.tpl.php?

  • Maybe dumb question but as I seem to not understand what's going on I'll ask anyway: You have a function in your theme and want to execute it in your module? What's happening when you just call the function in the module? And what's that Ajax about? Can you please edit your question to add clarification or a step by step to reproduce this issue? Many thanks 🙏🏻 – leymannx Mar 14 at 9:50
  • @leymannx I have updated dumb question. AJAX call is for passing few parameters while click. I may not following drupal best practices. I have custom module. I am getting data as results but can't access data in node template. – Nisarg Patel Apr 29 at 13:27
  • 1
    You are trying to use clientside data (from javascript) in your serverside template (Twig / PHP), so you clearly do not understand what you are doing. I'd say this question does not belong here and you should do some tutorials about how PHP and AJAX work. – vrijdenker May 1 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.