0

I'm creating a widget with an image field. But I keep writing 0 to the database. But i need the fid to be saved. I know I can manipulate the data with hook_field_presave(). But the items array only contains image => '0'.

mymodule.module

/**
* Implements hook_field_widget_form().
*/
function mymodule_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
...
  $element['image'] = array(
    '#type' => 'managed_file',
    '#name' => 'mymodule_custom_image',
    '#title' => t('Image'),
    '#size' => 40,
    '#description' => t(""),
    '#upload_location' => 'public://',
    '#theme' => 'mymodule_custom_thumb_upload',
  ); 
...
}

Edit:

/**
* Implements hook_field_widget_info().
*/
function mymodule_field_widget_info() {
  return array(
    'mymodule_polygone_widget' => array(
      'label' => t('Default'),
      'field types' => array('mymodule_polygone'),
    ),
  );
}

mymodule.install

/*
* Implements hook_field_schema().
*/
function mymodule_field_schema($field) {
  $columns = array(
    'image' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
    'zones' => array('type' => 'text'),
  );

  return array(
    'columns' => $columns,
    'indexes' => array(),
  );
}
  • On your hook_field_widget_info() function what do you have as your return statement? This actually matters – FatGuyLaughing Aug 11 '14 at 14:29
  • Added the code :). – mgoubert Aug 11 '14 at 14:33
  • Well I apologize, I meant what is the return on your hook_field_widget_form not info. I'm sorry about that. – FatGuyLaughing Aug 11 '14 at 14:41
0

Found it: Forgot the 'input ...' in my custom theme function :).

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

  if (isset($element['#file']->uri)) {
      $output = '<div id="edit-logo-ajax-wrapper"><div class="form-item form-type-managed-file form-item-logo"><span class="file">';
      $output .= '<img src="' . file_create_url($element['#file']->uri) . '" />';
      $output .= '</span><input type="submit" id="edit-' . $element['#name'] . '-remove-button" name="' . $element['#name'] . '_remove_button" value="Remove" class="form-submit ajax-processed">';
      $output .= '<input type="hidden" name="' . $element['#name'] . '[fid]" value="' . $element['#file']->fid . '">';

      return $output;
  }
}
|improve this answer|||||

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.