Add a Feeds Tamper plugin that will convert a text value to a boolean (from a list of multiple allowed values), with a default value.
This commit is contained in:
parent
02685fa3c2
commit
21bb2f0bff
|
@ -18,6 +18,15 @@ function farm_import_ctools_plugin_api($module = NULL, $api = NULL) {
|
||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements hook_ctools_plugin_directory().
|
||||||
|
*/
|
||||||
|
function farm_import_ctools_plugin_directory($owner, $plugin_type) {
|
||||||
|
if ($owner == 'feeds_tamper' && $plugin_type == 'plugins') {
|
||||||
|
return 'includes/feeds_tamper/plugins/';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements hook_farm_access_perms().
|
* Implements hook_farm_access_perms().
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file
|
||||||
|
* Feeds Tamper plugin for converting strings to booleans with a default value.
|
||||||
|
*/
|
||||||
|
|
||||||
|
$plugin = array(
|
||||||
|
'form' => 'farm_import_boolean_default_form',
|
||||||
|
'callback' => 'farm_import_boolean_default_callback',
|
||||||
|
'name' => 'Convert to boolean (with default)',
|
||||||
|
'multi' => 'direct',
|
||||||
|
'category' => 'Text',
|
||||||
|
);
|
||||||
|
|
||||||
|
function farm_import_boolean_default_form($importer, $element_key, $settings) {
|
||||||
|
$form = array();
|
||||||
|
$form['default_value'] = array(
|
||||||
|
'#type' => 'radios',
|
||||||
|
'#title' => t('Default value'),
|
||||||
|
'#options' => array(
|
||||||
|
0 => 'False',
|
||||||
|
1 => 'True',
|
||||||
|
),
|
||||||
|
'#default_value' => !empty($settings['default_value']) ? 1 : 0,
|
||||||
|
);
|
||||||
|
return $form;
|
||||||
|
}
|
||||||
|
|
||||||
|
function farm_import_boolean_default_callback($result, $item_key, $element_key, &$field, $settings, $source) {
|
||||||
|
|
||||||
|
// Get the value.
|
||||||
|
$value = $field;
|
||||||
|
|
||||||
|
// Get the default value (default to TRUE).
|
||||||
|
$default_value = isset($settings['default_value']) ? $settings['default_value'] : TRUE;
|
||||||
|
|
||||||
|
// Convert to lowercase.
|
||||||
|
$value = drupal_strtolower($value);
|
||||||
|
|
||||||
|
// Define allowed true/false values.
|
||||||
|
$allowed_values = array(
|
||||||
|
0 => array(
|
||||||
|
'false',
|
||||||
|
'f',
|
||||||
|
'no',
|
||||||
|
'n',
|
||||||
|
'0',
|
||||||
|
),
|
||||||
|
1 => array(
|
||||||
|
'true',
|
||||||
|
't',
|
||||||
|
'yes',
|
||||||
|
'y',
|
||||||
|
'1',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Iterate through allowed values to see if a match is found.
|
||||||
|
foreach ($allowed_values as $boolean => $options) {
|
||||||
|
foreach ($options as $option) {
|
||||||
|
if ($value == $option) {
|
||||||
|
$field = $boolean;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If a match was not found, use the default value.
|
||||||
|
$field = $default_value;
|
||||||
|
}
|
Loading…
Reference in New Issue