More on displaying images on D7

When we left our story last time, things were in decent shape. But then I noticed that some of my img_assist tags weren't displaying, and had to meddle with the custom img_assist filter some more. I made some progress, and I feel I might as well share.

As it turned out, the problem was that some of the images' image fields have the proper language ('en') set rather than being undefined ('und'). I had hardcoded 'und' into my module and this tripped me up. While I was in there I also implemented link=node so that any img_assist nodes intended to link back to the image node do so, since it was easy. The drupal api documentation needs more examples badly, but as documentation goes, it's all right.

Pattern: /\[img_assist\|(.+?)]/

Replacement text (PHP Code):

$elements = explode('|', $matches[1]);

$result = '(filter error or malformed img_assist tag)';

foreach ($elements as $element) {
  $parts = explode('=', $element);
  if ( $parts[1] != '' ) {
    $values[$parts[0]] = $parts[1];
  }
}

if ( $values[nid] ) {
  $mynode = node_load($values[nid]);
  if (!$mynode) {
    $result = "(cannot load image node)";
  } else {
    if ($mynode->node_image[$mynode->language][0]['uri']) {
      $path = $mynode->node_image[$mynode->language][0]['uri'];
    } else {
      $path = $mynode->node_image['und'][0]['uri'];
    }

    if ( $values[height] && $values[width] ) {
      $result = theme('image_style', array('style_name' => 'large', 'path' => $path, 'getsize' => FALSE, 'alt' => $mynode->title, 'attributes' => array('class' => 'img_assist', 'width' => $values[width], 'height' => $values[height])));
    } else {
      $result = theme('image_style', array('style_name' => 'large', 'path' => $path, 'getsize' => TRUE, 'alt' => $mynode->title, 'attributes' => array('class' => 'img_assist')));
    }
  }
  if ( $values[link] == 'node' ) {
    $result = l($result, 'node/'.$values[nid],array('html' => TRUE));
  }
}

return $result;

This code is the big fundamental change:

    if ($mynode->node_image[$mynode->language][0]['uri']) {
      $path = $mynode->node_image[$mynode->language][0]['uri'];
    } else {
      $path = $mynode->node_image['und'][0]['uri'];
    }

I copied this code into my image filter, too, to address the same issue should it crop up. As mentioned previously, implementing the node link was fairly trivial:

  if ( $values[link] == 'node' ) {
    $result = l($result, 'node/'.$values[nid],array('html' => TRUE));
  }

Clearly, if link is set to node, we wrap the result in a link. Since I broke the arguments to img_assist up into an associative array, it's easy to make decisions based on the contents. I know this is beyond obvious to people who self-identify as programmers, but this is all still nifty to me.

Add new comment

Default

  • Use [fn]...[/fn] (or <fn>...</fn>) to insert automatically numbered footnotes.
  • You may link to images on this site using a special syntax
  • Web page addresses and e-mail addresses turn into links automatically.
  • To post pieces of code, surround them with <code>...</code> tags. For PHP code, you can use <?php ... ?>, which will also colour it based on syntax.
  • Internal paths in single or double quotes, written as "internal:node/99", for example, are replaced with the appropriate absolute URL or path. Paths to files in single or double quotes, written as "files:somefile.ext", for example, are replaced with the appropriate URL that can be used to download the file.
  • Filtered words will be replaced with the filtered version of the word.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <q>

Issue

  • Lines and paragraphs break automatically.
  • To post pieces of code, surround them with <code>...</code> tags. For PHP code, you can use <?php ... ?>, which will also colour it based on syntax.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>

Drinking Game

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <p> <br> <pre> <h2> <h3> <h4>
  • Images may be embedded like: [image:node_id align=alignment hspace=n vspace=n border=n size=label width=n height=n nolink=(0|1) class=name style=style-data node=id] Leave off any attributes you don't want.
  • [img_assist|...] tags will be displayed, maybe. Please don't make more of them.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.