0

I have a module that creates a simple page. However in the page I want to link to iframes that I will generate in the module code.

Currently I have two hook_menu items: one for the simple page (no arguments), one for the iframe pages (with arguments).

However when I show the iframe, I need to get some values from a global variable that is populated from the simple page function but when I reference it in my function for the pages with arguments the variables are always null. I assume this is because drupal re-loads the .module page but skips running the simple page function when it loads the page with arguments, thus it doesn't populate the variables when it loads the iframe.

How could I populate my iframe with the variables from my simple page? Is there an easier way than what I'm doing? Can I do it without global?

Does that make sense?

function my_custom_page() {} for:
drupal/my-custom-page-module

function my_iframe($id) {} for:
drupal/my-custom-page-iframe/0

Code for drupal/my-custom-page-iframe/0 needs to get variables that were generated in function my_custom_page() {} when it runs the my_iframe($id) for the page callback.

I have defined the variables outside functions as global and included global var_name in both functions but it still doesn't work.

Also, the iframe should not have any Drupal theme/sidebar content: for this I'm just echo'ing my code and then return: within my_iframe{$id}.

What I was thinking was instead of using two functions with page callback, was to just use GET request for my 'iframes' like ?iframe={$id}. Does that sound like a better direction to go?

1 Answer 1

0

First off, Drupal is bootstrapped every time a new page is requested. Even if both the pages would be using the same menu callback, the global variables would be reset when passing from a page to the other one.

What you can do is using session variables, passing variables through the URL, or using a cache.
Passing variables through the URL would not use for complex variables, as there is a limit to the URL length accepted from browsers; using a cache would be a solution if the cached data should be available to more than one user, such as to user having the same rule; using session variables is the easier solution when the data to share are only for the currently logged-in users.

As for using the same callback, there is an example given in the hook_menu() documentation.

  function mymodule_menu() {
    $items['abc/def'] = array(
      'page callback' => 'mymodule_abc_view',
    );
    return $items;
  }

  function mymodule_abc_view($ghi = 0, $jkl = '') {
    // ...
  }

The menu callback would be used for http://example.com/abc/def, http://example.com/abc/def/222, and http://example.com/abc/def/333/aaaa. In the first case, $ghi and $jkl would get their default value; in the second case, only $ikl would get its default value; in the last case, none of the variables would get their default values.

That is true also for the form builder, with the exception that the first two arguments would be $form, and &$form_state.

2
  • Ah, ok, I am still quite new to Drupal, if I were to use cached data, how would I find out information on doing that? I assume its not persistent variables as that looks like itll be for config settings. My variables relate to array values from json_decode() and should be available to all users who can see the page so cache seems the best solution? Cheers
    – dan2k3k4
    Dec 5, 2012 at 21:51
  • The functions to get a value from the cache, and set a value in the cache are cache_get(), and cache_set(). If you are going to use a cache table that is not used from Drupal, check how block_schema() creates a database for the cache with drupal_get_schema_unprocessed().
    – apaderno
    Dec 5, 2012 at 22:28

Your Answer

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

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