Startseite
  • » Home
  • » Handbuch & FAQ
  • » Forum
  • » Übersetzungsserver
  • » Suche
Startseite › Forum › Drupalcenter.de › Module ›

Node Import: Jemand der gut PHP kann? Kleines Problem...

Eingetragen von wflorian (251)
am 16.10.2008 - 09:31 Uhr in
  • Module

Hallo,

ich versuche das Modul node_import für mich nutzbar zu machen. Hierbei können aus einer CSV Datei Nodes erstellt werden. Ich möchte nun allerdings das manche CCK Felder leer bleiben, sprich ein SQL "NULL" Wert bekommen. Das Nodeimport Modul schreibt aber in alle CCK Felder für die keine Informationen in der CSV Datei hinterlegt worden sind, eine normale Zahl 0 rein!

Da ich PHP nur teilweise verstehe, komme ich mit folgendem Codeteil nicht weiter:

      // Force a node value for missing data.
        if (empty($node->$dummy_name)) {
          $values = array(0 => $globals['fields'][$dummy_name]);
        }
        else {
          // Explode multiple values to create the $delta and $value for each.
          if ($field['multiple']) {
            $values = explode('||', $node->$dummy_name);
          }
          else {
            $values = array(0 => $node->$dummy_name);
          }
        }

        $value = ((!empty($field['widget']['default_value'][0]['value'])) ?  $field['widget']['default_value'][0]['value'] : '');

Ich denke, dass das das Richtige Stück Code sein müsste, das für die obenbeschriebene Problematik verantwortlich ist, oder nicht?
Ich habe bereits rumprobiert, bin aber zu keinem Ergebnis gekommen. Könnte mir vll. bitte jemand den Code so verändern, dass ein SQL "NULL" Wert in das Feld geschrieben wird, wenn kein Wert in der CSV hinterlegt ist.

Anbei auch noch die komplette Datei:.

Ich wäre euch wirklich vielmals dankbar, wenn mir jemand weiterhelfen kann.

Danke und Grüße
Florian

<?php
/* $Id: content.inc,v 1.1.2.2.2.6 2008/03/17 13:48:12 robrechtj Exp $ */

/**
*  CCK data can be imported into any column of a CCK field.
*  Most fields have a 'value' column, but there are others that
*  can be used, for example:
*   - Textareas have a 'value' and a 'format'
*   - Date fields have a 'value', if date-specific timezones are
*       used they also have a 'timezone' and 'offset'
*   - Nodereference fields have a 'nid' column
*   - Userreference fields have a 'uid' column
*
*  For nodereference and userreference fields you can either import the
*  numeric value for a nid or uid, or a text value for the title or
*  name. If a numeric value is imported, it is used as is.
*  If a title or name is imported, the module will search for
*  the nid or uid that goes with that title or name.
*
*  Date values can be imported as unix timestamps, as ISO values, or as
*  any text that can be interpreted by the php strtotime function.
*  The date 'timezone' column expects a full timezone name like 'US/Central',
*  timezone abbreviations are not unique and will not work.
*  The date 'offset' column is the plus or minus value in seconds.
*
*  @todo
*  Multiple values are not importing correctly yet -- CCK is creating
*  three blank values before the valid values, so a fix to CCK is needed.
*  Once fixed the idea is that multiple values can be imported into any
*  CCK field that has multiple values enabled by separating the values
*  with a double pipe (||).
*/


/**
* Implementation of hook_node_import_fields().
*/
function content_node_import_fields($type) {
  $types = (array) content_types();
  $fields = array();

  if (isset($types[$type])) {
    $content_type = $types[$type];
    foreach ($content_type['fields'] as $field) {
      // Create a potential input field for each field column by creating a dummy name for each column.
      $db_info = content_database_info($field);
      foreach ($db_info['columns'] as $column => $info) {
        $fields[$field['field_name'] .'_'. $column] = $field['widget']['label'] .' '. $column .' ('. $field['field_name'] .')';
      }
    }
  }
  return $fields;
}

