Hi all,

I just can't seem to get this right. I would like to create a new widget for the file field type.
Why? Because I want the widget to contain the default file upload element but also two textareas. One above the file upload element and one below. That way, users can put text above the (formatted) uploaded file and below the uploaded file. My module is a sandbox project calld Jmol (basically a formatter for molecular structure files). But this is irrelevant.
I'm guessing that the widget form for the file field type will by default contain a file upload element. Here I tried to add the prefix and suffix textareas.
The result is that when I create a new content type using file field and this new 'Molecular structure' widget, I can see the prefix and suffix, but not the file upload element upon the creation of a new node, which I thought would be automatically implemented, no?

This is my go untill now:

<?php
/**
* Implements hook_field_widget_info()
* Expose a new widget
*/
function jmol_field_widget_info() {
  return array(
   
'jmol_jmolfield_widget' => array(
     
'label' => t('Molecular structure'),
     
'field types' => array('file'),
     
'behaviors' => array(
       
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
       
'default value' => FIELD_BEHAVIOR_DEFAULT,
      ),
    ),
  );
}

/**
* Implements hook_field_widget_form()
* Defines what elements you want in the form to display to the user
*/
function jmol_field_widget_form(&$form,&$form_state,$field,$instance,$langcode,$items,$delta,$element) {
  if (
$instance['widget']['type'] == 'jmol_jmolfield_widget') {
   
$widget = $instance['widget'];
   
$settings = $widget['settings'];
   
$element['prefix'] = array(
     
'#title' => t('Paragraph text above the applet'),
     
'#type' => 'textarea',
     
// use #default_value to prepopulate the element
      // with the current saved value
     
'#default_value' => isset($items[$delta]['prefix']) ? $items[$delta]['prefix'] : '',
     
'#cols' => 50,
     
'#rows' => 10,
     
'#resizable' => TRUE,
     
'#weight' => 10,
    );
   
$element['suffix'] = array(
     
'#title' => t('Paragraph text below the applet'),
     
'#type' => 'textarea',
     
// use #default_value to prepopulate the element
      // with the current saved value
     
'#default_value' => isset($items[$delta]['suffix']) ? $items[$delta]['suffix'] : '',
     
'#cols' => 50,
     
'#rows' => 10,
     
'#resizable' => TRUE,
     
'#weight' => 30,
    );
  }
  return
$element;
}
?>

Comments

Why no use the field

Why no use the field collection module, make a collection with a file field, two text fields and use the new collection as a field in the desired content type.

Sounds great, but can this be

Sounds great, but can this be done programmatically?
The new field collection should be created when users install my module.

Wakken!

Ok, this seems like a good

Ok, this seems like a good compromise. :)
Users can install Field Collection and create their own collections.
I'm don't want to reinvent the wheel.

Thanks for the tip!

Wakken!

I believe you can use either

I believe you can use either ctools and/or features to export the field collection and then include in your module

Ah ctools. I'm gonna have to

Ah ctools. I'm gonna have to read up on that one. :)

Wakken!

nobody click here