Drupal 6.x modul fejlesztése - Adatok beolvasása CCK mezőkbe

Drupal 6.x-ben saját modulból szeretnénk az adminisztrációs oldalon egy lehetőséget felkínálni adatok node-okba tartalom feltöltésére, amelyek CCK mezőket is tartalmaznak.

Az alábbi megoldás nem teljesen tökéletes, azaz vannak benne még ki nem dolgozott részek, de már így is talán hasznos lehet annak, aki az egyes rész megoldásokra is kiváncsi. Pédául a CCK referencia mezők adatainak a feltöltésénél a hivatkozott node-ok azonosítása nincs megoldva.

Alább egy olyan megoldás látható, amelyben az új modul (mymodule) adminisztrációs oldalán megjelenik egy jelölőnégyzet (mymodule_load_defaults), és ha azt bejelölik, akkor a mentés után lefut az adatokat betöltő függvény (_mymodule_load).

<?php
// $Id: mymodule.module
/** -----------------------------------------------------------------------
* Implementation of hook_perm
* Valid permissions for this module
*/
function mymodule_perm() {
  return array(
   /* Here are the other permissions */
   'administer mymodule',
  );
} // function ci_checkbox_perm()
/** -----------------------------------------------------------------------
* Implementation of hook_menu
**/
function mymodule_menu() {
  $items = array();
  /* Here are the other menus */
  // Admin setting page
  $items['admin/settings/mymodule'] = array(
    'title' => 'MyModule',
    'description' => 'MyModule administration page',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_mymodule_admin'),
    'access arguments' => array('administer mymodule'),
    'type' => MENU_NORMAL_ITEM,
   );
  return $items;
} // function mymodule_menu()
/** -----------------------------------------------------------------------
* Admin settings
*/
function _mymodule_admin() {
  $form = array();
  /* Here are the other Form elements...
  */
  // If the loading was successfull, we don't show again this possibility on the admin form
  if (!variable_get('mymodule_load_defaults', 0)) {
    $form['mymodule_load_defaults'] = array(
      '#type' => 'checkbox',
      '#title' => t('Load default data'),
      '#default_value' => variable_get('mymodule_load_defaults', 0),
      '#description' => t("Check ON when you want to load default data."),
      '#required' => FALSE,
    );
    // After submit of the admin page, we want to call an other own function too: _mymodule_load
    $form['#submit'][] = '_mymodule_load';
  };
  return system_settings_form($form);
} // function _mymodule_admin()
/** -----------------------------------------------------------------------
* Load Default data
**/
function _mymodule_load($form, &$form_state) {
  // Exit if Load Default is not checked.
  if (!$form["#post"]['mymodule_load_defaults']) return;
  // Loading default data message
  drupal_set_message("Loading Default Data...");
  // Data for loading
  $data = array(
    "cck_region" => array( // the key is an existing content type      
      array("title" => "EU", "field_longname" => "Europe"),  // the "field_longname" is an existing CCK field of the cck_region type 
      array("title" => "NA", "field_longname" => "North America"),
    ),
    "cck_country" => array(// for the final resolution the value of "field_region" has to be found by the Region title. Here we put there the node ID, what is not good!!!
      array("title" => "UK",      "field_region" => 1), // the "field_region" is an existing CCK field, node-reference to the cck_region
      array("title" => "Hungary", "field_region" => 1),
      array("title" => "US",      "field_region" => 2),
      array("title" => "Canada",  "field_region" => 2),
    ),
  );
  // Calling the function for node save
  foreach ($data as $type => $fieldSets)
    foreach ($fieldSets as $fieldSet)
    _mymodule_add_node($type, $fieldSet);
  // Set Load Defaults variable into TRUE
  variable_set('mymodule_load_defaults', TRUE);
  drupal_set_message("Loading Default Data... DONE.");
} // function _mymodule_load
/** -----------------------------------------------------------------------
* Add new node
**/
function _mymodule_add_node($type, $fieldSet = array()) {
  module_load_include('inc', 'node', 'node.pages');
  global $user;
  // We load the Content Type info for deciding how to save the new node
  $cti       = _content_type_info();
  $ctiType   = $cti['content types'][$type];
  $ctiFields = $cti['content types'][$type]["fields"];
  // Base data for the new node
  $node = new StdClass();
  $node->type    = $type;       // Node Type
  $node->uid     = $user->uid;  // Author User ID
  $node->status  = 1;           // Change to 1 to publish
  $node->comment = 0;           // Allow comments?
  $node->promote = 0;           // Promote to front page?
  // Additional data for the new node
  foreach ($fieldSet as $field => $value)
    // If the field is in teh list of Fields, we check the field type
    if (isset($ctiFields[$field])) {
      // If the field type is "nodereference", means the input Value is a NID
      if ($ctiFields[$field]["type"] == "nodereference") {
        $node->{$field}[0]["nid"]   = $value;
      // Otherwise the input Value is a value
      } else {
        $node->{$field}[0]["value"] = $value;
      };
    // If the field is not in the list of Fields, we save it like a node base field value
    } else {
      $node->$field = $value;
    };
  // Save the field - it will be a new field because there isn't the NID value filled out
  node_save($node);
  // If the returned value exists, means the node is saved without errors
  if ($node->nid) {
    drupal_set_message("Node is saved. Type = $type Nid = ".$node->nid);
  } else
    drupal_set_message("Node failed to save. Type = $type", "error");
  // We return the node-nid for future use
  return $node->nid;
} // function _mymodule_add_node