I created a custom module, and I need to add a new field: image with cropping programmatically into a form. I know how to add from the content type, but I need to do it programmatically. Is it posible to do it?

$form['group_anuncio'] = array( 
  '#type' => 'fieldset',    
  '#title' => t('2- Sube tu anuncio'),
  '#collapsible' => TRUE,   );

$form['group_anuncio']['titulo'] = array(
  '#type' => 'textfield',
  '#title' => t('Título'),
  '#required' => TRUE,  

$form['group_anuncio']['image'] = array();

I need to insert the code in the third form.

I'm trying with this code, and it seems like it works for unloading. The problem is that I don't know how to crop the image.

$form['group_anuncio']['imagen'] = array(
        '#title' => t('Image'),
        '#type' => 'managed_file',
        '#upload_validators' => array('file_validate_extensions' => array('jpeg jpg png gif')),
        '#upload_location' => 'public://',
        '#progress_indicator' => 'throbber',
        '#status' => FILE_STATUS_PERMANENT,
        '#progress_message' => 'One moment while we save your file...',

  • Yes it is, either use this question to use Drupal's built-in code, or use it to find a built-in code and copy it. – Mołot Mar 31 '14 at 8:31
  • I'm sorry maybe my explanation it wasn't very good. I've edited the question. I hope you can help me. – more770 Mar 31 '14 at 10:07
  • Drupal way is to apply filters on output rather than on input, and linked question answers how to do it. Also, we can't simply write the code for you, that's not what this site is for. – Mołot Mar 31 '14 at 10:24
  • Have you seen this question - they have several different methods. I tried them myself, but didn't have much luck, sorry! You might be better off just using the managed_file forms api field type and asking the user to pre-crop their images. – alexkb Mar 31 '14 at 12:58
  • I've edit my question. I've done one part... – more770 Mar 31 '14 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.