user4554391
user4554391

Reputation: 11

Drupal 7 | Form managed file upload image preview

I'm can't get to work image preview on Drupal 7 Form managed file. I have for code like this in template.php:

function testform($form, &$form_state) {
  $form = array();
  $form['con_image'] = array(
        '#type' => 'managed_file',
        '#title' => t('Image'),
        '#required' => TRUE,
        '#default_value' => variable_get('con_image', ''),
        '#progress_indicator' => 'bar',
        '#progress_message' => 'Uploading ...',
        '#upload_location' => 'public://gallery/',
        '#theme' => 'test',
        '#upload_validators' => array(
            'file_validate_is_image' => array(),
            'file_validate_extensions' => array('jpg jpeg'),
            'file_validate_image_resolution' => array('6000x4000','800x600'),
            'file_validate_size' => array(6 * 1024 * 1024),

        ),


  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Add to site'),
  );
  return $form;
}

I call testform after checking additional conditions (like userpoints) via code

$arr = drupal_get_form('testform');
      print drupal_render($arr);

The form itself is working, I'm able to add image to node (programmatically) but cannot get the preview of image during upload process. I try to use #theme but it seems doesn't work at all. My theme function looks like this:

function theme_test($variables) {
  $element = $variables['element'];

  $output = '';

  $base = drupal_render_children($element); // renders element as usual

  if($element['fid']['#value'] != 0) {
    // if image is uploaded show its thumbnail to the output HTML
    $output .= '<div class="multifield-thumbnail">';
    $output .= theme('image_style', array('style_name' => 'thumbnail', 'path' => file_load($element['fid']['#value'])->uri, 'getsize' => FALSE));
    $output .= '</div>';
  }

Any ideas?

Upvotes: 0

Views: 1959

Answers (1)

andodew
andodew

Reputation: 11

You need to declare your theme with a hook_theme in your module.

function yourmodule_theme() {
   return array(
     'test' => array(
        'render element' => 'element',
    )
  );
}

Upvotes: 1

Related Questions