импорт с помощью feeds

Главные вкладки

Комментарии

Аватар пользователя validoll validoll 21 января 2012 в 21:39

Если есть опыт, то можно через свой модуль.
Смотрите в сторону hook_feeds_node_processor_targets_alter()
Через этот хук можно переопределить обработчик поля, в котором можно делать со значением что хочешь.
Например:

<?php
/**
* Implementation of hook_feeds_node_processor_targets_alter().
*/
function YOUR_MODULE_feeds_processor_targets_alter(&$targets$entity_type$content_type) {
    
$numeric_types = array(
        
'list_integer',
        
'list_float',
        
'list_boolean',
        
'number_integer',
        
'number_decimal',
        
'number_float',
    );
    
$string_types = array(
        
'list_text',
        
'text',
        
'text_long',
        
'text_with_summary',
    );
    
$taxonomy_types = array(
         
'taxonomy_term_reference',
    );

    if (

$entity_type == 'node') {
            foreach (
field_info_instances($entity_type$content_type) as $name => $instance) {
              
$info field_info_field($name);
              unset(
$callback);
              
                if (
in_array($info['type'], $numeric_types)) {
                  
$callback 'field_feeds_set_target_numeric';
                }
                if (
in_array($info['type'], $string_types)) {
                  
$callback 'field_feeds_set_target_text';
                }
                if (
in_array($info['type'], $taxonomy_types)) {
                    
$callback 'taxonomy_import_set_target';
                }

                if (isset(

$callback)) {
                  
$targets[$name] = array(
                    
'name' => check_plain($instance['label']),
                    
'callback' => $callback,
                    
'description' => t('The label field of the node.', array('label=> $instance['label'])),
                  );
                }
          }
    }
}
?>

В предыдущем коде мы переопределили обработчик для полей таксономии. Теперь описываем этот обработчик, где разделяем несколько значений в одном поле импорта. За это отвечает кусок:

<?php    // Handle non-multiple values.
    
if (!is_array($terms)) {
        
$terms explode(';',$terms);
    }
?>

А вот пример обработчика:

<?php
function taxonomy_import_set_target($source$entity$target$terms) {
   if (empty(
$terms)) {
     return;
   }
    
// Handle non-multiple values.
    
if (!is_array($terms)) {
        
$terms explode(';',$terms);
    }
    
$vid NULL;
    
// See http://drupal.org/node/881530
    
foreach (field_info_instances($entity->feeds_item->entity_type$entity->type) as $name => $instance) {
        
$info field_info_field($name);
        if (
$info['type'] == 'taxonomy_term_reference' && $name == $target) {
            
$vid=$info['settings']['allowed_values'][0]['vocabulary'];
        }
    }
    if (
$vid) {
        
$vocabulary taxonomy_vocabulary_machine_name_load($vid);
        
$i 0;
        
$entity->$target = isset($entity->$target) ? $entity->$target : array();
        foreach (
$terms as $term) {
            
$tid 0;
            if (
$term instanceof FeedsTermElement) {
              
$tid $term->tid;
            }
            elseif (
is_numeric($term)) {
              
$tid $term;
            }
            elseif (
is_string($term)) {
              
$tid taxonomy_term_check_term($term$vocabulary->vid);
            }
            if (
$tid) {
              
$entity->{$target}['und'][$i]['tid'] = $tid;
            }

            if (

$info['cardinality'] == 1) {
              break;
            }
            
$i++;
        }
    }
}
?>

Собственно, с помощью таких манипуляций можно сделать многое. У меня, например, получилось прикручивать изображения из архива, который посылается вместе с CSV-файлом импорта. Но это уже совсем другая история Smile