Localizer Language Field

aerodog - May 10, 2008 - 16:15
Project:Node import
Version:5.x-1.6
Component:Miscellaneous
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active
Description

Greetings! Thanks for this module, I really like it and it's worlds ahead of the import/export api in terms of simplicity imho...

I wanted to know whether you could also include some functionality that incorporates the localizer module. For example, something that could assign a language to all the imported nodes (via function localizer_node_import_global in a newly-made localizer.inc)...

This would be of great benefit to those of us importing content on multi-language sites.

Thank you!

#1

aerodog - May 11, 2008 - 20:10

Alright here's some progress. Now, I have the cute little field that lets me choose a language within the node import wizard (after fields are matched, during the stage where global values are set). I did this with the code below, which I included as a file named localizer.inc, and placed within modules/node_import/supported. What is left is for me to actually instantiate the respective nodes as localizer nodes based on the selected language, creating entries in the localizernode table for future reference. I'm not sure how to do this part. Any help would be greatly appreciated! I'm trying to figure out where in node_import I can even take care of this, since I'm new to forms, and sort of new to drupal in general.

------------

<?php
function localizer_node_import_global($type, $global_values) {
 
$form = array();
 
 
$form['node_import_localizer'] = array(
 
'#type' => 'fieldset',
 
'#title' => t('Localizer'),
 
'#weight' => 0,
 
'#tree' => TRUE,
  );
 
$form['node_import_localizer']['definition_language'] = array(
   
'#type' => 'select',
   
'#title' => t('Definition Languages'),
   
'#description' => t('Select the language of the dictionary about to be imported.'),
   
'#default_value' => localizer_get_default_language(),
   
'#options' => localizer_supported_languages(),
   
'#tree' => TRUE,
  );
 
  return
$form;
}
?>

#2

Robrecht Jacques - May 12, 2008 - 14:56

Adding support for localizer would need some knowledge of the localizer module itself. In general, but I haven't checked this for localizer, the module will add some field to the $node object before the node is saved. This field has some format (probably a language code).

So how to add this to node_import? The key is hook_node_import_prepare($type, &$node, ...). The $node that is passed here will be an object with all the fields that are mapped included. In your case above, you do not allow the localizer-field (language) to be mapped, to do so you would need to define hook_node_import_fields($type) (see below), but the object will contain one field: $node->node_import_localizer['definition_language']. This will contain the selected language in the global options page of the wizard.

Now, what you need to do is to save this somewhere in the $node object so the localizer module acts correctly. Probably something like:

<?php
function localizer_node_import_prepare($type, &$node, ...) {
  if (isset(
$node->node_import_localizer)) {
   
$node->language = $node->node_import_localizer['definition_language'];
  }
}
?>

I didn't check out the code of localizer module so this $node->language could be something else. If you can't find what to set, let me know, I'll dive into localizer module myself then.

Now you only implemented hook_node_import_global($type, $global_values) but it probably is useful to implement hook_node_import_fields($type) too. This allows users to map a column to the localizer language. Something like:

<?php
function localizer_node_import_fields($type) {
  return array(
   
'node_import_localizer_mapped' => t('Localizer: language code'),
  );
}
?>

Now you will be able to match a column to a language. If the user does this the $node object in hook_node_import_prepare() will have $node->node_import_localizer_mapped. So in the _prepare hook you would need to handle both cases:
<?php
function localizer_node_import_prepare($type, &$node, ...) {
 
$langs = localizer_supported_languages();
  if (isset(
$node->node_import_localizer_mapped) && isset($langs[$node->node_import_localizer_mapped])) {
   
$node->language = $node->node_import_localizer_mapped;
  }
  else if (isset(
$node->node_import_localizer)) {
   
$node->language = $node->node_import_localizer['definition_language'];
  }
}
?>

The first part checks whether $node->node_import_localizer_mapped is set (which means the user mapped a column) and then if that language is one of the languages localizer understands. If so, the mapped value is used. If not, the global option value is used.

If you need more help, let me know.

#3

aerodog - May 12, 2008 - 22:36

Holy smokes you must have MADE the node import module! So appreciative Robrecht, that was wonderful!

To sum that up: if you want to import Localizer's node language field either as a column in your csv or tsv file, or as a value that will be globally applied to all imported nodes, do the following:

1) create a file called localizer.inc within modules/node_import/supported
2) copy the code below into localizer.inc

<?php
function localizer_node_import_fields($type) {
  return array(
   
'node_import_localizer_mapped' => t('Localizer: language code'),
  );
}

function
localizer_node_import_prepare($node, $preview = FALSE) {
 
$langs = localizer_supported_languages();
  if (isset(
$node->node_import_localizer_mapped) && isset($langs[$node->node_import_localizer_mapped])) {
   
$node->language = $node->node_import_localizer_mapped;
  }
  else if (isset(
$node->node_import_localizer)) {
   
$node->language = $node->node_import_localizer['node_language'];
  }
}

function
localizer_node_import_global($type, $global_values) {
 
$form = array();
 
 
$form['node_import_localizer'] = array(
 
'#type' => 'fieldset',
 
'#title' => t('Localizer'),
 
'#weight' => 0,
 
'#tree' => TRUE,
  );
 
$form['node_import_localizer']['node_language'] = array(
   
'#type' => 'select',
   
'#title' => t('Node Languages'),
   
'#description' => t('Select the language of the nodes about to be imported.'),
   
'#default_value' => localizer_get_default_language(),
   
'#options' => localizer_supported_languages(),
   
'#tree' => TRUE,
  );
 
  return
$form;
}
?>

Thanks again Robrecht!

#4

JordiTR - May 12, 2009 - 08:56

#5

sandroz - July 3, 2009 - 08:30

Hi,

Is this usable for the actual Version too?

Best Regards
Sandro

#6

sandroz - July 3, 2009 - 09:21
 
 

Drupal is a registered trademark of Dries Buytaert.