Skip to content


Recovering from broken php code in a “PHP Code” block in Drupal 7

I love to live a little dangerously, and we allow our research group webmasters to put custom php code in their Drupal sites. This lets them do nice things like have blocks with “latest 3 publications” or “next seminar”. I keep meaning to write standard code for these but there’s millions of things on the TODO list as usual.

Anyhow, the webmaster for a research group put a typo in the PHP code for a block that displayed on every page on his site. This meant he couldn’t get into the site at all as no pages could render (we just saw a white page with a scrap of text generated before the php error)

I couldn’t find a blog post about how to recover from the situation so I figured I should write one.

Solution:

Go into the MySQL database for the site, and issue the following command:

mysql> update block_custom set format='full_html' where format='php_code' ;

Admittedly it’s a bit of a blunt instrument and stops php working in all blocks, to be a bit more subtle you could do.

mysql> select bid,info,format from block_custom ;
+-----+-------------------+---------------+
| bid | info              | format        |
+-----+-------------------+---------------+
|   2 | Student Quotation | full_html     |
|   3 | Footer Links      | full_html     |
|   4 | Latest News       | php_code      |
|   5 | Seminars          | php_code      |
|   6 | Demo HVLab Link   | filtered_html |
|   7 | LeftBar           | filtered_html |
|   8 | Lower Left        | php_code      |
|   9 | Featured News     | php_code      |
|  10 | Featured Project  | php_code      |
|  11 | t                 | filtered_html |
+-----+-------------------+---------------+
10 rows in set (0.00 sec)
mysql> update block_custom set format='full_html' where bid=6;

Where bid is the number of the block you want to reset.

Prevention:

To avoid getting into this situation is quite easy. It only happens if the broken block is on all pages, so make a page for testing new blocks and limit the PHP block to only show on that page while you’re working on it. If that page breaks you will still be able to go through the homepage to get to the admin interface.

Posted in Drupal, Tips.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.