0

I want to show image insida a form I've try it with tag inside form but it's doesnt render anything heres my code

$form['y']['image_div'] = array(
            '#prefix' => '<div class="image"><img src="https://4.bp.blogspot.com/-tGbw82Gz8hA/WPR6WiOGPaI/AAAAAAAAEjk/JDPdpK3p-8wAUe2QTQbtWSdTZR_DBc-QACLcB/s640/Checkout.png" />',
            '#suffix' => '</div>',
        );

How do I render an image inside form.inc in drupal ?

2
$form['y']['image_div'] = array(
  '#prefix' => '<div class="image">',
  '#suffix' => '</div>',
  '#markup' => theme('image', array(
    'path' => 'https://4.bp.blogspot.com/-tGbw82Gz8hA/WPR6WiOGPaI/AAAAAAAAEjk/JDPdpK3p-8wAUe2QTQbtWSdTZR_DBc-QACLcB/s640/Checkout.png',
  )),
);

And in your template file:

print drupal_render($form['y']['image_div']);

Result: enter image description here

  • which template file should I put drupal_render ? tpl or template.php ? – Aslam Aug 24 '17 at 2:28
  • In your tpl file of the form. – qzmenko Aug 24 '17 at 7:13
0

In your function that creates the form, add a $form element for your image that uses theme_image() to create the '#markup':

$form['element_id'] = array( '#prefix' => '', '#suffix' => '', '#markup' => theme_image(array( 'path' => file_create_url('uri_goes_here'), 'width' => [your_image_width], 'height' =>[your_image_height], 'title' => t('Image title for when the pointer hovers over the image'), 'attributes' => array( 'class' => 'some-image-class', 'id' => 'some_image_id', ), ) ), );

Your 'path' element can be created using file_create_url().

If you would like to render your image using an image style that is defined in your site, use theme_image_style() to create your form element's #markup:

$form['element_id'] = array( '#prefix' => '', '#suffix' => '', '#markup' => theme_image_style(array( 'style_name' => 'some_style_name', 'path' => file_create_url('uri_goes_here'), 'width' => [your_image_width], 'height' =>[your_image_height], 'title' => t('Image title for when the pointer hovers over the image'), 'attributes' => array( 'class' => 'some-image-class', 'id' => 'some_image_id', ), ) ), );

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.