Vocabulary.inc to get taxonomy terms in webform

Periander - July 29, 2007 - 12:16
Project:Webform
Version:5.x-2.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active
Description

Hi,

I'm new to Drupal, I'm loving it, but I've hit a bit of a wall with adding a list of taxonomy items, (just the strings), to a webform select component.

Anyone have any ideas?

Also, if this is not the right place/way to ask this question please let me know!

Thanks in advance.

#1

Sutharsan - August 22, 2007 - 18:18

I want to know this too! I need to ad a taxonomy select list (or a select list with tags) to the webform. Manually adding the terms to a webform select list is no option.

#2

Sutharsan - September 23, 2007 - 20:30
Title:How Do I Add A Taxonomy Term List To A Webform Select Component?» Add A Taxonomy Term List To A Webform Select Component
Category:support request» feature request
Status:active» patch (code needs review)

Based on select.inc I have made an include file to handle taxonomy tags. Code is tested but only for my (limited) use of taxonomies.

AttachmentSize
webform.vocabulary.inc_.162948-00.patch13.48 KB

#3

goose2000 - October 5, 2007 - 13:40

Wow, thanks - I'll try it out; Do we need to patch the select.inc with this file or is this a new webform include (component)?

#4

Sutharsan - October 7, 2007 - 12:51

This patch will create a new file: components/vocabulary.inc. select.inc is untouched.

#5

Summit - January 6, 2008 - 19:53

Hi,

Could this patch be updated for webform 5 1.7
The values of the vocabularies are not saved when I tested this.

Taxonomy support for webform is a great + for me, so please update the patch so it works with webform 5 1.7!
Thanks in advance,
greetings,
Martijn

#6

Summit - January 6, 2008 - 23:04
Version:5.x-1.4» 5.x-1.7
Component:Miscellaneous» Code

I think this would really benefit webform. Please add it to the newest version.
If it's working..
greetings,
Martijn

#7

Summit - January 7, 2008 - 08:49
Category:feature request» bug report

Hi,

I got the following error on this code:

PHP Fatal error:  [] operator not supported for strings in
/public_html/modules/webform/components/vocabulary.inc on line 151

The mail I got back when using vocabulary support in webform.module gives back blank terms:

Regionaal:
      •
      •
Rubrieken:
      •
      •

Where "Regionaal" and "Rubrieken" are my vocabularies.
Does anybody know what is wrong with this vocabulary.inc?

The complete code is as follows:

<?php
// $Id$
/**
* function webform_edit_vocabulary
* Create a set of form items to be displayed on the form for editing this component.
* Use care naming the form items, as this correlates directly to the database schema.
* The component "Name" and "Description" fields are added to every component type and
* are not necessary to specify here (although they may be overridden if desired).
* @returns An array of form items to be displayed on the edit component page
**/
function _webform_edit_vocabulary($currfield) {
$edit_fields = array();

$result = db_query(db_rewrite_sql("SELECT vid, name FROM {vocabulary}"));
while ($v = db_fetch_object($result)) {
   $vocabularies[$v->vid] = $v->name;
}
$edit_fields['extra']['vocabulary'] = array(
   '#type' => 'select',
   '#title' => t("Vocabulary"),
   '#options' => $vocabularies,
   '#default_value' => $currfield['extra']['vocabulary'],
   '#description' => t('The terms of this vocabulary can be selected by the user'),
   '#weight' => -2,
   '#required' => TRUE,
);
$edit_fields['extra']['multiple'] = array(
   '#type' => 'checkbox',
   '#title' => t("Multiple"),
   '#return_value' => 'Y',
   '#default_value' => ($currfield['extra']['multiple']=='Y'?TRUE:FALSE),
   '#description' => t('Check this option if the user should be allowed to choose multiple values.'),
);
$edit_fields['extra']['aslist'] = array(
   '#type' => 'checkbox',
   '#title' => t("Listbox"),
   '#return_value' => 'Y',
   '#default_value' => ($currfield['extra']['aslist']=='Y'?TRUE:FALSE),
   '#description' => t('Check this option if you want the select component to be of listbox type instead of radiobuttons or checkboxes.'),
);
return $edit_fields;
}

