added term field restoration batch script
This commit is contained in:
parent
c56a523286
commit
d1b069ad8b
@ -890,10 +890,8 @@ function materio_administration_views_default_views() {
|
||||
$handler->display->display_options['fields']['field_tags_libres']['id'] = 'field_tags_libres';
|
||||
$handler->display->display_options['fields']['field_tags_libres']['table'] = 'field_data_field_tags_libres';
|
||||
$handler->display->display_options['fields']['field_tags_libres']['field'] = 'field_tags_libres';
|
||||
$handler->display->display_options['fields']['field_tags_libres']['type'] = 'i18n_taxonomy_term_reference_plain';
|
||||
$handler->display->display_options['fields']['field_tags_libres']['delta_limit'] = '4';
|
||||
$handler->display->display_options['fields']['field_tags_libres']['delta_limit'] = '0';
|
||||
$handler->display->display_options['fields']['field_tags_libres']['delta_offset'] = '0';
|
||||
$handler->display->display_options['fields']['field_tags_libres']['multi_type'] = 'ul';
|
||||
/* Field: Content: Materiau(x) lié(s) */
|
||||
$handler->display->display_options['fields']['field_materiau_ref']['id'] = 'field_materiau_ref';
|
||||
$handler->display->display_options['fields']['field_materiau_ref']['table'] = 'field_data_field_materiau_ref';
|
||||
|
@ -1272,7 +1272,12 @@ function materio_content_types_user_default_permissions() {
|
||||
// Exported permission: 'view field_tags_libres'.
|
||||
$permissions['view field_tags_libres'] = array(
|
||||
'name' => 'view field_tags_libres',
|
||||
'roles' => array(),
|
||||
'roles' => array(
|
||||
'Admin showroom' => 'Admin showroom',
|
||||
'Translator' => 'Translator',
|
||||
'administrator' => 'administrator',
|
||||
'root' => 'root',
|
||||
),
|
||||
'module' => 'field_permissions',
|
||||
);
|
||||
|
||||
|
@ -1,10 +1,10 @@
|
||||
/**
|
||||
* @file
|
||||
* Javascript for Field Example.
|
||||
* Javascript for materio showroom field location.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Provides a farbtastic colorpicker for the fancier widget.
|
||||
* Disable draggable behaviour for the field.
|
||||
*/
|
||||
(function ($) {
|
||||
Drupal.behaviors.field_materio_showroom_location = {
|
||||
|
@ -18,6 +18,10 @@ function materio_admin_permission() {
|
||||
'title' => t('access duplicate mails list'),
|
||||
'description' => t('access duplicate mails list.'),
|
||||
),
|
||||
'materio admin fix term reference field' => array(
|
||||
'title' => t('Materio admin fix term reference field'),
|
||||
'description' => t('Materio admin fix term reference field.'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@ -34,23 +38,10 @@ function materio_admin_menu() {
|
||||
'type' => MENU_LOCAL_TASK
|
||||
);
|
||||
|
||||
|
||||
|
||||
if(module_exists('simplenews')){
|
||||
$cats = simplenews_category_list();
|
||||
// dsm($cats, 'cats');
|
||||
foreach ($cats as $tid => $name) {
|
||||
// $items['node/%node/simplenews'] = array(
|
||||
// 'title' => 'Newsletter',
|
||||
// 'type' => MENU_LOCAL_TASK,
|
||||
// 'access callback' => 'simplenews_node_tab_access',
|
||||
// 'access arguments' => array(1),
|
||||
// 'page callback' => 'simplenews_node_tab_page',
|
||||
// 'page arguments' => array(1),
|
||||
// 'context' => MENU_LOCAL_TASK,
|
||||
// 'file' => 'includes/simplenews.admin.inc',
|
||||
// 'weight' => 2,
|
||||
// );
|
||||
$items['node/add/simplenews/'.$tid] = array(
|
||||
'title' => $name,
|
||||
'title callback' => 'check_plain',
|
||||
@ -63,6 +54,23 @@ function materio_admin_menu() {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// fix term ref field lost with taxonomy
|
||||
$items['admin/config/content/materio'] = array(
|
||||
'title' => 'Materio',
|
||||
'page callback' => 'drupal_get_form',
|
||||
'page arguments' => array('materio_admin_fix_termref_fields_form'),
|
||||
'access arguments' => array('materio admin fix term reference field'),
|
||||
'type' => MENU_NORMAL_ITEM,
|
||||
);
|
||||
|
||||
$items['admin/config/content/materio/fix_termref_fields'] = array(
|
||||
'title' => 'Fix term reference field from taxonomy',
|
||||
'type' => MENU_DEFAULT_LOCAL_TASK,
|
||||
);
|
||||
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
@ -106,6 +114,272 @@ function materio_duplicatemails(){
|
||||
return $output;
|
||||
}
|
||||
|
||||
function materio_admin_fix_termref_fields_form(){
|
||||
drupal_set_title('Fix term reference fields', PASS_THROUGH);
|
||||
|
||||
$types = node_type_get_types();
|
||||
// dsm($types, 'types');
|
||||
$nt_options = array();
|
||||
foreach ($types as $mn => $type) {
|
||||
$nt_options[$mn] = $type->name;
|
||||
}
|
||||
|
||||
$node_type = variable_get('materio_admin_fix_termref_node_type', null);
|
||||
// source field (must be a text field)
|
||||
$form['node_type'] = array(
|
||||
'#type'=>'select',
|
||||
'#options'=>$nt_options,
|
||||
'#default_value' => $node_type,
|
||||
'#title' => t('Content type'),
|
||||
'#multiple' => false,
|
||||
);
|
||||
|
||||
if($node_type){
|
||||
|
||||
$fieldsmap = field_info_field_map();
|
||||
$target_options = array();
|
||||
foreach ($fieldsmap as $field_name => $field) {
|
||||
// dsm($field, $field_name);
|
||||
if ($field['type'] == 'taxonomy_term_reference'
|
||||
&& isset($field['bundles']['node'])
|
||||
&& in_array($node_type, $field['bundles']['node'])) {
|
||||
$target_options[$field_name] = $field_name;
|
||||
}
|
||||
}
|
||||
|
||||
$target_field = variable_get('materio_admin_fix_termref_target_field', null);
|
||||
// target field (must be a entity_reference field)
|
||||
$form['target_field'] = array(
|
||||
'#type'=>'select',
|
||||
'#options'=>$target_options,
|
||||
'#default_value' => $target_field,
|
||||
'#title' => t('target field (must be a term_reference field)'),
|
||||
'#multiple' => false,
|
||||
);
|
||||
|
||||
if($target_field){
|
||||
// retrive various field infos
|
||||
$field_info = field_info_field($target_field);
|
||||
// dsm($field_info, 'field_info');
|
||||
// $field_instance = field_info_instance('node', $target_field, $node_type);
|
||||
// dsm($field_instance, 'field_instance');
|
||||
|
||||
$voc_name = $field_info['settings']['allowed_values'][0]['vocabulary'];
|
||||
$vid = taxonomy_vocabulary_machine_name_load($voc_name)->vid;
|
||||
$voc = taxonomy_vocabulary_load($vid);
|
||||
$tree = taxonomy_get_tree($vid);
|
||||
// dsm($tree, 'tree');
|
||||
foreach ($tree as $key => $term) {
|
||||
$terms[$term->tid] = $term->name;
|
||||
}
|
||||
|
||||
|
||||
$src_term = variable_get('materio_admin_fix_termref_src_term', null);
|
||||
// src term
|
||||
$form['src_term'] = array(
|
||||
'#type'=>'select',
|
||||
'#options'=>$terms,
|
||||
'#default_value' => $src_term,
|
||||
'#title' => t('source term'),
|
||||
'#multiple' => false,
|
||||
);
|
||||
|
||||
if($src_term){
|
||||
|
||||
$form['txt_nids'] = array(
|
||||
'#type'=>'textarea',
|
||||
'#title' => t('nids to fix'),
|
||||
'#description' => t('comma separated nids list'),
|
||||
);
|
||||
|
||||
|
||||
$form['fixterms'] = array(
|
||||
'#type' => 'submit',
|
||||
'#value' => 'Fixterms',
|
||||
);
|
||||
|
||||
// $nodes = taxonomy_select_nodes($src_term, false);
|
||||
// dsm($nodes, 'nodes');
|
||||
// $src_items = field_get_items('node', node_load(11858), $target_field);
|
||||
// dsm($src_items);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
$form['submit'] = array(
|
||||
'#type' => 'submit',
|
||||
'#value' => 'Save',
|
||||
// '#submit' => array('materio_showroom_migrate_location_fields_migrate'),
|
||||
);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
function materio_admin_fix_termref_fields_form_submit($form, &$form_state){
|
||||
// dsm($form_state, 'form_state');
|
||||
|
||||
$node_type = $form_state['values']['node_type']; $context['results']['field_migrated']++;
|
||||
$target_field = $form_state['values']['target_field'];
|
||||
$src_term = $form_state['values']['src_term'];
|
||||
|
||||
variable_set('materio_admin_fix_termref_node_type', $node_type);
|
||||
variable_set('materio_admin_fix_termref_target_field', $target_field);
|
||||
variable_set('materio_admin_fix_termref_src_term', $src_term);
|
||||
|
||||
$nids = explode(',', $form_state['values']['txt_nids']);
|
||||
|
||||
if ($form_state['values']['op'] == 'Fixterms' && !empty($nids)){
|
||||
_materio_admin_batch_fixterm_ref($node_type, $target_field, $src_term, $nids);
|
||||
}
|
||||
}
|
||||
|
||||
function _materio_admin_batch_fixterm_ref($node_type, $target_field, $src_tid, $nids){
|
||||
// Reset counter for debug information.
|
||||
$_SESSION['http_request_count'] = 0;
|
||||
|
||||
$batch = array(
|
||||
'title' => t('Fixing ref terms ...'),
|
||||
'operations' => array(),
|
||||
'init_message' => t('Commencing'),
|
||||
'progress_message' => t('Processed @current out of @total.'),
|
||||
'error_message' => t('An error occurred during processing'),
|
||||
'finished' => '_materio_admin_batch_fixterm_ref_finished',
|
||||
);
|
||||
|
||||
foreach ($nids as $nid) {
|
||||
$nid = trim($nid);
|
||||
$batch['operations'][] = array(
|
||||
'_materio_admin_batch_fixterm_ref_op',
|
||||
array(
|
||||
$nid,
|
||||
$src_tid, $target_field,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// $nodes = taxonomy_select_nodes($src_tid);
|
||||
|
||||
// $k = 0;
|
||||
// foreach ($nodes as $nid) {
|
||||
// $batch['operations'][] = array(
|
||||
// '_materio_admin_batch_fixterm_ref_op',
|
||||
// array(
|
||||
// $nid,
|
||||
// $src_tid, $target_field,
|
||||
// $node_type,
|
||||
// )
|
||||
// );
|
||||
// $k++;
|
||||
// if ($k > 0) {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
|
||||
batch_set($batch);
|
||||
}
|
||||
|
||||
function _materio_admin_batch_fixterm_ref_op($nid, $src_tid, $target_field){
|
||||
$context['results']['field_migrated']++;
|
||||
|
||||
$node = node_load($nid);
|
||||
// if($node->type == $node_type){
|
||||
$items = field_get_items('node', $node, $target_field);
|
||||
foreach ($items as $key => $item) {
|
||||
$flat_items[] = $item['tid'];
|
||||
}
|
||||
if(!in_array($src_tid, $flat_items)){
|
||||
// $items[] = array('tid'=>$src_tid);
|
||||
$node->$target_field[LANGUAGE_NONE][] = array('tid'=>$src_tid);
|
||||
}
|
||||
|
||||
// $items = array(LANGUAGE_NONE => array());
|
||||
// foreach ($terms as $tid => $name) {
|
||||
// $items[LANGUAGE_NONE][] = array(
|
||||
// 'showroom_tid' => $tid,
|
||||
// 'location' => $default_tid == $tid ? $src_value : '',
|
||||
// );
|
||||
// }
|
||||
//
|
||||
|
||||
node_save($node);
|
||||
|
||||
// }
|
||||
|
||||
//Simply show the import row count.
|
||||
$context['message'] = t('Migrating node !c : %title (%nid)', array(
|
||||
'!c' => $context['results']['field_migrated'],
|
||||
'%title'=>$node->title,
|
||||
'%nid'=>$nid ));
|
||||
|
||||
// In order to slow importing and debug better,
|
||||
// we can uncomment this line to make each import slightly slower.
|
||||
// usleep(2500);
|
||||
|
||||
if ( false ) {
|
||||
$context['results']['failed_nodes'][] = $nid ;
|
||||
}
|
||||
|
||||
_materio_admin_update_http_requests();
|
||||
|
||||
}
|
||||
|
||||
function _materio_admin_batch_fixterm_ref_finished($success, $results, $operations){
|
||||
// dsm($success, 'success');
|
||||
// dsm($results, 'results');
|
||||
// dsm($operations, 'operations');
|
||||
if ($success) {
|
||||
// Here we could do something meaningful with the results.
|
||||
// We just display the number of nodes we processed...
|
||||
drupal_set_message(t('@count results processed in @requests HTTP requests.', array('@count' => count($results), '@requests' => _materio_admin_get_http_requests())));
|
||||
drupal_set_message(t('The final result was "%final"', array('%final' => end($results))));
|
||||
}
|
||||
else {
|
||||
// An error occurred.
|
||||
// $operations contains the operations that remained unprocessed.
|
||||
drupal_set_message(
|
||||
t('operations : @args',
|
||||
array(
|
||||
'@args' => print_r(current($operations), TRUE),
|
||||
)
|
||||
),
|
||||
'error'
|
||||
);
|
||||
$error_operation = reset($operations);
|
||||
drupal_set_message(
|
||||
t('An error occurred while processing @operation with arguments : @args',
|
||||
array(
|
||||
'@operation' => $error_operation[0],
|
||||
'@args' => print_r($error_operation[0], TRUE),
|
||||
)
|
||||
),
|
||||
'error'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Utility function to increment HTTP requests in a session variable.
|
||||
*/
|
||||
function _materio_admin_update_http_requests() {
|
||||
$_SESSION['http_request_count']++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility function to count the HTTP requests in a session variable.
|
||||
*
|
||||
* @return int
|
||||
* Number of requests.
|
||||
*/
|
||||
function _materio_admin_get_http_requests() {
|
||||
return !empty($_SESSION['http_request_count']) ? $_SESSION['http_request_count'] : 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Implements hook_menu_alter().
|
||||
*/
|
||||
|
@ -449,22 +449,14 @@ function materio_showroom_menu_local_tasks_alter(&$data, $router_item, $root_pat
|
||||
*/
|
||||
function materio_showroom_menu() {
|
||||
|
||||
$items['admin/config/content/materio_showroom'] = array(
|
||||
$items['admin/config/content/materio/migrate_showroom'] = array(
|
||||
'title' => 'Showrooms location fields migration settings',
|
||||
'page callback' => 'drupal_get_form',
|
||||
'page arguments' => array('materio_showroom_migrate_location_fields_settings_form'),
|
||||
'access arguments' => array('materio showroom migrate fields'),
|
||||
'type' => MENU_LOCAL_ACTION,
|
||||
'type' => MENU_LOCAL_TASK,
|
||||
// 'file' => ,
|
||||
);
|
||||
// $items['admin/config/content/materio_showroom/migrate'] = array(
|
||||
// 'title' => 'Migrate showrooms location fields',
|
||||
// 'page callback' => 'materio_showroom_migrate_location_fields',
|
||||
// // 'page arguments' => array('materio_showroom_migrate_location_fields'),
|
||||
// 'access arguments' => array('materio showroom migrate fields'),
|
||||
// 'type' => MENU_LOCAL_TASK,
|
||||
// // 'file' => ,
|
||||
// );
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user