Thursday, November 7, 2013

A simple blog page for Drupal 7

'Don't use the blog module! It turns your whole website into a blogging site!' Or: 'you're nuts if you don't use views' (then install twenty modules and spend the next six months learning how to use them!)

The Web is a great place for advice, isn't it? All I wanted was pretty simple: just a page with news items submitted by various users, a read more link and the capacity to comment on them. I doubted that the designers of Drupal hadn't already cracked this one, but you wouldn't know that from looking at some of the solutions posted on the Web. Though, admittedly, the designers of the blog module could have added a configure button ...

For those of you with similar ambitions, I suggest you at least try half a dozen mouse-clicks first:

  1. Enable the blog module. This turns on a blog entry type of content, which is exactly what you want.
  2. Go to Admin->Structure->Blog Entry->edit and deselect the 'promote to front page' option. Save.
  3. Go to http://<your-host>/?q=blog/ to add your first blog entry. Add this link to your front page and now you have a page of news items.

That's all there is to it. No rocket-science, no phpmyadmin hacking of the database, no fancy modules and courses in how to use Drupal views, no php. Sometimes I wonder why it is that, when people are faced by a problem they have no clue how to solve, that they start a competition to find the most complex answer to the simplest problem.

Tuesday, November 5, 2013

Using setenv.sh on tomcat6

If you want to set java runtime options for tomcat, a Java application, you can't do that in your web application, because by the time it gets called, the JVM has already been created and configured by Tomcat. So the Tomcat way around this is to let the user specify a shell script to be run from catalina.sh, which will be called to set CATALINA_OPTS, which will be passed to java. One of the gotchas associated with this method, however, is that catalina.sh, which is the script used by the OS on Linux to launch Tomcat uses the dash (/bin/sh) not the bash shell. If you, like foolish me, wrote your script in bash then it will not run. If you worse still invoke bash from within dash using a shebang #!/usr/bin/bash line the contents of your script will be ignored. So you have to write it in dash. I had to learn this the hard way, by tracing back uninformative error messages, and I don't want to forget it. So that is why I am recording it here.

Sunday, November 3, 2013

Native height, width of an image in jQuery

jQuery doesn't seem to provide a function for this, I guess because .nativeWidth is not consistently implemented across browsers. The reason I want to know the native width, that is, unscaled width of an image, is so that I can scale an imagemap that overlays it. Without knowing the precise ratio of the current scaling, the imagemap areas will be offset by the difference between the original area coordinates and the current positions and size of the image.

So my solution, which may not work for everyone, was to write the real image dimensions into the HTML in the first place. I did this via getimagesize in php. Then, by attaching an event handler in jQuery when the image first loads you can record the native width and height using custom attributes:

$( 'img' ).load(function() {
        $this.attr('nativew',$this.attr('width'));
        $this.attr('nativeh',$this.attr('height'));
    });
...// now retrieve them later:
w = $that.attr('nativew'),
h = $that.attr('nativeh');

Now I absolutely haven't tested this on more than Firefox and Chrome, but my hunch is that it will work on everything, because at bottom it relies on jQuery. The only problem for some will be writing the true image dimensions to the HTML. But it may not work with all image types. I used .png.