Search code examples
drupal-forms

Drupal 7: insert a photo in a drupal form


Is it possible in drupal7 to insert a photo in fieldset form? I have a start interface that contains two buttons leading to two athors interfaces and I would like to insert a photo above the two buttons

function my_module_start_form($form, &$form_state) {
  $form['start']['image'] = array(
    '#type' => 'fieldset',
    '#title' => t('image'),
   // is it possible some how to insert a photo in this form?
    );

    $form['start']['next'] = array(
    '#type' => 'submit',
    '#value' => t('Create charts')
  );
  $form['start']['examples'] = array(
    '#type' => 'submit',
    '#value' => t('See charts examples')
  );
    return $form;
}

Solution

  • I would simply nest a "markup" type form element with the image (assuming that it's a static image that doesn't change with user input): https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#markup

    Something like this should work:

    function my_module_start_form($form, &$form_state) {
      $image_options = array(
        'path' => 'path/to/img.jpg', 
        'alt' => 'Test alt',
        'title' => 'Test title',
        'width' => '50%',
        'height' => '50%',
        'attributes' => array('class' => 'some-img', 'id' => 'my-img'),
      );
      $image = theme('image', $image_options);
      $form['start']['image'] = array(
        '#markup' => $image,
      );
      ETC...
    

    I'm assuming the ['start'] form element is one fieldset, and you'll have another fieldset for the other grouping that includes an image and some form elements.