Wednesday, August 31, 2011

To Use drupal_eval()

 drupal_eval();

Evaluate a string of PHP code
This is a wrapper around PHP's eval(). It uses output buffering to capture both returned and printed text. Unlike eval(), we require code to be surrounded by <?php ?> tags; in other words, we evaluate the code as if it were a stand-alone PHP file.
Using this wrapper also ensures that the PHP code which is evaluated can not overwrite any variables in the calling code, unlike a regular eval() call.

Example for drupal_eval();
Create a content in the admin side and note that node id and give the url for that content.This is the tpl file where we are  sending the node of the content page to the module file as an argument through page_print() function.
 
<?php   $node_id = 20;
$text = page_print($node_id);    ?>
  <div style="position: absolute;">
    <table cellspacing="0" cellpadding="0" style="width: 600px;">
      <tbody>
        <tr>
          <td style="vertical-align: top; padding: 19px 0px 23px 16px;">
            &nbsp;
          </td>
          <td class="white fontsize11" style="vertical-align: top; padding: 14px 21px 0px 0px; text-align: right;">
            <a class="white noul" onclick="window.close();" href="javascript://">Close window</a>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
<?php print drupal_eval($text);?>

Here we are using that node id to get the body of the content from node table.Then we will return those value to the tpl file again.

function page_print($node_id=NULL) {
       $node_body = db_result(db_query("SELECT body FROM {node_revisions} WHERE nid = %d",$node_id));
    return $node_body;
}

Finally we will print that body value in tpl file,<?php print $text;?>
But we cant get the exact body content.i.e,We cant access the image path and theme path we given in that content body.
For that we will use 'drupal_eval' to display the complete structure of that body content.
i.e,
<?php print drupal_eval($text);?>

No comments:

Post a Comment