I'm currently working with the new webform API (thanks!), and I'm stuck. I've successfully built a custom component that renders correctly when being viewed and submitted. My component renders a list of options that a user can select as either radio buttons or check boxes. (I'm pulling data from the DB in order to display the list of options, otherwise I would just use the select component.) Here is the array that I return from within _webform_display_MY_COMPONENT:

  return array(
    '#title' => $component['name'],
    '#weight' => $component['weight'],
    '#theme' => 'webform_display_select',
    '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
    '#post_render' => array('webform_element_wrapper'),
    '#component' => $component,
    '#format' => $format,
    '#value' => $products,
  );

When I do a var_dump on the $products array, I get the following:

array(1) { 
  [0]=> string(22) "Product 3, Price: $179" 
}
array(3) { 
  [0]=> string(21) "Product 1, Price: $99" 
  [1]=> string(22) "Product 4, Price: $129" 
  [2]=> string(21) "Product 5, Price: $29"   
} 

array(3) represents a component that included check boxes, and the user selected 3 out of the five options. The problem is that the result page for that particular submission only ever displays the first item in the array. I'd like it to display all three in an unordered list, like the default "select" component does for checkboxes.

I'd love to contribute back by providing patches to the webform module itself, but I'm simply not there yet with my PHP/drupal knowledge. Instead, I've started a documentation page for the webform API, and I'll be adding to it as I work through this small project. Hopefully you'll be able to refer people there when you get questions about the webform API.

Thanks!

Comments

quicksketch’s picture

(I'm pulling data from the DB in order to display the list of options, otherwise I would just use the select component.)

You should use hook_webform_select_options_info() to create your own database-driven list and use it within a select component.

The reason your items don't show up is because the theme_webform_display_select function loads the list of select options from the component, but if you're loading the list of results from somewhere else, then it assumes this must be an "other..." choice manually entered by the user, in which case there can only be one value.

ldweeks’s picture

hook_webform_select_options_info() is a very helpful function, but there are some limitations to it that kept me from using it in this case. For one, you can't edit options within a pre-built list, and in my case the site-builder needs to be able to do that. So I decided to roll my own component.

I'm pulling the options available to the site-builder (on the component edit page) from the database, and he then chooses which of those items he wants to appear in the actual webform. Once he select the items he wants, those options do get stored in the "extra" column of the webform_component table.

Your link to theme_webform_display_select tipped me off to the fact that that function checks the value of $component['extra']['multiple']. In my code, I had used a key other than "multiple" in the extra array. When I changed it to "multiple", it worked.

The results now display correctly, but only after I've flushed the theme registry (and I have to flush it each time). If I simply view the result of submission without flushing the cache, my component results don't appear at all. Would it be best for me to write my own theme function for my component?

Thanks,

quicksketch’s picture

Would it be best for me to write my own theme function for my component?

Yes. Considering we may change select.inc in the future to accommodate more features, you shouldn't make any assumptions about your component and select.inc sharing code.

ldweeks’s picture

Status: Active » Fixed

Thanks very much, quicksketch. I figured out what I needed to keep moving on my project, so I'll just post a few notes to try and help the next guy and mark this issue "fixed".

If you ever need to roll your own custom component, and your custom component is some variation on a select list, then you'll need to implement a theme function for when the results are displayed (as quicksketch mentioned above).

The file "select.inc", located in the components directory, includes the following function:

/**
 * Format the text output for this component.
 */
function theme_webform_display_select($element) {
  ... 
}

Below is a very simple function that you can use to theme your custom "select" component. It simply lists the choices as an unordered list on the results page:

function theme_webform_display_YOUR_COMPONENT($element) {
  
  if (isset($element['#value'])) {
    $output = theme('item_list', $element['#value']);
  }

  return $output;
}

Once you have the function defined, you'll need to make sure that you implement the webform hook, _webform_display_YOUR_COMPONENT. Take a look at here for more information about the hook. That function will need to return the array specified in the api, and one of the elements in that array should be:

'#theme' => 'webform_display_YOUR_COMPONENT',

Finally, you'll need to make sure that you implement the following function within your module, otherwise your custom theme function will never be called:

/**
 * Module specific instance of hook_theme().
 *
 * This allows each Webform component to add information into hook_theme().
 */
function _webform_theme_YOUR_COMPONENT() {
  return array(
    'webform_display_YOUR_COMPONENT' => array(
      'arguments' => array('element' => NULL),
    ),
  );
}

If you get all that right, you should have things working nicely.

Status: Fixed » Closed (fixed)

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