drupal_eval() does not allow you to access context variable. but...

I am currently working on a project which uses the module site_user_list and I wanted to use some variables in my template. The documentation of the module said I could use the variables from the $r array but I discovered that was not working. I found someone discovered the same problem and opened an issue:

That same person also contributed a patch but I was unhappy with it. I provided my own.

If you want to use drupal_eval() and allow the code inside that to access (read-only) some of your context variables, try this:

Let's assume you want to eval the PHP code found in the variable $code. You might consider doing like this:

$options = array('foo' => 'bar');
drupal_eval('<?php ' . $code . '?>');

This would mean that whatever PHP code found in $code would have no access to $options.

This is why I recommend this:

$options = array('foo' => 'bar');
drupal_eval('<?php $options = '. var_export($options, TRUE).'; ' . $code . '?>');

The var_export() function (php core) will create valid PHP code from your array. You assign that to another variable (could be called anything) and voila. This code will be executed and the values from your $options array becomes accessible (read-only) to your $code.
If the code inside $code tries to change the values of $options, it will NOT change the values of the real $options since it is a copy.



Drupal has provided

Drupal has provided extremely well features and facilities to the users which is the main reason for migration of users to Drupal platform inorder to avail all opportunities present in Drupal.I think Drupal has defeated all its competitor in a very short time with its excellent features.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • You may post PHP code. You should include <?php ?> tags.
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options