/**
* function webform_render_vocabulary
* Build a form item array containing all the properties of this component
* @param $component An array of information describing the component, directly correlating to the webform_component database schema
* @returns An array of a form item to be displayed on the client-side webform
**/
function _webform_render_vocabulary($component) {
$form_item = array(
   '#title'         => htmlspecialchars($component['name'], ENT_QUOTES),
   '#required'      => $component['mandatory'],
   '#weight'        => $component['weight'],
   '#description'   => _webform_filtervalues($component['extra']['description']),
   '#prefix'        => '<div class="webform-component-'. $component['type'] .'" id="webform-component-'. $component['form_key'] .'">',
   '#suffix'        => '</div>',
);

if ($component['extra']['aslist'] == 'Y' && $component['extra']['multiple'] != 'Y') {
   $options = array('' => t('select...'));
}
else {
   $options = array();
}

// Extract terms from user-selected vocabulary
$tree = taxonomy_get_tree($component['extra']['vocabulary']);
if ($tree && count($tree) > 0) {
   foreach ($tree as $term) {
     $options[$term->name] = str_repeat('-', $term->depth). $term->name;
   }
}
$default_value = current($options);  // first element of the array is the default value

// Set the component options
$form_item['#options'] = $options;

// Set the default value
if ($default_value) {
   // Convert default value to a list if necessary
   if ($component['extra']['multiple'] == 'Y') {
     if (strpos($default_value, ',')) {
       $varray = explode(',', $default_value);
       foreach ($varray as $key => $v) {
         if (array_key_exists(_webform_safe_name($v), $options)) {
           $form_item['#default_value'][] = _webform_safe_name($v);
         }
         else {
           $form_item['#default_value'][] = $v;
         }
       }
     }
     else {
       if (array_key_exists(_webform_safe_name($default_value), $options)) {
         $form_item['#default_value'] = _webform_safe_name($default_value);
       }
       else {
         $form_item['#default_value'] = $default_value;
       }
     }
   }
   else {
     if (array_key_exists(_webform_safe_name($default_value), $options)) {
       $form_item['#default_value'] = _webform_safe_name($default_value);
     }
     else {
       $form_item['#default_value'] = $default_value;
     }
   }
}

if ($component['extra']['aslist'] == 'Y') {
   // Set display as a select list:
   $form_item['#type'] = 'select';
   if ($component['extra']['multiple'] == 'Y') {
     $form_item['#multiple'] = TRUE;
   }
}
else {
   if ($component['extra']['multiple'] == 'Y') {
     // Set display as a checkbox set
     $form_item['#type'] = 'checkboxes';
    
   }
   else {
     // Set display as a radio set
     $form_item['#type'] = 'radios';
   }
}
return $form_item;
}


/**
* function _webform_submission_display_vocabulary
* Display the result of a textfield submission. The output of this function will be displayed under the "results" tab then "submissions"
* @param $data An array of information containing the submission result, directly correlating to the webform_submitted_data database schema
* @param $component An array of information describing the component, directly correlating to the webform_component database schema
* @returns Textual output formatted for human reading.
**/
function _webform_submission_display_vocabulary($data, $component) {
$form_item = _webform_render_vocabulary($component);
if ($component['extra']['multiple'] == 'Y') {
   // Set the value as an array
   foreach ((array)$data['value'] as $key => $value) {
     if (array_key_exists(_webform_safe_name($value), $form_item['#options'])) {
       $form_item['#default_value'][] = _webform_safe_name($value);
     }
     else {
       $form_item['#default_value'][] = $value;
     }
   }
}
else {
   // Set the value as a single string
   foreach ((array)$data['value'] as $value) {
     if ($value !== '0') {
       if (array_key_exists(_webform_safe_name($value), $form_item['#options'])) {
         $form_item['#default_value'] = _webform_safe_name($value);
       }
       else {
         $form_item['#default_value'] = $value;
       }
       break;
     }
   }
}
$form_item['#attributes'] = array("disabled" => "disabled");
return $form_item;
}


/**
* function webform_submit_vocabulary
* Translates the submitted 'safe' form values back into their un-edited original form
* @param $data The POST data associated with the component
* @param $component An array of information describing the component, directly correlating to the webform_component database schema
* @returns Nothing
**/
function _webform_submit_vocabulary(&$data, $component) {
$value = _webform_filtervalues($component['value']);
$tree = taxonomy_get_tree($component['extra']['vocabulary']);
if ($tree && count($tree) > 0) {
   foreach ($tree as $term) {
     $options[$term->name] = str_repeat('-', $term->depth). $term->name;
   }
}

if (is_array($data)) {
   foreach ($data as $key => $value) {
     if ($value) {
       $data[$key] = $options[$key];
     }
   }
}
elseif (!empty($data)) {
   $data = $options[$data];
}
}

