2

Basically I want to create a block with my module which's edit page has a few content fields and two image upload fields. When visiting the node where this block is rendered, I want to render the images with a certain image style, while on the edit form it would be cool to show the images as a thumbnail preview.

I am adding a field like described here: https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#managed_file but this makes me curious:

New files are uploaded with a status of 0 and are treated as temporary files which are removed after 6 hours via cron. Your module is responsible for changing the $file objects status to FILE_STATUS_PERMANENT and saving the new status to the database.

Also, what is the difference of managed_file and file when I want to provide an image upload field?

Currently I have in hook_block_configure:

$form['left']['image'] = array(
        '#type' => 'managed_file',
        '#title' => 'test',
    );

Where do I define how this image shall be rendered (image style)? Do I have to set that permanent flag?

Thanks

3

You cannot use fields from the Field API in forms of the Form API directly, as Fields can be used directly in entities, and entities can be used directly with forms.

Instead, you can use the approach described here, as Clive kindly pointed out. Alternatively, you can use ctools_field_invoke_field() function of CTools module too.

The two fields 'file' and 'managed_file' of the Form API are similar, with the difference that the latter provides a progress upload widget. Also, managed_file will add the file entry to a table with the same name. Beyond that, there is some work involved in order to render the file on a page, and that is not the imagefield of the Field API.

  • 1
    It's a bit strangled but you can get field API fields into normal forms pretty easily (without ctools). See Scott Joudry's answer on this question; I use that method quite regularly, works a treat – Clive Feb 12 '14 at 14:47
  • 1
    Very nice one, thank you @Clive. I edited the answer to include this one. – Wtower Feb 12 '14 at 14:52
1

Also, what is the difference of managed_file and file when I want to provide an image upload field?

There isn't a difference for what you're doing. managed files let you control more aspects of the file, like using it in other places, applying rules, etc.

Where do I define how this image shall be rendered (image style)?

Hmm.. not sure I understand. image styles are handled on output, not during upload.

Do I have to set that permanent flag? you do, but afaik that will be set when your form is saved.

0

Put this piece of code into block: $fid is a file id.

$img = file_load($fid);
$blockcontent = theme('image_style',array('style_name' => 'YOURSTYLENAME', 'path' => $img->uri));

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.