Quantcast
Channel: Recent posts across whole site
Viewing all articles
Browse latest Browse all 49221

So near yet so far with a filefield and textarea compound field

$
0
0

Hey,

Firstly, am new to this group, so hopefully my discussion item is relevant.

I have been spending the last few days trying out a compound CCK field, using examples such as http://www.poplarware.com/articles/cck_field_module as a great starting point. I have come to realise such how complex an area this is, but I feel I have managed to get quite far along a solution, and wanted to ask around if anyone had any pointers or advice for completing it.

I'm looking to create a CCK field containing a textarea element, and an imagefield element so that a content editor could create n number of 'paragraphs' (textareas) in a node, with images attached to the paragraph. I also want the imagefield to be optional within the compound CCK field; you should be able to have a paragraph without an image. This differs slightly from Jennifer's img_cap_tax_fld example (from PoplarWare) in that the caption field appears after a file has been uploaded, and is stored serially with the file.

So far, my module is structured as follows:

function MYMODULE_elements(), an implementation of Forms API hook_elements() sets up an $elements array, adding the imagefield_widget, imagefield_widget_upload_validators, my MYMODULE_process function and the text_textarea_process function (from the CCK text field):

<?php
/**
* Implementation of Form API's hook_elements().
*/
function MYMODULE_elements() {

$imgel = imagefield_elements();
   
  
$elements = array(
    
'MYFIELD_entry' => $imgel['imagefield_widget']
  ); 
  
  
$elements['MYFIELD_entry']['#upload_validators'] = imagefield_widget_upload_validators($field);
   
  
$elements['MYFIELD_entry']['#input'] = TRUE;
  
$elements['MYFIELD_entry']['#process'][] = 'MYMODULE_MYFIELD_entry_process';
$elements['MYFIELD_entry']['#process'][] = 'text_textarea_process';
 
  return
$elements;
}
?>

My process callback then attempts to add my additional paragraph element:

<?php
/**
* Process callback for widget
*/
function MYMODULE_MYFIELD_entry_process($element, $edit, &$form_state, $form) {

 
$defaults = $element['#default_value']['data'];
 
// Make sure $defaults is unserialized
 
if( !is_array( $defaults )) {
   
$defaults = unserialize( $defaults );
  }
 
$field = content_fields($element['#field_name'], $element['#type_name']);
 
 
$element['paragraph'] = array(
   
'#title' => t('Paragraph'),
   
'#type' => 'text_textarea',
   
'#input' => TRUE,
   
'#columns' => 60,
   
'#rows' => 4,
   
'#default_value' => $defaults['paragraph'],
   
'#weight' => 2,
  );

  return
$element;
}
?>

Unfortunately this is where I have stalled, due to not really understanding the imagefield (which is essentially the filefield) widget. I get the error warning: array_merge() [function.array-merge]: Argument #1 is not an array in /path/to/drupal/sites/all/modules/contrib/cck/filefield/filefield_widget.inc on line 255. Delving into the filefield_widget.inc it seems it is not recognising $item as an array. But I'm a little lost as to how to fix this.

I'm guessing the problem is as a result of essentially using a widget inside a widget.

Would be really happy to discuss this further with people to find a solution. It seems a few people are looking for similar functionality so am more than willing to contribute if I can.

Thanks!

Jon


Viewing all articles
Browse latest Browse all 49221

Trending Articles