Thursday, September 21, 2017

Drupal 7.x Search and CSS feed bugs due to extra lines

I recently noticed that the automatic css feeds that Drupal generate and the site search tool were not working on my Drupal 7 site.  After a little digging courtesy of Google this pretty old post on the Drupal community site helped me solve both problems at once.

The search function was returning an error when I searched on any term.  While the CSS link would prompt me to save or open the file in firefox rather than render a CSS page automatically.  When checking the downloaded file you could see the first line in the file was blank followed by the expected xml tags and script befinning on line 2.

Basically both bugs appear to be caused by blank lines before the opening or after closing PHP tags ( and ?>) in the module files..  In fact as best practice developers are encouraged not to include the PHP ending tag in their module files as it is so easy to save a file with one or two blank lines at the end to the file which causes errors.

A number of solutions and quick fixes are aoutlined in the linked post.  I basically dragged all my module files into my favourite text editor and visually checked for blank lines before the opening PHP tags.  Then continued to check visually that all the module files which did have closing PHP tags "?>" did not have any trailing blank lines or spaces after the tag.  In my case I found only two modules with closing PHP tags and both had blank lines after the tags.  After deleting these the bugs were solved.

To locate the module files which need to be checked they are found in the sites/all/modules/ directory.  Within each module folder check the file with the .module suffix for blank lines as described above.