You are here

function views_bulk_operations_modify_action in Views Bulk Operations (VBO) 7.3

Action function.

Goes through new values and uses them to modify the passed entity by either replacing the existing values, or appending to them (based on user input).

File

actions/modify.action.inc, line 36
VBO action to modify entity values (properties and fields).

Code

function views_bulk_operations_modify_action($entity, $context) {
  list(, , $bundle_name) = entity_extract_ids($context['entity_type'], $entity);

  // Handle Field API fields.
  if (!empty($context['selected']['bundle_' . $bundle_name])) {

    // The pseudo entity is cloned so that changes to it don't get carried
    // over to the next execution.
    $pseudo_entity = clone $context['entities'][$bundle_name];
    foreach ($context['selected']['bundle_' . $bundle_name] as $key) {

      // Get this field's language. We can just pull it from the pseudo entity
      // as it was created using field_attach_form and entity_language so it's
      // already been figured out if this field is translatable or not and
      // applied the appropriate language code to the field.
      $language = key($pseudo_entity->{$key});

      // Replace any tokens that might exist in the field columns.
      foreach ($pseudo_entity->{$key}[$language] as $delta => &$item) {
        foreach ($item as $column => $value) {
          if (is_string($value)) {
            $item[$column] = token_replace($value, array(
              $context['entity_type'] => $entity,
            ), array(
              'sanitize' => FALSE,
            ));
          }
        }
      }
      if (in_array($key, $context['append']['bundle_' . $bundle_name]) && !empty($entity->{$key})) {
        $entity->{$key}[$language] = array_merge($entity->{$key}[$language], $pseudo_entity->{$key}[$language]);

        // Check if we breached cardinality, and notify the user.
        $field_info = field_info_field($key);
        $field_count = count($entity->{$key}[$language]);
        if ($field_info['cardinality'] != FIELD_CARDINALITY_UNLIMITED && $field_count > $field_info['cardinality']) {
          $entity_label = entity_label($context['entity_type'], $entity);
          $warning = t('Tried to set !field_count values for field !field_name that supports a maximum of !cardinality.', array(
            '!field_count' => $field_count,
            '!field_name' => $field_info['field_name'],
            '!cardinality' => $field_info['cardinality'],
          ));
          drupal_set_message($warning, 'warning', FALSE);
        }

        // Prevent storing duplicate references.
        if (strpos($field_info['type'], 'reference') !== FALSE) {
          $entity->{$key}[$language] = array_unique($entity->{$key}[LANGUAGE_NONE], SORT_REGULAR);
        }
      }
      else {
        $entity->{$key}[$language] = $pseudo_entity->{$key}[$language];
      }
    }
  }

  // Handle properties.
  // Use the wrapper to set property values, since some properties need
  // additional massaging by their setter callbacks.
  // The wrapper will automatically modify $entity itself.
  $wrapper = entity_metadata_wrapper($context['entity_type'], $entity);

  // The default setting for 'revision' property (create new revisions) should
  // be respected for nodes. This requires some special treatment.
  if ($context['entity_type'] == 'node' && in_array('revision', variable_get('node_options_' . $bundle_name)) && !in_array('revision', $context['selected']['properties'])) {
    $wrapper->revision
      ->set(1);
  }
  if (!empty($context['selected']['properties'])) {
    foreach ($context['selected']['properties'] as $key) {
      if (!$wrapper->{$key}
        ->access('update')) {

        // No access.
        continue;
      }
      if (in_array($key, $context['append']['properties'])) {
        $old_values = $wrapper->{$key}
          ->value();
        $wrapper->{$key}
          ->set($context['properties'][$key]);
        $new_values = $wrapper->{$key}
          ->value();
        $all_values = array_merge($old_values, $new_values);
        $wrapper->{$key}
          ->set($all_values);
      }
      else {
        $value = $context['properties'][$key];
        if (is_string($value)) {
          $value = token_replace($value, array(
            $context['entity_type'] => $entity,
          ), array(
            'sanitize' => FALSE,
          ));
        }
        $wrapper->{$key}
          ->set($value);
      }
    }
  }
}