Hi!

I'd really need your module to have field text with a drop-down list to select a value from a list. Is this possible?

Thanks in advance

Comments

alan d.’s picture

Title: selecting from a data list » Extending the available field types
Component: Code » Documentation
Category: feature » support
Status: Active » Needs review

The module is by design a helper tool for developers with a few extra "ease of use" fields. I did not want to go down the Webform path adding 10+ different fields via an interface. The way to add any FAPI field follows the following paradigm.

If you use the Drupal alter hook supplied, you can add any field type. All you need is a custom module and something like this:

<?php

function MYMODULE_imagefield_extended_widget_alter($element, $extra_values) {
  // If you need the field or widget to conditionally add a FAPI field, use these.
  $field = content_fields($element['#field_name'], $element['#type_name']);
  $widget = $field['widget'];

  return array(
    'MYFIELD' => array(
      '#type' => 'select',
      '#title' => t('My select field'),
      '#options' => array('a' => t('A option'), 'b' => t('B option')),
      '#default_value' => isset($extra_values['MYFIELD']) ? $extra_values['MYFIELD'] : 'b', // this makes b the default
    ),
  )
}

?>

The data should be saved and loaded by FileField and your theming functions would present the value to the end user.

For the custom module, all you need is the info file and this function.

kiko’s picture

Thanks a lot!

alan d.’s picture

Status: Needs review » Fixed

I've added this to the documentation page, http://drupal.org/node/628640. Reopen if you have any issues.

;)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

bleen’s picture

Status: Closed (fixed) » Active

Alan D.

I'm having no luck with teh code you supplied. My module (which works for other mini tasks around the site I am working on) is called "strong_helper". The code I am using is here:

function strong_helper_imagefield_extended_widget_alter($element, $extra_values) {
  // If you need the field or widget to conditionally add a FAPI field, use these.
  dpm('in the strong_helper_imagefield_extended_widget_alter() function');
  //$field = content_fields($element['#field_name'], $element['#type_name']);
  //$widget = $field['widget'];

  return array(
    'crop' => array(
      '#type' => 'select',
      '#title' => t('Crop'),
      '#options' => array('top' => t('top'), 'middle' => t('middle'), 'bottom' => t('bottom')),
      '#default_value' => isset($extra_values['crop']) ? $extra_values['crop'] : 'top', 
    ),
  )
}

Is there anything else I need to do? I've cleared my cache...

I see the line

$extra_fields = module_invoke_all('imagefield_extended_widget', $element, $extra_values);

in the IFE module but it's as though my module is being skipped.

Thoughts? ideas?

(drupal 6.14 | IFE 6.x-3.1)

alan d.’s picture

Your code looks OK.

Is the function in an include or the module file? It needs to be in the main module file.

The widget is an ImageField Extended widget too? It does not work on the standard imagefield. (Although I think I know a way to eliminate the need for this additional widget now - I just need the time to try this out)

Debugging.

Is the code being executed at the line at "module_invoke_all" is called? In your alter hook? And what is being returned after the invoke all call?

Cheers

bleen’s picture

Status: Active » Closed (fixed)

Alan D. ... thanks for getting back to me. I'm embarrassed to report that the problem appears to be that the module was disabled by another user. D'oh!!

alan d.’s picture

Thanks for the feedback. Glade it was easily resolved.