1

Using drupal 7, Views 3, I have a function (present on Leaflet Module) which takes only 2 parameters. I use this hook function to print resulting points from view in different colors

//Allow modules to alter the points data while rendering a leaflet views row.
MY_MODULE_leaflet_views_alter_points_data_alter ($result, &$points) 
{
  if (isset($result->number)) 
 {
  // Add number value to every points data entry, if present.
    array_walk($points, function(&$point, $key, $number) {
     $point['number'] = $number;
   }, $result->number);
 }
}

I am trying to use this hook to take variables from outside the function. For example:

$data='heeeello';

MY_MODULE_leaflet_views_alter_points_data_alter ($result, &$points) 
 {
  echo $data;
 }

Just returns NULL. Tried setting it up as Global (which should already be, but just in case), also variable_set($data,'hello')... no success.

Executing the function passing the params works neither:

MY_MODULE_leaflet_views_alter_points_data_alter ($result, &$points,$param)
{
  echo $param;
}

thanks in advance

EDIT

I want to populate an external array based on the data processed by my hook (see below MY_MODULE_leaflet_views_alter_points_data_alter)

This final array has to contain unique values. On standard PHP I would access the external array and populate recursively it but I have problems accessing this array via my hook.

Using global doesn't work, but it works using variable_set and variable_get, but it means that each time the hook access the variable (variable_get(param)), the array starts empty again.

$array = array();

variable_set('my_array', $array);

//testing global, doesnt work
$test=array();

MY_MODULE_leaflet_views_alter_points_data_alter ($result, &$points) {

    global $test;
    var_dump($test); //NULL

    $my_array= variable_get('my_array');
    //I will get allways an empty array... even if its later correctly populated

    //fetching results from the hook
    $name=$result->name;

    if (!in_array($name,$my_array))
      {
        array_push($my_array,$name);
      }

    //plots just a value (array is not recursively populated, as allways start empty inside the hook)  
    var_dump($my_array);
 }  

closed as off-topic by Clive Mar 30 '15 at 13:59

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on programming, PHP, SQL, etc. that do not relate directly to Drupal are off-topic here, but can be asked on Stack Overflow." – Clive
If this question can be reworded to fit the rules in the help center, please edit the question.

  • ¿How about put the code that gets $data value inside MY_MODULE_leaflet_views_alter_points_data_alter ()? Usually you call another function that gets the data. And keep in mind that you shouldn't output data directly (echo, print) but modify $results or whateever you need to fit your needs. – sanzante Mar 30 '15 at 13:32
  • As this is just a basic PHP issue it's off-topic here – Clive Mar 30 '15 at 13:59
  • I don't think its just a PHP issue, never had these issues before with 'standard' PHP – user1249791 Mar 30 '15 at 14:04
  • your misuse of global variables proof you wrong, but see my answer, I also provided you an example vor Drupals variable_set and variable_get. – Andre Baumeier Mar 30 '15 at 14:17
  • Drupal can't change the language - global variables are global variables in any php application – Clive Mar 30 '15 at 16:41
1

This is a php issue.

$data = 'heeeello';

MY_MODULE_leaflet_views_alter_points_data_alter ($result, &$points) {
  echo $data; // $data is NULL, will also throw a notice
}

MY_MODULE_leaflet_views_alter_points_data_alter ($result, &$points) {
  global $data;
  echo $data; // $data is 'heeeello'
}

regarding your variable_set, the params are different:

$data = 'heeeello';
variable_set('hello', $data);
MY_MODULE_leaflet_views_alter_points_data_alter ($result, &$points) {
  $data = variable_get('hello');
  echo $data; // $data is 'heeeello'
}

RE edit: this goes towards your extension: variable_set() is not designed to do sets on every request, variables should be quiet consistent, every variable_set will clear your cache (to some extend), abusing this will lead to a lot of performance problems.

If you need a cache use cache_set and cache_get, for singletons/ static stuff use drupal_static. The global variable $test you are using right now in your question, should not return NULL but array(), everything else looks like a different problem.

If you want to build up a storage backend create proper tables using hook_schema or (advanced) build your own entities.

edit: almost forgot, your code from above, is not working because you are not saving that variable again, so even for the second run it will always be empty.

  • Thanks Andre, in fact, variable_set works properly, but not global. This later option would be ideal, but really does not work in that context. The question was quite simplified for the sake of comprehension, now I see I can access to this external variable via variable_set and variable_get. What I would easily achieve via PHP , I have problems now because of Drupal way to work (I guess...), so I dare to modify my question...see above. Thanks again. – user1249791 Mar 30 '15 at 15:22
  • there you go... – Andre Baumeier Mar 30 '15 at 16:30
  • Thanks a lot Andre, didn't get global working (which would be the simplest way) but at least I can continue working using drupal_static (or variable_set/get but, as you suggested, better to use drupal_static). Answering this kind of basic questions is fundamental for Drupal dev beginners, sometimes the easiest things become huge waste of time for us... really appreaciate your effort. Got more details on how to use drupal_static in drupal.stackexchange.com/questions/85968/… – user1249791 Mar 31 '15 at 7:08

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