欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

导入 excel 为 drupal节点2 平忠自己实现的方法 有大用

shiping1 的头像

节点导入的时候 创建节点 对象时 有额外的动作   其实我们不需要在这时(node import和hook) 进行做什么 我们所要做的 其实就是在节点保存时 hook一下 这里的node import 的hook 用不到

这是平忠经过实践得来的 excel的要用utf-8的格式 ,同时在drupal的后台配置导入时(admin/content/node_import/add)

我们的格式 其实是指配置excel里面的格式,而不是drupal里面的格式

 

1) PHPExcel 是用来操作Office Excel 文档的一个PHP类库,它基于微软的OpenXML标准和PHP语言。可以使用它来读取、写入不同格式的电子表格,如 Excel (BIFF) .xls, Excel 2007 (OfficeOpenXML) .xlsx, CSV, Libre/OpenOffice Calc .ods, Gnumeric, PDF, HTML等等。

项目主页:https://github.com/PHPOffice/PHPExcel

Drupal 通过Library 调用 PHPExcel

将PHPExcel 下载后,上传到Drupal目录:sites/all/libraries/PHPExcel

如果你的项目中安装了libraries模块,可以通过libraries_load($name);来调用。
libraries_load('PHPExcel');

如果没有安装libraries模块,可以简单的使用下列代码来调用:

require("sites/all/libraries/PHPExcel/PHPExcel/IOFactory.php");

注意为了确保Excel全部导入,程序可以会话很长的时间来进行。

所以在代码开头部分加入:

set_time_limit(0);

来确保运行时间不受限制。

(好像上面的根本就不需要)
 
2)  Node import 模块                      (node_import)    (好像只能导 csv 或 tsv)
  
3)  Node import via cron                (ni_cron.info)
 
平忠的方法
1) 安装上面的两个模块 (上面的第一步 使用phpexcel的类库可能不需要)
2) admin/content/node_import 这个路径
再到 admin/content/node_import/list/files 路径 然后在最下面可以看到 "upload file" 按钮
上传csv 文件  (上传的文件必须为utf-8的格式)
3) admin/content/node_import/add 新建导入 下一步 下一步  (其中excel的要用utf-8,其中用到格式的话是excel的格式 而不是drupal的格式)直到 "start import" 开始导入
   

讓 Drupal 的 Node Import 模組支援 option widget

來源:http://blog.pixnet.net/HACGIS/post/19002887

Node Import 模組是個非常好用的模組,可以把各種類型的 Node 資料先用打好,存成 csv 檔然後匯入。
不過對於自設的 CCK 欄位有個缺憾,就是在匯入之前必須先把 widget 的型態,由 option (select list / checkbox / radio) 改成 text,這樣才能匯入,實在是不太方便。
我修改了一點程式,讓它可以不用轉換 widget 的型態也能匯入,不過有個限制,目前修改的方式,只支援 Allowed values list 為下列形式:

value1
value2
vaule3

不支援 Allowed values list 為下列形式:

key1|value1
key2|value2
key3|value3

這是要注意的地方。

修改方式:
找出 node_import/supported/cck/content.inc 中的 function content_node_import_prepare
在該函數接近結尾的地方找到:

        // Unset the dummy column value.
        unset($node->$dummy_name);

改成:
        $option_type = array('options_select' => true, 'options_onoff' => true, 'options_buttons' => true);
        if (isset($option_type[$field['widget']['type']])) {
            $keys = array();
                    foreach($node->$field_name as $value_list) {
                        $keys[] = $value_list['value'];
                    }
                    if ($field['multiple'] || $field['widget']['type'] == 'options_onoff') {
                        $node->$field_name += array('keys' => $keys);
                    } else {
                        $node->$field_name += array('key' => reset($keys));
                    }
                }

        // Unset the dummy column value.
        unset($node->$dummy_name);

這樣就可以了~
jimmy's 的頭像

Re: 讓 Drupal 的 Node Import 模組支援 option widget

感謝分享~~~
真是感動
--
from open mind to open source~

--
from open mind to open source~

node_import 模块里面 node_import.api.php 下面是 node_import 的 hook函数

 

Node import hooks

  1. node_import

    1. 6

Functions & methods

