Adjuntar archivos de imagen a nodos mediante programación en Drupal 7


Es posible añadir una imagen a un nodo mediante programación?

Author: AgA, 2011-02-26

8 answers

Aquí hay un código de ejemplo que puede usar con node_save

$filepath = drupal_realpath('misc/druplicon.png');
  // Create managed File object and associate with Image field.
  $file = (object) array(
    'uid' => 1,
    'uri' => $filepath,
    'filemime' => file_get_mimetype($filepath),
    'status' => 1,
  );

  // We save the file to the root of the files directory.
  $file = file_copy($file, 'public://');

  $node->field_image[LANGUAGE_NONE][0] = (array)$file;
`
 50
Author: Daniel Wehner,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-02-28 11:41:13

Una manera más fácil:

$filename = 'image.txt';
$image = file_get_contents('http://www.ibiblio.org/wm/paint/auth/gogh/gogh.white-house.jpg');
$file = file_save_data($image, 'public://' . $filename, FILE_EXISTS_RENAME);
$node->field_image = array(LANGUAGE_NONE => array('0' => (array)$file));
 31
Author: Juan Pablo Novillo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-09-22 14:57:22

Esto es lo que funcionó para mí:

$file_temp = file_get_contents('public://someimage.jpg');

// Saves a file to the specified destination and creates a database entry.
$file_temp = file_save_data($file_temp, 'public://' . 'someimage.jpg', FILE_EXISTS_RENAME);

$node->field_page_image = array(
  'und' => array(
    0 => array(
      'fid' => $file_temp->fid,
      'filename' => $file_temp->filename,
      'filemime' => $file_temp->filemime,
      'uid' => 1,
      'uri' => $file_temp->uri,
      'status' => 1,
      'display' => 1
    )
  )
);
 6
Author: Randell,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-12 13:34:41

Aquí hay un poco más que me hizo tropezar por un tiempo: esto adjuntará la imagen al nodo, y si estás agregando la imagen, entonces estás bien. Sin embargo, si estás actualizando una imagen, y te importa mostrarla en una página, entonces se necesita un paso adicional antes de llamar a node_save ():

image_path_flush($node->field_image['und'][0]['uri']);

Esto regenerará todos los estilos de esa imagen.

 3
Author: Ergaster,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-01-04 06:29:11

$node->field_image[LANGUAGE_NONE][0] = (array)$file;

Probé esto con un sitio multilingüe. Fracasó bastante... pero horriblemente. Tuve que especificar el idioma en cuestión. En pocas palabras, esto funcionó en su lugar:

$node->field_image['en'][0] = (array)$file;

Sin él, el archivo adjunto se podía ver en la pantalla 'view' pero no en la pantalla 'edit'.

 2
Author: rockthedrop,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-24 08:02:09

Sí, hágalo parte del objeto node node cuando lo guarde. Guardarlo usando node_save().

 0
Author: cjm2671,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-02-26 21:17:07

Esto funciona para mí:

define('DRUPAL_ROOT', $_SERVER['DOCUMENT_ROOT']);
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$node = node_load(99);
$filename = 'image.txt';
chdir(DRUPAL_ROOT);
$image = file_get_contents('http://www.ibiblio.org/wm/paint/auth/gogh/gogh.white-house.jpg');
$file = file_save_data($image, 'public://' . $filename, FILE_EXISTS_RENAME);
$node->field_imagen_producto = array(LANGUAGE_NONE => array('0' => (array)$file));
node_save($node);
 0
Author: Dexpo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-28 16:16:20

Simplemente voy a pegar mi solución aquí también, necesitaba crear un nuevo nodo y cargar una imagen mediante programación.

$filepath = variable_get('file_public_path') . '/xmas_banner.jpg';
$file_temp = file_get_contents($filepath);
$file_temp = file_save_data($file_temp, file_default_scheme() . '://' .'xmas_banner_nl.jpg', FILE_EXISTS_RENAME);

$node = new stdClass();
$node->type = 'carousel'; // custom content type
$node->title = 'XMAS NL';
$node->field_banner_image[LANGUAGE_NONE][0] = (array) $file_temp;
$node->uid = 1;
$node->status = 0;
$node->active = 0;
$node->promote = 0;
node_save($node);
 0
Author: Bram,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-08 12:47:32