Mapping existing file against a new content type

I am facing a situation here.

I have a content type A with an image field(field_upload_snaps) in it. There is another content type B which also contains an image field(field_valid_snaps).

Now user will upload the pics from A content type. So if user upload 3 pics via A then 3 FID and their corresponding URIs will be created in file_managed table in Drupal 7.

Now I have a requirement where I have to insert 2 images out of 3 programatically using Drupal code. Now there is no need to generate any new image copy in public URI as images already exist via A.

I tried two approaches:-

1) I have created a new node of Type B. I tried to fetch file URI,filesize,filemime and all relevant parameter from existing fid and associate with the node using this.

 global $user;  $node = new stdClass();  $node->type = "user_slideshow_snaps";  $node->language = LANGUAGE_NONE;  node_object_prepare($node);  $node->uid = $user->uid;  $file = (object) array(   'uid' => $user->uid,   'uri' => 'public://1.jpeg" ,   'filemime' => 'image/jpeg',   'status' => 1,  );    $node->field_upload_snaps['und'][] = (array)$file;  $node = node_submit($node); // Prepare node for a submit  node_save($node); 

But it gives me an error that “Cant insert duplicate value in file_managed”. It will give because such value already exist in file_managed via A. So I am not able to save value.

2) I saved node first and then manually insert in the image field like this.

 global $user;  $node = new stdClass();  $node->type = "user_slideshow_snaps";  $node->language = LANGUAGE_NONE;  node_object_prepare($node);  $node->uid = $user->uid;  $node = node_submit($node); // Prepare node for a submit  node_save($node);   db_insert('field_data_field_valid_snaps')     ->fields(array(          'entity_type' => 'node',     'bundle' => 'user_actualD_snaps',     'deleted'=> 0,     'entity_id'=>$node->nid,     'delta'=>0,     'field_valid_snaps_fid'=>517,     'field_valid_snaps_alt'=>'',     'field_valid_snaps_title'=>'',     'field_valid_snaps_width'=>200,     'field_valid_snaps_height'=>300,      ))     ->execute(); 

But the line after node_save() doesnt execute in this case. So I am not able to save it.

Are there any functions available which will map existing images into another table?

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Mapping existing file against a new content type

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.