/**
* theme_webform_mail_vocabulary
* Format the output of emailed data for this component
*
* @param mixed $data A string or array of the submitted data
* @param array $component An array of information describing the component, directly correlating to the webform_component database schema
* @returns string Textual output to be included in the email
*/
function theme_webform_mail_vocabulary($data, $component) {
// Extract terms from user-selected vocabulary
$tree = taxonomy_get_tree($component['extra']['vocabulary']);
if ($tree && count($tree) > 0) {
   foreach ($tree as $term) {
     $options[$term->name] = str_repeat('-', $term->depth). $term->name;
   }
}
$options = array();

// Generate the output
if ($component['extra']['multiple']) {
   $output = $component['name'] .":\n";
   foreach ($data as $value) {
     if ($value) {
       if ($options[$value]) {
         $output .= "    • ". $options[$value] ."\n";
       }
       else {
         $output .= "    • ". $options[_webform_safe_name($value)] ."\n";
       }
     }
   }
}
else {
   $output = $component['name'] .": ". $data ."\n";
}
return $output;
}

/**
* function _webform_help_vocabulary
* Module specific instance of hook_help
**/
function _webform_help_vocabulary($section) {
switch ($section) {
   case 'admin/settings/webform#vocabulary_description':
     $output = t("Allows creation of checkboxes, radio buttons, or select menus with taxonomy terms.");
     break;
}
return $output;
}

/**
* function _webform_analysis_view_vocabulary
* Calculate and returns statistics about results for this component from all submission to this webform. The output of this function will be displayed under the "results" tab then "analysis"
* @param $component An array of information describing the component, directly correlating to the webform_component database schema
* @returns An array of data rows, each containing a statistic for this component's submissions.
**/
function _webform_analysis_rows_vocabulary($component) {
$component['extra'] = unserialize($component['extra']);
// Extract terms from user-selected vocabulary
$tree = taxonomy_get_tree($component['extra']['vocabulary']);
if ($tree && count($tree) > 0) {
   foreach ($tree as $term) {
     $options[$term->name] = str_repeat('-', $term->depth). $term->name;
   }
}
$options = array();

$query = 'SELECT data, count(data) as datacount '.
   ' FROM {webform_submitted_data} '.
   ' WHERE nid = %d '.
   ' AND cid = %d '.
   " AND data != '0' AND data != '' ".
   ' GROUP BY data ';
$result = db_query($query, $component['nid'], $component['cid']);
$rows = array();
while ($data = db_fetch_array($result)) {
   if ($options[$data['data']]) {
     $display_option = $options[$data['data']];
   }
   else {
     $display_option = $data['data'];
   }
   $rows[] = array($display_option, $data['datacount']);
}
return $rows;
}

/**
* function _webform_table_data_vocabulary
* Return the result of this component's submission for display in a table. The output of this function will be displayed under the "results" tab then "table"
* @param $data An array of information containing the submission result, directly correlating to the webform_submitted_data database schema
* @returns Textual output formatted for human reading.
**/
function _webform_table_data_vocabulary($data) {
// Set the value as a single string
if (is_array($data['value'])) {
   foreach ($data['value'] as $value) {
     if ($value !== '0') {
       $output .= check_plain($value) ."<br />";
     }
   }
}
else {
   $output = check_plain(empty($data['value']['0']) ? "" : $data['value']['0']);
}
return $output;
}


/**
* function _webform_csv_headers_vocabulary
*  Return the header information for this component to be displayed in a comma seperated value file. The output of this function will be displayed under the "results" tab then "download"
* @param $component An array of information describing the component, directly correlating to the webform_component database schema
* @returns An array of data to be displayed in the first three rows of a CSV file, not including either prefixed or trailing commas
**/
function _webform_csv_headers_vocabulary($component) {
$header = array();
$header[0] = '';

if ($component['extra']['multiple']) {
   $header[1] = $component['name'];
   $tree = taxonomy_get_tree($component['extra']['vocabulary']);
   if ($tree && count($tree) > 0) {
     foreach ($tree as $term) {
       $items[] = $term->name;
     }
   }
   foreach ($items as $item) {
     $header[2] .= "\,". $item;
   }
   // Remove the preceding extra comma
   $header[2] = substr($header[2], 2);
}
else {
   $header[2] = $component['name'];
}
return $header;
}

/**
* function _webform_csv_data_vocabulary
* Return the result of a textfield submission. The output of this function will be displayed under the "results" tab then "submissions"
* @param $data An array of information containing the submission result, directly correlating to the webform_submitted_data database schema
* @returns Textual output formatted for CSV, not including either prefixed or trailing commas
**/
function _webform_csv_data_vocabulary($data, $component) {
$value = _webform_filtervalues($component['value']);
$options = array();
// Extract terms from user-selected vocabulary
$tree = taxonomy_get_tree($component['extra']['vocabulary']);
if ($tree && count($tree) > 0) {
   foreach ($tree as $term) {
     $options[$term->name] = $term->name;
   }
}

if ($component['extra']['multiple']) {
   foreach ($options as $key => $item) {
     if (in_array($key, (array)$data['value']) === true) {
       $output .= '\,Yes';
     }
     else {
       $output .= '\,No';
     }
   }
   // Remove the preceding extra comma
  $output = substr($output, 2);
}
else {
   $output = $data['value'][0];
}
return $output;
}

I think the problem is on this part of the code:

/**
* theme_webform_mail_vocabulary
* Format the output of emailed data for this component
*
* @param mixed $data A string or array of the submitted data
* @param array $component An array of information describing the component, directly correlating to the webform_component database schema
* @returns string Textual output to be included in the email
*/
function theme_webform_mail_vocabulary($data, $component) {
// Extract terms from user-selected vocabulary
$tree = taxonomy_get_tree($component['extra']['vocabulary']);
if ($tree && count($tree) > 0) {
   foreach ($tree as $term) {
     $options[$term->name] = str_repeat('-', $term->depth). $term->name;
   }
}
$options = array();

// Generate the output
if ($component['extra']['multiple']) {
   $output = $component['name'] .":\n";
   foreach ($data as $value) {
     if ($value) {
       if ($options[$value]) {
         $output .= "    • ". $options[$value] ."\n";
       }
       else {
         $output .= "    • ". $options[_webform_safe_name($value)] ."\n";
       }
     }
   }
}
else {
   $output = $component['name'] .": ". $data ."\n";
}
return $output;
}

The newest select.inc (the file where Erik made the vocabulary.inc from) has one difference to the old select.inc and that is:
Old select
   $output = $component['name'] .": ". $data ."\n";

New Select:
    $output = $component['name'] .": ". $options[$data] ."\n";

Has this something to do with it?

Could somebody please help me to get this vocabulary support of webform working?

Thanks in advance!
greetings,
Martijn

#8

Summit - January 7, 2008 - 10:56
Status:patch (code needs review)» patch (code needs work)

Hi,

I have seen that the single term option works fine, but the selection of multiple terms not.
May be the code:

if ($component['extra']['multiple']) {
   $output = $component['name'] .":\n";
   foreach ($data as $value) {
     if ($value) {
       if ($options[$value]) {
         $output .= "    • ". $options[$value] ."\n";
       }
       else {
         $output .= "    • ". $options[_webform_safe_name($value)] ."\n";
       }
     }

Is not correct?
Can somebody please help?
Thanks in advance!
greetings,
Martijn

#9

Summit - January 22, 2008 - 10:32
Status:patch (code needs work)» patch (code needs review)

Hi,

Please can someone assist why the multiselect terms is not working?
The single select is working. The multiselect is working in the webform, but the email which is received has empty fields for selected terms..
Please assist!

Thanks a lot in advance!

greetings,
Martijn

#10

Summit - January 22, 2008 - 21:30
Status:patch (code needs review)» active

Hi,

THis error is away using: http://drupal.org/node/154870#comment-574381
Deleting [] from
$form_item['#default_value'][] becomes $form_item['#default_value']

I still don't have my vocabulary output on mail..If somebody has a suggestion. Please assist!

greetings,
Martijn

#11

Summit - January 26, 2008 - 01:26
Title:Add A Taxonomy Term List To A Webform Select Component» Vocabulary.inc to get taxonomy terms in webform

Hi,

I think the bug should be in:

function _webform_submission_display_vocabulary($data, $component, $enabled = false) {
  $form_item = _webform_render_vocabulary($component);
  if ($component['extra']['multiple'] == 'Y') {
    // Set the value as an array
    foreach ((array)$data['value'] as $key => $value) {
      if (array_key_exists(_webform_safe_name($value), $form_item['#options'])) {
        $form_item['#default_value'] = _webform_safe_name($value);
      }
      else {
        $form_item['#default_value'] = $value;
      }
    }
  }
  else {
    // Set the value as a single string
    foreach ((array)$data['value'] as $value) {
      if ($value !== '0') {
        if (array_key_exists(_webform_safe_name($value), $form_item['#options'])) {
          $form_item['#default_value'] = _webform_safe_name($value);
        }
        else {
          $form_item['#default_value'] = $value;
        }
        break;
      }
    }
  }
  $form_item['#disabled'] = !$enabled;
  return $form_item;
}

Especially in:
if (array_key_exists(_webform_safe_name($value), $form_item['#options'])) {
          $form_item['#default_value'] = _webform_safe_name($value);

This gives blank results back...
How can I debug this? Does somebody else also have this bug?
I attach my to webform 1.8 converted vocabulary.inc, please assist in solving this!Than webform has the taxonomy terms to be chosen from for your users!

Thanks in advance,
greetings,
Martijn

AttachmentSize
vocabulary.txt13.44 KB

#12

Summit - January 26, 2008 - 01:28
Version:5.x-1.7» 5.x-1.8

Hi, the attachment: http://drupal.org/files/issues/vocabulary.txt is converted to webform 1.8.
Again, please assist.
Thanks in advance!
greetings,
Martijn

#13

Summit - January 26, 2008 - 01:30

Something went wrong, pressed twice..sorry..to late in the morning here..greetings, Martijn

#14

quicksketch - February 22, 2008 - 18:12

Marked http://drupal.org/node/220733 as duplicate.

I'm not wild about this idea mostly because the code is 90% the same as select.inc, meaning every bug in select.inc will need to be fixed in vocabulary.inc also. Wouldn't it be possible to include as part of select.inc somehow instead?

#15

quicksketch - February 22, 2008 - 18:13
Version:5.x-1.8» 5.x-2.x-dev
Category:bug report» feature request

New features are no longer being added to the 1.x branch which is now maintenance-only.

#16

quicksketch - March 11, 2008 - 04:26

Marked http://drupal.org/node/230205 as duplicate.

#17

Summit - May 26, 2008 - 10:58

Hi,
I am testing webform 2.0 now, and it is great...But it still doesn't have taxonomy support for user-entries.
If it will not be in a seperate file as you described earlier, may be you could build it inside the select.inc please?
Thanks in advance for considering this. It would be a great add-on to webform I think!

greetings,
Martijn

#18

Summit - July 3, 2008 - 16:55
Title:Vocabulary.inc to get taxonomy terms in webform» Hierarchical select taxonomy in webform

Hi,
There is a great new module release in the making of hierarchical select (version 3). www.drupal.org/project/hierarchical_select
The taxonomy terms can be put in dropdown lists with depth option.
Could this be implemented in webform also, so finally a user can also select taxonomy terms with other values to fill a webform?

Thanks in advance for considering this!

greetings,
Martijn

#19

quicksketch - July 3, 2008 - 17:09
Title:Hierarchical select taxonomy in webform» Vocabulary.inc to get taxonomy terms in webform

Please make a new request Summit. That one is quite different from the basic need of getting taxonomy terms into a select list.

#20

Wim Leers - July 3, 2008 - 17:27

quicksketch: it's not :) It's a simple matter of changing #type and passing the preferred settings. You won't have to set #options anymore, either. But it definitely should be the second step, because it requires an additional dependency (on the hs_taxonomy module, which is included in HS).

#21

Wim Leers - July 3, 2008 - 17:30

Here's the separate issue for HS support: http://drupal.org/node/278236#comment-906938.

#22

Summit - July 3, 2008 - 17:34

Quicksketch, do you still think it is a seperate issue? If so off course I make another one then.

It would be great to join both great modules together.

greetings,
Martijn

#23

quicksketch - July 3, 2008 - 18:07

Great, we can use the issue Wim posted in #21 as the separate issue. Yes, it's still separate though, as the first goal (the purpose of this thread) is to get a dynamic select working. Integration with hierarchical select can come later.

#24

Summit - July 3, 2008 - 19:33

Hi Quicksketch,
great if dynamic select could become working. Then perfect it integration with HS would be possible!
Thanks!
greetings,
Martijn

#25

nektir - July 3, 2008 - 21:15

subscribing

#26

quicksketch - July 4, 2008 - 22:26

Just to let users know, I have absolutely no problem with a patch that enhances the existing select.inc. Also, I have no interest in personally implementing the feature. It's going to have to come from the community.

 
 

Drupal is a registered trademark of Dries Buytaert.