I'm writing a module similar to features that exports configurations.
One issue that I've run into is when you initially export the body field of a node, some values in the field instance array will return as booleans. Once you go to the field_ui_field_settings_form and save that form (without doing anything) any values that used to be a boolean is now an integer of 1 or 0.
This is an issue if you export the field instance arrays and expect to have identical results. I've pasted below a before and after picture of what the same body field instance looks like after only hitting submit on field settings form.
Also, if you diff the two arrays below, you'll see not only are the booleans now integers, but a new setting was added to widget "active" which was just added only by hitting the save button on the field settings form.
var_export on array field_info_instance('node', 'body', 'page')
array(
'label' => 'Body',
'widget' => array(
'type' => 'text_textarea_with_summary',
'settings' => array(
'rows' => 20,
'summary_rows' => 5,
),
'weight' => -4,
'module' => 'text',
),
'settings' => array(
'display_summary' => TRUE,
'text_processing' => 1,
'user_register_form' => FALSE,
),
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'text_default',
'settings' => array(),
'module' => 'text',
'weight' => 0,
),
'teaser' => array(
'label' => 'hidden',
'type' => 'text_summary_or_trimmed',
'settings' => array(
'trim_length' => 600,
),
'module' => 'text',
'weight' => 0,
),
),
'required' => FALSE,
'description' => '',
'id' => 2,
'field_id' => 2,
'field_name' => 'body',
'entity_type' => 'node',
'bundle' => 'page',
'deleted' => 0,
'default_value' => NULL,
)var_export on array after changing nothing and hitting save
array(
'label' => 'Body',
'widget' => array(
'weight' => -4,
'type' => 'text_textarea_with_summary',
'module' => 'text',
'active' => 1,
'settings' => array(
'rows' => 20,
'summary_rows' => 5,
),
),
'settings' => array(
'text_processing' => 1,
'display_summary' => 1,
'user_register_form' => FALSE,
),
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'text_default',
'settings' => array(),
'module' => 'text',
'weight' => 0,
),
'teaser' => array(
'label' => 'hidden',
'type' => 'text_summary_or_trimmed',
'settings' => array(
'trim_length' => 600,
),
'module' => 'text',
'weight' => 0,
),
),
'required' => 0,
'description' => '',
'default_value' => NULL,
'id' => 2,
'field_id' => 2,
'field_name' => 'body',
'entity_type' => 'node',
'bundle' => 'page',
'deleted' => 0,
)Here is the export function I'm using to export arrays. It's pretty much the same as the features version, with the exception that I have added ensuring that numbers stay numeric and do not turn into strings.
/**
* Export var function -- from Views.
*/
function configuration_var_export($var, $prefix = '', $init = TRUE) {
if (is_object($var)) {
$output = method_exists($var, 'export') ? $var->export() : configuration_var_export((array) $var);
}
else if (is_array($var)) {
if (empty($var)) {
$output = 'array()';
}
else {
$output = "array(\n";
foreach ($var as $key => $value) {
// Using normal var_export on the key to ensure correct quoting.
$output .= " " . var_export($key, TRUE) . " => " . configuration_var_export($value, ' ', FALSE) . ",\n";
}
$output .= ')';
}
}
else if (is_bool($var)) {
$output = $var ? 'TRUE' : 'FALSE';
}
else if (is_string($var) && strpos($var, "\n") !== FALSE) {
// Replace line breaks in strings with a token for replacement
// at the very end. This protects whitespace in strings from
// unintentional indentation.
$var = str_replace("\n", "***BREAK***", $var);
$output = var_export($var, TRUE);
}
else if (is_numeric($var)) {
$output = is_int($var) ? var_export((int) $var, TRUE) : var_export((float) $var, TRUE);
}
else {
$output = var_export($var, TRUE);
}
if ($prefix) {
$output = str_replace("\n", "\n$prefix", $output);
}
if ($init) {
$output = str_replace("***BREAK***", "\n", $output);
}
return $output;
}
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | 1382172-3-field-booleans-to-integers.patch | 1.14 KB | tom friedhof |
| #3 | 1382172-2-field-booleans-to-integers.patch | 1.5 KB | tom friedhof |
| #1 | 1382172-field-booleans-to-integers.patch | 1.5 KB | tom friedhof |
Comments
Comment #1
tom friedhof commentedSince all boolean values get converted to integers in field_ui after modifying, maybe they should start off as integers.
Here is a patch that fixes the specific issue I ran into with the body field, and also a small modification to field_ui to set the required field default to 0 rather than FALSE
Comment #3
tom friedhof commentedHere is the patch again against latest in the 7.x branch.
Comment #4
tom friedhof commentedComment #6
tom friedhof commentedUploading the patch again without --no-prefix
Comment #7
tom friedhof commentedComment #8.0
(not verified) commentedclarifying