1
votes

I have a view in Drupal 7 that renders a nodequeue. Now I want each node to be rendered with a title, text summary and an image if an image is available inside that nodes body field.

I'm thinking of creating a module that uses regular expression like:

/<img.+src="([^"]+)/i

...to find the fist image in the body field for that node and present it as a view field and call that field something like "Content: First image".

I would also like to have the image style options ect. as available for the already existing image fields.

How should I create a module like that, which hooks should I use to make that field available for views?

1
I'm not sure how to go about creating the module, but here's another approach that might work: Add 2 instances of your node's body field into the view, then edit the second instance's template file and use your regex to present an image instead of the content in that template file. - PJ McCormick

1 Answers

0
votes

I would do this using field templates. Create a body field and then apply a template on it. Something like this :

<?php
preg_match_all("/<img[^']*?src=\"([^']*?)\"[^']*?>/", $output, $image);

if(isset($image[1][0]) && trim($image[1][0])!='') {
  echo theme('image_style', array('style_name' => 'Large', 'path' => $image[1][0]));
}
?>

You can also probably achieve the same result using a display suite code field.