Skip to main content
added another example
Source Link
Codium
  • 5k
  • 11
  • 41
  • 91

EDIT

Anyway I switched from node type to entity. The issue still present. I put test code to Drupal installation which is using database called 'gallery':

$e = entity_create('model', array('type' => 'image'));
$ew = entity_metadata_wrapper('model',$e);
$ew->field_entity_image_is_public->set(1);
$ew->field_entity_image->set(array(
    'fid' => 12,
    'alt' => 'alt',
    'title' => 'title',
));
$ew->field_entity_image_name->set('image name');
$ew->save();

It works ok, but when I try to use similar snipper in other installation:

$file = file_load($image['fid']);
unset($file->fid);
$file->uid = 1;
db_set_active('gallery');
file_save($file);
$e = entity_create('model', array('type' => 'image'));
$ew = entity_metadata_wrapper('model',$e);
$ew->field_entity_image_is_public->set(1);
$ew->field_entity_image->set(array(
    'fid' => $file->fid,
    'alt' => $image['alt'],
    'title' => $image['title'],
));
$ew->field_entity_image_name->set($file->filename);
$ew->save();
db_set_active();

Custom fields are not stored. Only record in file_managed table is inserted correctly.

All Drupal installations are on the same server so I thought that I can just switch to other installation database, but now I start thinking about using somekind of web service but it is pointless in this case :/

EDIT

Anyway I switched from node type to entity. The issue still present. I put test code to Drupal installation which is using database called 'gallery':

$e = entity_create('model', array('type' => 'image'));
$ew = entity_metadata_wrapper('model',$e);
$ew->field_entity_image_is_public->set(1);
$ew->field_entity_image->set(array(
    'fid' => 12,
    'alt' => 'alt',
    'title' => 'title',
));
$ew->field_entity_image_name->set('image name');
$ew->save();

It works ok, but when I try to use similar snipper in other installation:

$file = file_load($image['fid']);
unset($file->fid);
$file->uid = 1;
db_set_active('gallery');
file_save($file);
$e = entity_create('model', array('type' => 'image'));
$ew = entity_metadata_wrapper('model',$e);
$ew->field_entity_image_is_public->set(1);
$ew->field_entity_image->set(array(
    'fid' => $file->fid,
    'alt' => $image['alt'],
    'title' => $image['title'],
));
$ew->field_entity_image_name->set($file->filename);
$ew->save();
db_set_active();

Custom fields are not stored. Only record in file_managed table is inserted correctly.

All Drupal installations are on the same server so I thought that I can just switch to other installation database, but now I start thinking about using somekind of web service but it is pointless in this case :/

updated code snippet
Source Link
Codium
  • 5k
  • 11
  • 41
  • 91
$file = file_load($image['fid']);
unset($file->fid);
$file->uid = 1;
file_save($file);
$image['fid'] = $file->fid;
$node = new stdClass;
$node->type = GALLERY_IMAGE_TYPE;
node_object_prepare($node);
$node->is_new = True;
$node->language = LANGUAGE_NONE;
$node->title = $file->filename;
$node->uid = 1;
$node->status = 1;
$node->{GALLERY_IMAGE_FIELD_NAME}[LANGUAGE_NONE][] = array(
    'fid' => $file->fid,
    'alt' => $image['alt'],
    'title' => $image['title'],
);
$node->{GALLERY_IMAGE_PUBLIC_FIELD_NAME}[LANGUAGE_NONE][]['value'] = $is_public;
node_save($node);
db_set_active();
$file = file_load($image['fid']);
unset($file->fid);
$file->uid = 1;
file_save($file);
$image['fid'] = $file->fid;
$node = new stdClass;
$node->type = GALLERY_IMAGE_TYPE;
node_object_prepare($node);
$node->is_new = True;
$node->language = LANGUAGE_NONE;
$node->title = $file->filename;
$node->uid = 1;
$node->status = 1;
$node->{GALLERY_IMAGE_FIELD_NAME}[LANGUAGE_NONE][] = array(
    'fid' => $file->fid,
    'alt' => $image['alt'],
    'title' => $image['title'],
);
$node->{GALLERY_IMAGE_PUBLIC_FIELD_NAME}[LANGUAGE_NONE][]['value'] = $is_public;
node_save($node);
db_set_active();
$node = new stdClass;
$node->type = GALLERY_IMAGE_TYPE;
node_object_prepare($node);
$node->is_new = True;
$node->language = LANGUAGE_NONE;
$node->title = $file->filename;
$node->uid = 1;
$node->status = 1;
$node->{GALLERY_IMAGE_FIELD_NAME}[LANGUAGE_NONE][] = array(
    'fid' => $file->fid,
    'alt' => $image['alt'],
    'title' => $image['title'],
);
$node->{GALLERY_IMAGE_PUBLIC_FIELD_NAME}[LANGUAGE_NONE][]['value'] = $is_public;
node_save($node);
db_set_active();
added 173 characters in body
Source Link
Codium
  • 5k
  • 11
  • 41
  • 91
$file = file_load($image['fid']);
unset($file->fid);
$file->uid = 1;
file_save($file);
$image['fid'] = $file->fid;
$node = new stdClass;
$node->type = GALLERY_IMAGE_TYPE;
node_object_prepare($node);
$node->is_new = True;
$node->language = LANGUAGE_NONE;
$node->title = $file->filename;
$node->uid = 1;
$node->status = 1;
$node->{GALLERY_IMAGE_FIELD_NAME}[LANGUAGE_NONE][] = array(
    'fid' => $file->fid,
    'alt' => $image['alt'],
    'title' => $image['title'],
);
$node->{GALLERY_IMAGE_PUBLIC_FIELD_NAME}[LANGUAGE_NONE][]['value'] = $is_public;
node_save($node);
db_set_active();
$file = file_load($image['fid']);
unset($file->fid);
$file->uid = 1;
file_save($file);
$image['fid'] = $file->fid;
$node = new stdClass;
$node->type = GALLERY_IMAGE_TYPE;
node_object_prepare($node);
$node->is_new = True;
$node->language = LANGUAGE_NONE;
$node->title = $file->filename;
$node->uid = 1;
$node->status = 1;
$node->{GALLERY_IMAGE_PUBLIC_FIELD_NAME}[LANGUAGE_NONE][]['value'] = $is_public;
node_save($node);
db_set_active();
$file = file_load($image['fid']);
unset($file->fid);
$file->uid = 1;
file_save($file);
$image['fid'] = $file->fid;
$node = new stdClass;
$node->type = GALLERY_IMAGE_TYPE;
node_object_prepare($node);
$node->is_new = True;
$node->language = LANGUAGE_NONE;
$node->title = $file->filename;
$node->uid = 1;
$node->status = 1;
$node->{GALLERY_IMAGE_FIELD_NAME}[LANGUAGE_NONE][] = array(
    'fid' => $file->fid,
    'alt' => $image['alt'],
    'title' => $image['title'],
);
$node->{GALLERY_IMAGE_PUBLIC_FIELD_NAME}[LANGUAGE_NONE][]['value'] = $is_public;
node_save($node);
db_set_active();
Source Link
Codium
  • 5k
  • 11
  • 41
  • 91
Loading