You are here

function views_bulk_operations_fields_action in Views Bulk Operations (VBO) 6

Same name and namespace in other branches
  1. 6.3 fields.action.inc \views_bulk_operations_fields_action()

Action function.

1 call to views_bulk_operations_fields_action()
views_bulk_operations_fields_rules_action in ./views_bulk_operations.rules.inc

File

actions/fields.action.inc, line 39
Drupal action to set individual field values.

Code

function views_bulk_operations_fields_action(&$node, $context) {
  if (empty($context['#field_info'])) {

    // called directly: recreate the field info from the passed node
    $fields = _views_bulk_operations_fields_action_non_cck($node->type);
    $type_info = content_types($node->type);
    $fields += $type_info['fields'];
    foreach ($fields as $field_name => $field) {
      if (array_key_exists($field_name, $context)) {
        $context[$field_name . '_check'] = TRUE;
      }
    }
    $context['#field_info'] = $fields;
  }
  foreach ($context['#field_info'] as $field_name => $field) {
    if (empty($context[$field_name . '_check'])) {
      continue;
    }

    // Get the value, either by PHP evaluation or literally.
    if (!empty($context[$field_name . '_code'])) {
      $value = eval('?>' . $context[$field_name . '_code']);
    }
    else {
      $value = $context[$field_name];
    }

    // Set the value.
    if ($field['type'] == 'non_cck') {

      // our hacked definition
      if (is_array($value)) {
        foreach ($value as $v_key => $v_item) {
          $node->{$v_key} = _views_bulk_operations_fields_action_token($v_item, $node, $field);
        }
      }
      else {
        $node->{$field_name} = _views_bulk_operations_fields_action_token($value, $node, $field);
      }
      if (is_array($field['submit'])) {
        foreach ($field['submit'] as $function) {
          if (!function_exists($function)) {
            continue;
          }
          $function($node, $value, $field);
        }
      }
    }
    else {

      // CCK
      if (empty($context[$field_name . '_add'])) {
        if (is_array($value)) {
          $node->{$field_name} = _views_bulk_operations_fields_action_token($value, $node, $field);
        }
        else {

          // We're given just one value: we can manage if the field has a single column
          // or there's a column called 'value'.
          $columns = array_keys($field['columns']);
          $column = FALSE;
          if (count($columns) == 1) {
            $column = $columns[0];
          }
          else {
            if (in_array('value', $columns)) {
              $column = 'value';
            }
          }
          if ($column !== FALSE) {
            $node->{$field_name} = array(
              array(
                $column => _views_bulk_operations_fields_action_token($value, $node, $field),
              ),
            );
          }
          else {
            watchdog('vbo', 'Failed to set a scalar value on multi-column field %field.', array(
              '%field' => $field_name,
            ), WATCHDOG_ERROR);
          }
        }
      }
      else {
        if (is_array($value)) {

          // Adding to existing values: do it one by one.
          foreach ($value as $v_delta => $v_item) {
            if (is_string($v_delta)) {
              $node->{$field_name}[$v_delta] = _views_bulk_operations_fields_action_token($v_item, $node, $field, $v_delta);
            }
            else {
              $node->{$field_name}[] = _views_bulk_operations_fields_action_token($v_item, $node, $field, $v_delta);
            }
          }
        }
      }
    }
  }
}