I love feed_tamper and would like to make a plugin that converts textual 'true'/'false' to real bool true/false, however my first attempts are not going very well, my plugin callback does not seem to be called at all, do you have any idea why?
The code is as follows (in /plugins/convert_boolean.inc):
<?php
$plugin = array(
'form' => 'feeds_tamper_convert_boolean_form',
'callback' => 'feeds_tamper_convert_boolean_callback',
'name' => 'Convert boolean',
'multi' => 'loop',
'category' => 'Text',
'description' => 'feeds_tamper_convert_boolean_description',
);
function feeds_tamper_convert_boolean_form($importer, $element_key, $settings) {
$form = array();
$form['convert_boolean']['#markup'] = t('Convert textual boolean "true" to true and "false" to false');
return $form;
}
function feeds_tamper_convert_boolean_description($settings) {
$text = t('Convert textual boolean "true" to true and "false" to false');
return $text;
}
function feeds_tamper_convert_boolean_callback($result, $item_key, $element_key, &$field, $settings) {
if(0 != preg_match('/^true$/i',$field))
$field = true;
else
$field = false;
}
The original reason for wanting the plugin came about when i was trying to import XML feeds, and having issue accepting that boolean data needs to be imported as text in order to preserve the data value.
E.g. given the XML element <some-element type="boolean">false</some-element> Directly reading the feed kept resulting in a value of 'true' (due to PHP type conversion rules i think).
Any help or advice anyone can offer on this would be greatly appreciated.
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | convert_boolean.zip updated 4/3/2012 | 961 bytes | miltonsp |
Comments
Comment #1
PartTimer commentedHave you tried mapping 'true' => 1, 'false' => 0? At least in the select box issue posted elsewhere this works for me.
Comment #2
twistor commentedI like this idea. Here's a 2 minute sketch of what this might look like.
The thing it really needs is a 'FileNotFound' value.
Comment #3
iccle commentedThis looks ideal, and keen to test it i swapped out the code i have written and gave it a shot.
It still looks like the callback is never being called, i added a watchdog entry at the start of the function (feeds_tamper_convert_boolean_validate) and it never shows up, i cleared my logs and tried again and the only error/warning that appeared was the following:
There are two issues i would like to check here:
Comment #4
twistor commentedThis has been added.
http://drupalcode.org/project/feeds_tamper.git/commit/db60049
Comment #6
miltonsp commentedThe 'Convert to boolean' plug-in does not work out-of-the-box in Drupal 7.
Specifically, during an import to a node, a boolean field was not being set.
Here is a small patch to make it work.
Comment #7
twistor commentedCommitted to 7.x
http://drupalcode.org/project/feeds_tamper.git/commit/0e42495
I'm somewhat confused by this, but not that concerned since 1 and 0 will work.
Does this need to be ported to 6.x?