/**
* Implementation of hook_node_import_prepare().
*/
function content_node_import_prepare(&$node, $preview = FALSE) {
  $errors = array();
  $globals = $node->content_import_node;
  unset($node->content_import_node);

  $types = (array) content_types();
  if (isset($types[$node->type])) {
    $content_type = $types[$node->type];
    foreach ($content_type['fields'] as $field) {
      $field_name = $field['field_name'];

      // Find the column names for this field type and process each.
      $db_info = content_database_info($field);
      foreach ($db_info['columns'] as $column => $info) {
        $dummy_name = $field['field_name'] .'_'. $column;

      // Force a node value for missing data.
        if (empty($node->$dummy_name)) {
          $values = array(0 => $globals['fields'][$dummy_name]);
        }
        else {
          // Explode multiple values to create the $delta and $value for each.
          if ($field['multiple']) {
            $values = explode('||', $node->$dummy_name);
          }
          else {
            $values = array(0 => $node->$dummy_name);
          }
        }

        foreach ($values as $delta => $value) {
          switch ($field['type']) {
            case 'date':
            case 'datestamp':
              // Get properly formatted date for value column.
              // Assume timezone and offset values are correct as input.
              if ($column == 'value') {
                include_once(drupal_get_path('module', 'date') .'/date.inc');
                if ($field['type'] == 'date') {
                  // Is the imported date a unix timestamp? If so convert it to ISO date.
                  if (is_numeric($value) && date_is_valid($value, DATE_UNIX)) {
                    $value = date_unix2iso($value);
                  }
                  // If the imported date is a (partial) ISO date, convert it
                  // to date's ISO date.
                  else if (($date_array = date_iso2array($value)) != 'ERROR') {
                    $value = date_array2iso($date_array);
                  }
                  // Otherwise, use strtotime and unix2iso to convert
                  // partial ISO or text date into full ISO.
                  else {
                    $value = date_unix2iso(strtotime($value));
                  }
                }
                else {
                  // Is this already a unix timestamp? If not try strototime.
                  if (!is_numeric($value) || !date_is_valid($value, DATE_UNIX)) {
                    $value = strtotime($value);
                  }
                }
              }
              break;

            case 'nodereference':
              // If $value is numeric use it, otherwise
              // find a node title that matches and get nid.
              if (intval($value) > 0) {
                $value = intval($value);
              }
              else {
                $referenced_nodes = array();
                $sql = "SELECT nid, title FROM {node} WHERE title SOUNDS LIKE '%s'";
                $result = db_query($sql, $value);
                $record_found = db_fetch_object($result);
                if ($record_found) {
                  $value = $record_found->nid;
                }
              }
              break;

            case 'userreference':
              // If $value is numeric use it, otherwise
              // find a user name that matches and get uid.
              if (intval($value) > 0) {
                $value = intval($value);
              }
              else {
                if (($account = user_load(array('name' => $author['name'])))) {
                  $value = $account->uid;
                }
              }
              break;

            case 'number_integer':
              // Make sure numeric data is brought in as numeric values.
              $value = intval($value);
              break;

            case 'number_decimal':
              $value = floatval($value);
              break;
          }

          // Set the correct node field values.
          if (!is_array($node->$field['field_name'])) {
            $node->$field_name = array($delta => array($column => $value));
          }
          else {
            $node->$field_name += array($delta => array($column => $value));
          }
        }

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

    }
  }

  return $errors;
}

/**
* Implementation of hook_node_import_global().
*/
function content_node_import_global($type, $global_values) {
  $form = array();
  $form['content_import_node']['fields'] = array(
    '#type' => 'fieldset',
    '#title' => t('Field options'),
    '#description' => t('Select the default values you want to assign to all imported fields unless specifically set otherwise in the CSV file'),
    '#tree' => TRUE,
  );
  $types = (array) content_types();
  if (isset($types[$type])) {
    $content_type = $types[$type];
    foreach ($content_type['fields'] as $field) {
      // Create a default value for each field column by creating a dummy name for each column.
      $db_info = content_database_info($field);
      foreach ($db_info['columns'] as $column => $info) {
        $value = ((!empty($field['widget']['default_value'][0]['value'])) ?  $field['widget']['default_value'][0]['value'] : '');
        $form['content_import_node']['fields'][$field['field_name'] .'_'. $column] = array(
          '#type' => 'textfield',
          '#title' => $field['widget']['label'] .' '. $column,
          '#default_value' => $value,
        );
      }
    }
  }
  return $form;
}

‹ modul webform: wie default size für textfield ändern? mass_contact, installtiert, finde aber nichts, wo ich eine Mail verfassen und versenden könnte ›
  • Anmelden oder Registrieren um Kommentare zu schreiben

Lösung

Eingetragen von Frank Ralf (2135)
am 17.10.2008 - 19:29 Uhr

Hallo Florian,

nur für andere Benutzer, die auch nach der Lösung suchen, hier der Link auf dein Posting der Original-Issue-Queue: http://drupal.org/node/321753

Frank

XING

Gruß
Frank

Bitte Erledigtes im Betreff des ersten Postings als [gelöst] markieren. Danke!

  • Anmelden oder Registrieren um Kommentare zu schreiben

Du hast gut aufgepasst Frank

Eingetragen von wflorian (251)
am 17.10.2008 - 19:38 Uhr

Du hast gut aufgepasst Frank :D

  • Anmelden oder Registrieren um Kommentare zu schreiben

Benutzeranmeldung

  • Registrieren
  • Neues Passwort anfordern

Aktive Forenthemen

  • entity print - pdf template Seitennummern
  • Webform - kleiner Einleitungstext vor dem Formular.
  • migrate: legacy-db-key in settings.php, wie?
  • Konto löschen, wie? (Drupalorg/Drupalcenter)
  • Wie 'saubere' Dateinamen bei Upload erzwingen?
  • Modul lässt Website anstürzen
  • Showroom
  • rename admin paths - Probleme mit Modul - Alterantive?
  • Probleme mit Installation voa COMPOSER
  • Drupal- Vor- und Nachteile
  • Text Editor verschwunden
  • Wie URL Alias für Entity in Drupal 9 erstellen?
Weiter

Neue Kommentare

  • Du könntest einen
    vor 2 Tagen 41 Minuten
  • Das findet man in diesem
    vor 4 Tagen 1 Stunde
  • Hallo, bitte löscht meinen
    vor 5 Tagen 23 Stunden
  • Schau mal hier
    vor 6 Tagen 5 Stunden
  • Das Modul ist ja ganz schön,
    vor 1 Woche 2 Tagen
  • Modul Purge
    vor 1 Woche 2 Tagen
  • Nö
    vor 1 Woche 3 Tagen
  • Manuell aus der Datenbank löschen
    vor 1 Woche 3 Tagen
  • Bots ... auf Abstand
    vor 1 Woche 3 Tagen
  • Cache vs Browser
    vor 1 Woche 3 Tagen

Statistik

Beiträge im Forum: 247807
Registrierte User: 19542

Neue User:

  • Dvkah
  • Dhev
  • Chrisvek

» Alle User anzeigen

User nach Punkten sortiert:
wla9210
stBorchert6003
quiptime4972
Tobias Bähr4019
bv3924
ronald3845
md3717
Thoor3678
Alexander Langer3416
Exterior2903
» User nach Punkten
Zur Zeit sind 0 User und 4 Gäste online.

Hauptmenü

  • » Home
  • » Handbuch & FAQ
  • » Forum
  • » Übersetzungsserver
  • » Suche

Quicklinks I

  • Infos
  • Drupal Showcase
  • Installation
  • Update
  • Forum
  • Team
  • Verhaltensregeln

Quicklinks II

  • Drupal Jobs
  • FAQ
  • Drupal-Kochbuch
  • Best Practice - Drupal Sites - Guidelines
  • Drupal How To's

Quicklinks III

  • Tipps & Tricks
  • Drupal Theme System
  • Theme Handbuch
  • Leitfaden zur Entwicklung von Modulen

RSS & Twitter

  • Drupal Planet deutsch
  • RSS Feed News
  • RSS Feed Planet
  • Twitter Drupalcenter
Drupalcenter Team | Impressum & Datenschutz | Kontakt
Angetrieben von Drupal | Drupal is a registered trademark of Dries Buytaert.
Drupal Initiative - Drupal Association