Drupal - How to make an array accessible around the world?

I use this code in a view field template (in this case view-view-field - all-members - uid.tpl.php):

<?php $users_friends = flag_friend_get_friends($user->uid); $users_friends_ids = array(); foreach ($users_friends as $id => $value) { $users_friends_ids[] = $id; } ?> 

It basically gets the user IDs and puts them in an array, so I can check if the field matches any of the user IDs.

So, my problem is that I do not want to have this in this template (for several reasons), but if I do not, I can not access the array. How can I make this array globally accessible?

+6
source share
2 answers

Unaware of my โ€œseveral reasons,โ€ I cannot say if this is the answer. My own reasons probably are that I do not want the same code to execute a bunch of times, and I would prefer not to have the exact exact code in several places.

Then I would create a function with a static variable to store an array of friends.

 function mymodule_get_friends_ids() { // pull in the current global user variable global $user; // call up the static variable static $users_friends_ids; // return if this static var has already been set if (is_array($users_friends_ids)) { return $users_friends_ids; } // if we hit here, then this function has not been // run yet for this page load. // init array $users_friends_ids = array(); // if user is anon, no need to go on if (user_is_anonymous()) { return $users_friends_ids; } // get friends array $users_friends = flag_friend_get_friends($user->uid); // build ids array foreach ($users_friends as $id => $value) { $users_friends_ids[] = $id; } return $users_friends_ids; } 

Now in your templates you can call mymodule_get_friends_ids () as many times as you want, and the working code below the first return will be executed only on the first call.

+5
source

Coder1 advice is very good - it does not allow you to fill the namespace of global variables with a lot of unwanted messages. This is probably the most "elegant". It may not be the easiest to use if you are fairly new to PHP (which, I suppose, might happen if you find it hard to find your head around returned arrays, but that's fine).

However, if this is really a priority, you probably do not need to have one additional global variable.

I suppose I can state the obvious here, but you can do it at almost any moment of execution (if you need the necessary information, for example, the user variable $ was filled), follow these steps:

 $GLOBALS['users_friends_ids'] = /* your code goes here */ 

Then in your template you will get access to this ...

 $friendsArray = $GLOBALS['users_friends_ids']; 

Or you can just use the construct

 global $user_friends_ids; 

when you want to initialize a variable or access it inside a function or class (which is the case for your template files - they are called inside functions, so you need to globalize or use the $GLOBALS array, which "automatically" all the variables that operate in the global namespace )

The most โ€œlogicalโ€ place for this would be inside the module using one of the many hooks available to execute this code only once. hook_init() can do this for you if the user object is already loaded at this point (not sure if you will have to test it). But you may not want to compute Drupal modules (this is not so difficult).

If you do this inside a template (and although this is not a good practice, many Drupal site owners with an initial knowledge of PHP put everything into templates), you need to know what template code is executed when. Node template code is usually executed before the page template code, which is logical, because otherwise the variables for Node content in the page template will not be filled.

If you have lists of nodes, they will call this code several times, so in the end you will do something similar to what Coder1 describes. If you do not want to create your own small module, you can place a function declaration that he wrote in the theme theme.php file, since it was called only once. You do not want to place function declarations in tpl.php files, as they are sometimes called more than once (and you are not allowed to declare functions more than once).

If you find it difficult to understand the function and return, you can always do something similar in your code (which is very, very inelegant), but it is better to have an inelegant code that you understand than an elegant code that you don't).

 if(!isset($GLOBALS['users_friends_ids'])) { $GLOBALS['users_friends_ids'] = /* your code here */ } 
+2
source

Source: https://habr.com/ru/post/886107/


All Articles