function views_bulk_operations_fields_action_form in Views Bulk Operations (VBO) 6.3
Same name and namespace in other branches
- 6 actions/fields.action.inc \views_bulk_operations_fields_action_form()
File
- ./
fields.action.inc, line 28 - Drupal action to set individual field values.
Code
function views_bulk_operations_fields_action_form($context) {
module_load_include('inc', 'content', 'includes/content.node_form');
// This action form uses static-time settings. If they were not set, pull the defaults now.
if (!isset($context['settings'])) {
$context['settings'] = views_bulk_operations_fields_action_views_bulk_operations_form_options();
}
$form['#settings'] = $context['settings'];
$form['#theme'] = 'views_bulk_operations_fields_action_form';
$form['#node'] = (object) array(
'type' => NULL,
);
$form_state = array();
$weight = -100;
if (isset($context['selection']) && isset($context['view'])) {
$nids = array_map('_views_bulk_operations_get_oid', $context['selection'], array_fill(0, count($context['selection']), $context['view']->base_field));
$result = db_query("SELECT DISTINCT type FROM {node} WHERE nid IN (%s)", implode(',', $nids));
}
else {
$result = db_query("SELECT type from {node_type}");
}
$fields = array();
while ($type = db_result($result)) {
$type_info = content_types($type);
$fields += $type_info['fields'];
//$fields += _views_bulk_operations_fields_action_non_cck($type);
}
if (empty($fields)) {
form_set_error('', t('The selected nodes do not have any editable fields. Please select other nodes and try again.'));
return array();
}
foreach ($fields as $field) {
if (module_exists('content_permissions') && in_array('edit ' . $field['field_name'], module_invoke('content_permissions', 'perm')) && !user_access('edit ' . $field['field_name'])) {
continue;
}
if (!empty($context['settings']['display_fields']) && !in_array($field['field_name'], $context['settings']['display_fields']) && !in_array(VBO_ACTION_FIELDS_ALL, $context['settings']['display_fields'])) {
continue;
}
$form['#node']->{$field['field_name']} = NULL;
// The field info and widget.
if (isset($field['field_form'])) {
// is it our hacked definition?
$form += (array) $field['field_form'];
}
else {
// no, it's CCK
$field['required'] = FALSE;
$form += (array) content_field_form($form, $form_state, $field);
}
if (!isset($form[$field['field_name']])) {
continue;
}
$form['#field_info'][$field['field_name']] = $field;
// Adjust some settings on the field itself.
$form[$field['field_name']]['#weight'] = $weight++;
$form[$field['field_name']]['#prefix'] = '<div class="fields-action-togglable">' . @$form[$field['field_name']]['#prefix'];
$form[$field['field_name']]['#suffix'] = @$form[$field['field_name']]['#suffix'] . '</div>';
// Checkbox to enable/disable this field.
$form[$field['field_name'] . '_check'] = array(
'#type' => 'checkbox',
'#attributes' => array(
'class' => 'fields-action-toggler',
),
);
// Checkbox to add/overwrite values in this field.
if ($field['multiple']) {
$form[$field['field_name'] . '_add'] = array(
'#type' => 'checkbox',
'#prefix' => '<div class="fields-action-togglable">',
'#suffix' => '</div>',
'#attributes' => array(
'title' => t('Check this box to add the new values to existing ones, instead of overwriting them.'),
),
);
}
else {
$form[$field['field_name'] . '_add'] = array(
'#type' => 'value',
'#value' => FALSE,
);
}
// Default value.
if (@$context[$field['field_name'] . '_check']) {
$form[$field['field_name']]['#default_value'] = $context[$field['field_name']];
$form[$field['field_name'] . '_check']['#default_value'] = TRUE;
}
// PHP code to program the value.
if (user_access('Use PHP input for field settings (dangerous - grant with care)') && $context['settings']['php_code']) {
if (isset($field['field_form'])) {
$sample = t('return value;');
}
else {
$db_info = content_database_info($field);
$columns = array_keys($db_info['columns']);
foreach ($columns as $key => $column) {
$columns[$key] = t("'@column' => value for @column", array(
'@column' => $column,
));
}
$sample = t("return array(\n" . " 0 => array(\n @columns\n ),\n" . " // You'll usually want to stop here. Provide more values\n" . " // if you want your 'default value' to be multi-valued:\n" . " 1 => array(\n @columns\n ),\n" . " 2 => ...\n);", array(
'@columns' => implode(",\n ", $columns),
));
}
$form[$field['field_name'] . '_code'] = array(
'#type' => 'textarea',
'#default_value' => '',
'#rows' => 6,
'#description' => t('Expected format: <pre>!sample</pre>', array(
'!sample' => $sample,
)),
'#prefix' => '<div class="fields-action-togglable">',
'#suffix' => '</div>',
);
}
}
// Special case for only one field: convert the checkbox into a value that's TRUE by default.
if (count($form['#field_info']) == 1) {
$field_name = key($form['#field_info']);
$form[$field_name . '_check'] = array(
'#type' => 'value',
'#value' => TRUE,
);
}
return $form;
}