0

I'm using Drupal 7. I have a field called field_a which is a file-field and want to create another file-field called field_b. What I want to do is to migrate all the files from field_a to field_b, then I also want to erase the records from field_a without erasing field_a. Note that field_a and field_b are in the same content type.

Is it possible? Is there a way to do this with VBO?

3
  • "...then erase the files on "field_a" whitout erasing "field_a""... that's a weird statement. The way I'm reading it, if you erased the files associated with all field_a entries, then you'd essentially be erasing the files in the field_b entry as well after you migrated to field_b. Please clarify. May 14, 2012 at 22:08
  • Hello @LesterPeabody sorry for my bad sentence. I mean erase all the records on field_a, not the files.
    – Yaazkal
    May 14, 2012 at 22:25
  • @Yaazkal: Can you mark the answer below that I commented on as the correct answer? That fellow put a lot of work into his answer and it was just slightly missing something that I've now fixed.
    – reor
    Dec 13, 2016 at 1:39

4 Answers 4

2

The best way to do this without writing a module or messing with your database is to use views bulk operations with rules.

Here is an example of moving a file from one file field to another. You'll have to change the field names before you import this rule.

Basically add a rules component- then add your conditions and action

Variables

Datatype Node

Usage Parameter

Conditions

Entity has field - field_a

Entity has field - field_b

Actions

Create a loop to loop over multiple values in field_a.

Inside loop add item to list

Inside same loop remove item from list

Outside of loop Save your node.

{ "rules_fa_fb" : {
    "LABEL" : "Move resource files from one field to another.",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "TAGS" : [ "Dev" ],
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : { "nodes" : { "label" : "nodes", "type" : "node" } },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "nodes" ], "field" : "field_field_a" } },
      { "entity_has_field" : { "entity" : [ "nodes" ], "field" : "field_field_b" } }
    ],
    "DO" : [
      { "LOOP" : {
          "USING" : { "list" : [ "nodes:field-field-b" ] },
          "ITEM" : { "file" : "files" },
          "DO" : [
            { "list_add" : {
                "list" : [ "nodes:field-field-a" ],
                "item" : [ "file" ],
                "unique" : 1
              }
            },
            { "entity_save" : { "data" : [ "resource" ], "immediate" : "1" } },
            { "list_remove" : { "list" : [ "nodes:field-field-a" ], "item" : [ "file" ] } }
          ]
        }
      },
      { "entity_save" : { "data" : [ "nodes" ], "immediate" : 1 } }
    ]
  }
}

Then create the view with a bulk operations field, you'll need to change some values to import into your system.

