Link to home
Start Free TrialLog in
Avatar of itkadmin
itkadminFlag for Canada

asked on

How to properly add PHP code to blocks in Drupal 7

<?php
$inittime=time();
$dateatota=strtotime('2013-04-29 18:00:00');
$timediff = $dateatota - $inittime; $days=intval($timediff/86400);$remaining=$timediff%86400;
echo $days;?>

Open in new window

Hello.
I am trying to add the attached code to a block in D7.
It isn't working at all. I get the following error:

Parse error: syntax error, unexpected '>' in /home/username/public_html/drupal/modules/php/php.module(80) : eval()'d code on line 1

I have php filtering module enabled. I have the input type on the block set as 'PHP Code'.

This php seems to be working fine on it's own, it does not work when embedded in Drupal. I can get it to work in an IFrame in a block, but I'd sooner not do that.

Any idea on how I can fix it? I haver very little PHP knowledge.

thank you
Avatar of Hagay Mandel
Hagay Mandel
Flag of Israel image

Try removing the php closing tag.
If not, remove the opening one as well.
Avatar of itkadmin

ASKER

That didn't work.

Removing the closing tag didn't change anything, still got the error. Removing the opening tag renders the code as plain text.

the instructions for PHP text format type say:
You may post PHP code. You should include <?php ?> tags.

I've used PHP in blocks before, mostly for linking things relative to the front page and I didn't have issues. Used opening and closing tags there.
I know about the php insertion instructions. In early versions, php tags were not allowed, so this was a shot in the dark.

The only thing I can think of is to check in php.module (modules/php/php.module) file, whether line #80 is the following:
print eval('?>' . $code);

Open in new window

yes it is. I dont know what it means though
Is it processing code at all? Try something like this to see if it works:

<?php print 'test'; ?>
Is the Input Format set to PHP? I'm sure it is, but it's good to check just in case something got wacky during your debugging.

Does the user in question have permissions set for PHP (if user 1/admin then this doesn't apply)?

The other way to do this is at the theme level is to use a .tpl.php file based on block.tpl.php. That way you can just use the code in a separate file that's not part of the database itself, and it gives you a lot more flexibility...including version controllability. If you need people to be able to change the PHP code in the interface, though, this won't work. And in .tpl.php files you do not put the closing ?> tag.

Just some random thoughts off the top of my head
<?php print 'test'; ?>
works fine

user is 1/admin
When you are entering the PHP, are you using a WYSIWYG editor, or just a straight text box? Or the View Source option of a WYSIWYG editor? Could be some sort of WYSIWYG ickiness...I see some of that with CKEditor a lot.
You might be pasting in some funky invisible character or something that's breaking the code evaluation. Try copy/pasting directly from this:

<?php
$inittime=time();
$dateatota=strtotime('2013-04-29 18:00:00');
$timediff = $dateatota - $inittime; $days=intval($timediff/86400);$remaining=$timediff%86400;
echo $days;?>

Open in new window

no joy. tried that and flushed caches
ASKER CERTIFIED SOLUTION
Avatar of Aaron Feledy
Aaron Feledy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Something like this (added a little bit of Drupal code standards spacing and format):

<?php
$inittime = time();
$dateatota = strtotime('2013-04-29 18:00:00');
$timediff = $dateatota - $inittime;
$days = intval($timediff / 86400);
$remaining = $timediff % 86400;
echo $days;
?>

Open in new window

same problem.

Also double checked to make sure wywywig was disabled. thought it may be adding some invisible thing
What I do in situations like this is start with just the PHP tags, and then add one new line at a time and see where it throws an error. Also, copy your code onto a notepad document or some plain text program and copy it off of that.

Also maybe add this to report all errors in case something else is really going on. And make sure if you have added any html into this block that you all good html code there.
error_reporting(-1);
I just put this code in a D7 block on one of my websites, and I didn't get any errors at all, and it showed the number 97. So it's not your code on this page. Please ignore my last suggestion. When this has happened to me before, there is some PHP code somewhere on your site that is throwing the error and it's not necessary on the page where you are seeing it.
If this is a big complicated site, it might be faster to export the database and do a search for <?PHP in the table that holds the content of pages and blocks. If you don't know which tables to use, let me know, I have to go into one of my databases and check. I don't know offhand.
This should be working. Your site is properly processing php in a block because you've tested it with other code. I tested this code myself and it worked fine on my setup. There seems to be something about this specific code and your setup. I wonder if something screwy is going on with one of the functions you call. Try this:

<?php
$inittime =  $_SERVER['REQUEST_TIME'];
$dateatota = 1367272800;
$timediff = $dateatota - $inittime;
$days = intval($timediff / 86400);
$remaining = $timediff % 86400;
echo $days;
?>

Open in new window

My money is on a bit of PHP that has been entered incorrectly somewhere else on the site, not something screwy with functions being called.
My money is on Drupal Gremlins®
Avatar of rinfo
rinfo


As stated in the PHP documentation, PHP opening tags must not be present to assess a string. To fix the problem simply delete the opening tag of the string.
and this relates to /home/username/public_html/drupal/modules/php/php.module(80) and not the php code you have mentioned.
it looks like junipllc's "Drupal gremlins" is the correct answer.  I tried the same code on some different drupal sites, some using the same code base. It worked on some, not on others.

I backed up the DB, thed disabled/uninstalled all non essential modules except php filter. Still wasn't working.

I copied the code from notepad, notepad++, gVim, Dreamweaver and Coda thinking I was getting wierd characters someplace. Still same error.

On a whim, I removed all the line returns so it looks like:

<?php $inittime =  $_SERVER['REQUEST_TIME']; $dateatota = 1367272800; $timediff = $dateatota - $inittime; $days = intval($timediff / 86400); $remaining = $timediff % 86400; echo $days; ?>

Voila. That worked. I have no idea why.
The solution of using <?php,?> for each line works, or you can all the code on the same line with no returns. It's not the prettiest solution, but it works.
Additional Info.

seems it is somehow tied to Wysywig

If you move PHP Code to the Top of the list on the Text Formats page at admin/config/content/formats, you can then use PHP code, with line returns, and there are no issues
That makes sense... There was probably a filter set up to add <br> tags for line breaks which would cause an unexpected '<' to be in your code.