3

When I create a block through views I can easily link it to the content page by setting link image to in the field's settings. But I also have some blocks that are created programmatically and I would like to similarly add links to these images. How would I do this?

EDIT: Here is the code I use to create the block:

  $block = array();
  $rows = array();
  $format = 'l, F j, Y  - G:i';

  $header = array('Project Name', 'Screenshot' , 'Submitted');
  foreach ($results['node'] as $nid_array) {
    $row = $nid_array->nid;
    $node = node_load($row);
    $path = $node->field_icon_thumbnail['und'][0]['uri'];
      $variables = array(
    'path' =>  image_style_url('icon_thumbnail',$path),
    'alt' => 'Thumbnail',
    'title' => 'Thumbnail',
    'attributes' => array('class' => 'some-img', 'id' => 'my-img'),
    );
      $image = theme('image', $variables);

      $rows[] = array(
    l($node->title, "node/$node->nid"),
    $image,
    date($format, $node->changed)
      );
  }
  $rows = array_reverse($rows);
  $table = array('rows' => $rows, 'header' => $header, 'empty' => 'No terms found');
  $block['content'] = array(
  '#theme' => 'table',
  '#header' => $header,
  '#rows' => $rows,
  '#empty' => t('No terms found')
  );
  return $block;
  • I think you if you make it programmatically you should have nid that related to image so you can link image to node/nid <a href="node/nid"><img/></a> – Ahmed Jun 27 '12 at 16:25
  • Hey Ahmed, I'm not quite sure what I would do with that html snipet. I have added the code I use to create the block. Do I need to create my block differently? – Mike2012 Jun 27 '12 at 16:36
7

You can use the l() function to wrap the image in a link. As it's HTML you'll need to pass the html option to the function so that it isn't run through check_plain():

$image = theme('image', $variables);

$linked_image = l($image, "node/$node->nid", array('html' => TRUE));

$rows[] = array(
  l($node->title, "node/$node->nid"),
  $linked_image,
  date($format, $node->changed)
);
| improve this answer | |
  • Missed that html => TRUE, thank you very much. That's what I looked for. – kwoxer Oct 16 '17 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.