$view = new view();
$view->name = 'move_files_from_on_field_to_another';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'Move files from on field to another';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Move files from on field to another';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
$handler->display->display_options['style_plugin'] = 'table';
$handler->display->display_options['style_options']['columns'] = array(
  'title' => 'title',
);
$handler->display->display_options['style_options']['default'] = '-1';
$handler->display->display_options['style_options']['info'] = array(
  'title' => array(
    'sortable' => 0,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
);
/* Field: Bulk operations: Content */
$handler->display->display_options['fields']['views_bulk_operations']['id'] = 'views_bulk_operations';
$handler->display->display_options['fields']['views_bulk_operations']['table'] = 'node';
$handler->display->display_options['fields']['views_bulk_operations']['field'] = 'views_bulk_operations';
$handler->display->display_options['fields']['views_bulk_operations']['vbo_settings']['display_type'] = '0';
$handler->display->display_options['fields']['views_bulk_operations']['vbo_settings']['enable_select_all_pages'] = 1;
$handler->display->display_options['fields']['views_bulk_operations']['vbo_settings']['force_single'] = 0;
$handler->display->display_options['fields']['views_bulk_operations']['vbo_settings']['entity_load_capacity'] = '10';
$handler->display->display_options['fields']['views_bulk_operations']['vbo_operations'] = array(
  'action::node_assign_owner_action' => array(
    'selected' => 0,
    'postpone_processing' => 0,
    'skip_confirmation' => 0,
    'override_label' => 0,
    'label' => '',
  ),
  'action::views_bulk_operations_delete_item' => array(
    'selected' => 0,
    'postpone_processing' => 0,
    'skip_confirmation' => 0,
    'override_label' => 0,
    'label' => '',
  ),
  'action::views_bulk_operations_delete_revision' => array(
    'selected' => 0,
    'postpone_processing' => 0,
    'skip_confirmation' => 0,
    'override_label' => 0,
    'label' => '',
  ),
  'action::views_bulk_operations_script_action' => array(
    'selected' => 0,
    'postpone_processing' => 0,
    'skip_confirmation' => 0,
    'override_label' => 0,
    'label' => '',
  ),
  'action::flag_node_action' => array(
    'selected' => 0,
    'postpone_processing' => 0,
    'skip_confirmation' => 0,
    'override_label' => 0,
    'label' => '',
  ),
  'action::node_make_sticky_action' => array(
    'selected' => 0,
    'postpone_processing' => 0,
    'skip_confirmation' => 0,
    'override_label' => 0,
    'label' => '',
  ),
  'action::node_make_unsticky_action' => array(
    'selected' => 0,
    'postpone_processing' => 0,
    'skip_confirmation' => 0,
    'override_label' => 0,
    'label' => '',
  ),
  'action::views_bulk_operations_modify_action' => array(
    'selected' => 0,
    'postpone_processing' => 0,
    'skip_confirmation' => 0,
    'override_label' => 0,
    'label' => '',
    'settings' => array(
      'show_all_tokens' => 1,
      'display_values' => array(
        '_all_' => '_all_',
      ),
    ),
  ),
  'rules_component::rules_fa_fb' => array(
    'selected' => 1,
    'postpone_processing' => 0,
    'skip_confirmation' => 1,
    'override_label' => 0,
    'label' => '',
  ),
  'action::views_bulk_operations_argument_selector_action' => array(
    'selected' => 0,
    'skip_confirmation' => 0,
    'override_label' => 0,
    'label' => '',
    'settings' => array(
      'url' => '',
    ),
  ),
  'action::node_promote_action' => array(
    'selected' => 0,
    'postpone_processing' => 0,
    'skip_confirmation' => 0,
    'override_label' => 0,
    'label' => '',
  ),
  'action::node_publish_action' => array(
    'selected' => 0,
    'postpone_processing' => 0,
    'skip_confirmation' => 0,
    'override_label' => 0,
    'label' => '',
  ),
  'action::node_unpromote_action' => array(
    'selected' => 0,
    'postpone_processing' => 0,
    'skip_confirmation' => 0,
    'override_label' => 0,
    'label' => '',
  ),
  'action::node_save_action' => array(
    'selected' => 0,
    'postpone_processing' => 0,
    'skip_confirmation' => 0,
    'override_label' => 0,
    'label' => '',
  ),
  'action::system_send_email_action' => array(
    'selected' => 0,
    'postpone_processing' => 0,
    'skip_confirmation' => 0,
    'override_label' => 0,
    'label' => '',
  ),
  'action::redhen_fields_email_action' => array(
    'selected' => 0,
    'postpone_processing' => 0,
    'skip_confirmation' => 0,
    'override_label' => 0,
    'label' => '',
  ),
  'action::node_unpublish_action' => array(
    'selected' => 0,
    'postpone_processing' => 0,
    'skip_confirmation' => 0,
    'override_label' => 0,
    'label' => '',
  ),
  'action::node_unpublish_by_keyword_action' => array(
    'selected' => 0,
    'postpone_processing' => 0,
    'skip_confirmation' => 0,
    'override_label' => 0,
    'label' => '',
  ),
  'action::auto_entitylabel_entity_update_action' => array(
    'selected' => 0,
    'postpone_processing' => 0,
    'skip_confirmation' => 0,
    'override_label' => 0,
    'label' => '',
  ),
  'action::pathauto_node_update_action' => array(
    'selected' => 0,
    'postpone_processing' => 0,
    'skip_confirmation' => 0,
    'override_label' => 0,
    'label' => '',
  ),
);
/* Field: Content: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['label'] = '';
$handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;
/* Field: Content: Files - for digital */
$handler->display->display_options['fields']['field_resource_file']['id'] = 'field_resource_file';
$handler->display->display_options['fields']['field_resource_file']['table'] = 'field_data_field_resource_file';
$handler->display->display_options['fields']['field_resource_file']['field'] = 'field_resource_file';
$handler->display->display_options['fields']['field_resource_file']['click_sort_column'] = 'fid';
$handler->display->display_options['fields']['field_resource_file']['delta_offset'] = '0';
/* Field: Content: Files - for print */
$handler->display->display_options['fields']['field_files_for_print']['id'] = 'field_files_for_print';
$handler->display->display_options['fields']['field_files_for_print']['table'] = 'field_data_field_files_for_print';
$handler->display->display_options['fields']['field_files_for_print']['field'] = 'field_files_for_print';
$handler->display->display_options['fields']['field_files_for_print']['click_sort_column'] = 'fid';
$handler->display->display_options['fields']['field_files_for_print']['delta_offset'] = '0';
/* Sort criterion: Content: Post date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'node';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
  'resource' => 'resource',
);

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'admin/move-files-from-on-field-to-another';
2
  • Did this work for you? It doesn't work for me and I think what is happening is that rather than copying the file it's just copying a pointer to the file and then when you delete the field_a file it deletes the file that field_b is now pointing to. If I don't delete the file then both fields end up with the same file in them. Any idea how to get past this restriction? And way to force a copy of the file?
    – reor
    Dec 12, 2016 at 3:42
  • 1
    I figured it out. I had to force a save right after copying the first file. I've edited the answer and the owner should mark this as the correct answer.
    – reor
    Dec 12, 2016 at 3:44
1

You might need to get creative on this one. Can you accomplish this by just renaming labels to your liking and create new fields? If you're comfortable making database changes you could always change the name of field_a to field_b in all of its instances (which may or may not be a nightmare given your patience and time frame).

But to my knowledge there is no quick solution to copy all of the data in one field to another without either writing a fancy Migration Migration script, or renaming fields in the database.

0

Gotta second what Johnathan wrote, there is no easy way to do this. In all honesty, I don't understand why you would want to either. It sounds like you just want to literally change the name of the field in Drupal, and unless there is a good reason to do that you're much better off just changing the label associated with the field in the content type.

If you absolutely have to change the name of the field, then identify all the instances in the database where that field name is mentioned (i.e. columns names, entries in various configuration settings, etc.) and then physically change them through whatever database administration mechanism you have at your disposal.

Before you do anything like that though, be sure to backup your database in full.

0

You can easily achieve this with Migrate 2, check this: Migrate Contents in Drupal 7 Using Migrate 2

This is the query and mapping part I wrote for you, you can try using it in your migrate class.

<?php
// Grab data from tables in source database
$query = db_select(SOURCE_DATABASE . '.node', 'n')
      ->fields('n', array('nid', 'vid', 'type', 'language', 'title', 'uid', 'status', 'created', 'changed', 'comment', 'promote', 'sticky', 'tnid', 'translate'))
      ->condition('n.type', NODE_TYPE, '=');
// Grab the field
$query->join(SOURCE_DATABASE . '.' . NODE_TYPE, 'ct', 'ct.vid = n.vid'); 
$query->addField('ct', FIELD_A);
if (module_exists('statistics')) {
  $query->join(SOURCE_DATABASE . '.node_counter', 'nc', 'n.nid = nc.nid');
  $query->addField('nc', 'totalcount');
  $query->addField('nc', 'daycount');
  $query->addField('nc', 'timestamp');
}
$query->orderBy('n.nid', 'ASC');
// Set up our destination - nodes of type migrate_example_beer
$this->destination = new MigrateDestinationNode(NODE_TYPE);

// Mapping: Assign mappings TO destination fields FROM source fields.
$this->addSimpleMappings(array('nid', 'vid', 'type', 'language', 'title', 'uid', 'status', 'created', 'changed', 'comment', 'promote', 'sticky', 'tnid', 'translate'));
$this->addFieldMapping(FIELD_B, FIELD_A);
$body_arguments = MigrateTextFieldHandler::arguments(array('source_field' => 'teaser'), array('source_field' => 'format'));
    $this->addFieldMapping('body', 'body')->arguments($body_arguments);
if (module_exists('statistics')) {
  $this->addSimpleMappings(array('totalcount', 'daycount', 'timestamp'));
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.