Hi, I recently needed to insert an image from an image field into the node's body prior to rendering. I was able to do this using this code: function THEME_preprocess_field(&$vars) { if ($vars['element']['#object']->type == 'case_study' && $vars['element']['#field_name'] == 'body') { $node = node_load($vars['element']['#object']->nid); $img = field_get_items('node', $node, 'field_cs_image'); $img = field_view_value('node', $node, 'field_cs_image', $img[0], array( 'type' => 'image', 'settings' => array( 'image_style' => 'list_image_large', ) )); $img = render($img); $vars['items'][0]['#markup'] = str_replace('alt=""', 'style="float:right;margin:0 0 20px 20px" alt="' . $node->title . '"', $img) . $vars['items'][0]['#markup']; } } I don't like having to use the str_replace() function to specify attributes for the image but my efforts to use theme('image', ...) and theme('image_style', ...) were unsuccessful because they don't include width and height attributes. I saw the big thread with Dave Reid on the performance issue with getsize(). I understand the reasoning for removing the function but how is it that rendering the image with field_view_value() produces an image tag with width and height attributes? Couldn't the theme functions use the same method? Is there a better or more proper way to render an image with width and height attributes AND specify other attributes such as alt and style? Thanks, Cameron
Cameron You can enable the alt attribute for the image fields and that should handle that. You should probably use css to style the image and not attach a style attribute to the img tag Nevets On 5/23/2012 12:41 PM, Cameron B. Prince wrote:
Hi,
I recently needed to insert an image from an image field into the node's body prior to rendering. I was able to do this using this code:
function THEME_preprocess_field(&$vars) { if ($vars['element']['#object']->type == 'case_study'&& $vars['element']['#field_name'] == 'body') { $node = node_load($vars['element']['#object']->nid); $img = field_get_items('node', $node, 'field_cs_image'); $img = field_view_value('node', $node, 'field_cs_image', $img[0], array( 'type' => 'image', 'settings' => array( 'image_style' => 'list_image_large', ) )); $img = render($img); $vars['items'][0]['#markup'] = str_replace('alt=""', 'style="float:right;margin:0 0 20px 20px" alt="' . $node->title . '"', $img) . $vars['items'][0]['#markup']; } }
I don't like having to use the str_replace() function to specify attributes for the image but my efforts to use theme('image', ...) and theme('image_style', ...) were unsuccessful because they don't include width and height attributes.
I saw the big thread with Dave Reid on the performance issue with getsize(). I understand the reasoning for removing the function but how is it that rendering the image with field_view_value() produces an image tag with width and height attributes? Couldn't the theme functions use the same method?
Is there a better or more proper way to render an image with width and height attributes AND specify other attributes such as alt and style?
Thanks, Cameron
Thanks for your reply, but it is not really a complete or suitable answer. If you look at the code, the alt tag is set to the node's title. I don't want to require the client to enter this data again for every image he uploads using a separate alt field. I'm well aware of using CSS but unless I want to affect every image within the body, I'll need a class for the prepended image, which I also am unable to do with field_view_value(); Cameron On May 23, 2012, at 1:11 PM, Steve Ringwood wrote:
Cameron
You can enable the alt attribute for the image fields and that should handle that. You should probably use css to style the image and not attach a style attribute to the img tag
Nevets
On 5/23/2012 12:41 PM, Cameron B. Prince wrote:
Hi,
I recently needed to insert an image from an image field into the node's body prior to rendering. I was able to do this using this code:
function THEME_preprocess_field(&$vars) { if ($vars['element']['#object']->type == 'case_study'&& $vars['element']['#field_name'] == 'body') { $node = node_load($vars['element']['#object']->nid); $img = field_get_items('node', $node, 'field_cs_image'); $img = field_view_value('node', $node, 'field_cs_image', $img[0], array( 'type' => 'image', 'settings' => array( 'image_style' => 'list_image_large', ) )); $img = render($img); $vars['items'][0]['#markup'] = str_replace('alt=""', 'style="float:right;margin:0 0 20px 20px" alt="' . $node->title . '"', $img) . $vars['items'][0]['#markup']; } }
I don't like having to use the str_replace() function to specify attributes for the image but my efforts to use theme('image', ...) and theme('image_style', ...) were unsuccessful because they don't include width and height attributes.
I saw the big thread with Dave Reid on the performance issue with getsize(). I understand the reasoning for removing the function but how is it that rendering the image with field_view_value() produces an image tag with width and height attributes? Couldn't the theme functions use the same method?
Is there a better or more proper way to render an image with width and height attributes AND specify other attributes such as alt and style?
Thanks, Cameron
participants (2)
-
Cameron B. Prince -
Steve Ringwood