You are here

function vbo_search_and_replace_action in Views Bulk Operations Search & Replace 7

Same name and namespace in other branches
  1. 6 vbo_search_and_replace.module \vbo_search_and_replace_action()

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

./vbo_search_and_replace.module, line 51

Code

function vbo_search_and_replace_action($entity, $context) {
  $settings = $context['search_and_replace_settings'];
  $entity_type = $context['entity_type'];
  $entity_title = entity_label($entity_type, $entity);
  $entity_info = entity_get_info($entity_type);
  $bundle_name = $entity->{$entity_info['entity keys']['bundle']};
  $entity_label = $entity_info['bundles'][$bundle_name]['label'];
  $replacements = array(
    '%entity_title' => $entity_title,
    '@entity_label' => $entity_label,
  );
  $changed = FALSE;
  if (!empty($context['selected']['properties'])) {
    foreach ($context['selected']['properties'] as $property_name) {
      if (property_exists($entity, $property_name)) {
        $replaced = _vbo_search_and_replace_search_and_replace($settings['search'], $settings['replace'], $entity->{$property_name}, $settings);
        $replacements['%property_label'] = $property_name;
        if ($entity->{$property_name} !== $replaced) {
          $replacements['@old'] = $entity->{$property_name};
          $replacements['@new'] = $entity->{$property_name} = $replaced;
          $changed = TRUE;
          drupal_set_message(t('Property %property_label in @entity_label %entity_title changed <strong>from</strong>:<br> "@old"<br> <strong>to</strong>:<br> "@new"', $replacements));
        }
        else {
          drupal_set_message(t('Property %property_label in @entity_label %entity_title skipped', $replacements));
        }
      }
    }
  }
  if (!empty($context['selected']['bundle_' . $bundle_name])) {
    foreach ($context['selected']['bundle_' . $bundle_name] as $field_name) {
      if (property_exists($entity, $field_name)) {
        $langcode = field_language($entity_type, $entity, $field_name);
        if ($entity->{$field_name} && isset($entity->{$field_name}[$langcode])) {
          foreach ($entity->{$field_name}[$langcode] as $delta => $value) {
            $replaced = _vbo_search_and_replace_search_and_replace($settings['search'], $settings['replace'], $entity->{$field_name}[$langcode][$delta]['value'], $settings);
            $field_info = field_info_instance($entity_type, $field_name, $bundle_name);
            $replacements['%field_label'] = $field_info['label'];
            if ($entity->{$field_name}[$langcode][$delta]['value'] !== $replaced) {
              $replacements['@old'] = $entity->{$field_name}[$langcode][$delta]['value'];
              $replacements['@new'] = $entity->{$field_name}[$langcode][$delta]['value'] = $replaced;
              $changed = TRUE;
              drupal_set_message(t('Field %field_label in @entity_label %entity_title changed <strong>from</strong>:<br> "@old"<br> <strong>to</strong>:<br> "@new"', $replacements));
            }
            else {
              drupal_set_message(t('Field %field_label in @entity_label %entity_title skipped', $replacements));
            }
          }
        }
      }
    }
  }
  if ($changed) {
    entity_save($entity_type, $entity);
  }
}