Tuesday, September 20, 2011

Creating new custom node with image field and other in drupal7

This code is useful for making a custom node with fields and it also guides how to add data to your custom fields

function node_photo_create_data()
{
$qry = db_query("select * from tp_photos limit 1");
foreach($qry as $qryString)
{
  $image_valid=db_query("select * from field_data_field_tp_image_id where field_tp_image_id_value=".$qryString->photo_id);
   drupal_set_message(count($image_valid));
  if(count($image_valid)==0)
   {
  $node = (object) array
  (
    'title' => $qryString->photo_title,
    'type' => 'tp_photo',
    'language' => LANGUAGE_NONE, 
   );
 node_object_prepare($node);
 $filepath = drupal_realpath('images/shutterstock_'.$qryString->photo_id.'.jpg');
 $file = (object) array(
     '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://tpimages');
  $node->field_tp_image[LANGUAGE_NONE][0] = (array)$file;
  $node->field_tp_image_cityid[LANGUAGE_NONE][0]['value']=$qryString->city_id;
  $node->field_tp_image_copyright[LANGUAGE_NONE][0]['value']=$qryString->photo_copyright;
  $node->field_tp_image_id[LANGUAGE_NONE][0]['value']=$qryString->photo_id;
  $node->field_tp_image_url[LANGUAGE_NONE][0]['value']=$qryString->photo_url;
  $node->field_tp_image_loc[LANGUAGE_NONE][0]['value']=$qryString->photo_location;
  $node->field_tp_image_category[LANGUAGE_NONE][0]['value']=$qryString->photo_category;
  
  $node->field_tp_image_source[LANGUAGE_NONE][0]['value']=$qryString->photo_Source;
  $node->field_tp_image_tags[LANGUAGE_NONE][0]['value']=$qryString->photo_tags;
 
  node_save((object) $node);

      } //for each
}
}

note: create a new content type and name add fields which you want to add it.and then assign its custom name data

No comments:

Post a Comment