NameDescription
hook_node_import_defaultsList the FAPI elements to set the default values for each field.
hook_node_import_defaults_alterChange the FAPI elements to set the default values for each field.
hook_node_import_fieldsList the available fields for given content type.
hook_node_import_fields_alterChange the list of available fields for given content type.
hook_node_import_format_optionsThis hook allows users to change some of the options the user is presented with on the wizard form.
hook_node_import_format_options_alterChange the format options.
hook_node_import_optionsList the FAPI elements to set the options for each field.
hook_node_import_options_alterChange the FAPI elements to set the options for each field.
hook_node_import_postprocessThis hook is invoked after the form to create the $type has been submitted. You can use this hook to do stuff after the node has (or has not) been created, eg use the nid to store some additional information in the db tables.
hook_node_import_taskThis hook is invoked when a task is loaded, inserted, deleted, resumed or suspended.
hook_node_import_typesList the available content types.
hook_node_import_types_alterChange the list of available content types.
hook_node_import_valuesList the (static) values to use to create the content type.
hook_node_import_values_alterChange the list of values to use to create the content type.
node_import_defaultsReturns a list of default (form elements).
node_import_fieldsReturns a list of available content fields for given node_import type.
node_import_format_optionsGet a list of options for different stuff presented to the user in the wizard form such as 'record separators', ...
node_import_optionsReturns a list of options (form elements).
node_import_typesReturns a list of available content types.
node_import_valuesCreate an array of values to submit to the form.

./node_import.inc, line 100

点上面的链接 可以进到 hook的 详细页面

来自 http://api.drupalhelp.net/api/node_import/node_import.inc/group/node_import_hooks/6

 

 

hook_node_import_values_alter() 例子

<?php
/**
* Implementation of hook_node_import_fields_alter().
*/

function yourmodule_node_import_fields_alter(&$fields, $type) {
 
// Check if the type is a node content type.
 
if (($node_type = node_import_type_is_node($type)) !== FALSE) {
   
// Now you would need to alter the field. Let's say the Drupal vocabulary has
    // ID 42. What you want to do is add a new 'preprocess' function to the
    // array of already present 'preprocess' functions. This function would
    // convert the input value from the CSV to a tid that taxonomy can use.
   
if (isset($fields['taxonomy:42']) {
     
$fields['taxonomy:42']['preprocess'] = array_unshift('yourmodule_business_logic', (array) $fields['taxonomy:42']['preprocess']);
    }

   
// If you would want to add the same business logic preprocess function
    // to all taxonomy forms, you could do a:
    // foreach ($fields as $fieldname => $fieldinfo) {
    //   if ($fieldinfo['input_format'] = 'taxonomy_term') {
    //     // same thing
    //   }
    // }
    // Note that yourmodule would need to run after the taxonomy
    // module currently. That's because supported/taxonomy.inc
    // does more or less the same thing in taxonomy_node_import_fields_alter().
    // I'll change that because it can do it just as well in
    // taxonomy_node_import_fields().
 
}
}


// What you have done now is told node_import that before submitting
// the value from the CSV file, you would first like to run a
// "yourmodule_business_logic" function on the value.
// See from the hook_node_import_docs.php:
/**
* ...
* - \b "preprocess" : Array of callback functions. Each of the functions
*   can preprocess the mapped value. This is a way to validate and
*   preprocess the input. These preprocess functions can
*   be used in companion with the hook_node_import_values_alter().
*
*   The signature of the callback is:
*     @code
*       $return = $function(&$value, $field, $options, $preview);
*     @endcode
*   and it should alter the $value passed. Note that if the field
*   "has_hierachy", the value passed will be an array (grandparent,
*   parent, child).
*
*   The $return value should be FALSE if there is an input error,
*   NULL if there was not, but not a valid value could be found or
*   TRUE if a valid value could be found and so other preprocess
*   functions can be skipped.
*
*   See @ref node_import_preprocess for examples.
*
*   Defaults to array().
*/

/**
* A preprocess function that converts a value to the correct
* taxonomy term.
*/

function yourmodule_business_logic(&$value, $field, $options, $preview) {
 
// The function is given the $value (from CSV file or from a previous
  // executed preprocess function) for the given $field (as returned by
  // node_import_fields()). It also passes some options for the field if
  // you have implemented hook_node_import_options().

  // The $field includes $fields['vocabulary'] which is the vocabulary
  // object by the way.

  // Now you need to lookup the $value in your "category source 1"
  // and convert it to a "drupal taxonomy term tid".
 
$value = yourmodule_lookup_source1($value);

 
// You could give some errors if the value does not appear to be
  // valid in source 1. Look at some the other examples such as
  // node_import_check_user_reference, etc (all *_check_* functions).

  // If you have succesfully looked up the value you can return
  // TRUE here. If you still want the builtin preprocess functions
  // to run (which convert a term name to a term tid) you would
  // return NULL. If there was an error, you would return FALSE.
 
return TRUE;
}

?>

来自 https://drupal.org/node/386496

普通分类: