Im quite new to drupal 7. But my approach is learning by doing. This have worked fine until now....

My problem is, that I in a custom module, with a form submit, save a node with all the filled fields, including images. (Code below)

My problem is that i want to add a, a href to the images i save, so when opening the node the images will link to the link i have saved.

Is this even possible?

My node_save with pictures is like this:

$who = $user->uid;
$test = 'term_id' . $who;
$test1 = 'sales_id' . $who;
variable_set($test, $base_url . '/Sales/' . $form_state['values']['sales_id_terms']);
variable_set($test1, $base_url . '/Sales/' . $form_state['values']['sales_id_comp']);

$term_id = $form_state['values']['sales_id_terms'];
$file_temp_top = file_load($user->top_banner);
$file_temp_bund = file_load($user->bund_banner);
$file_temp_power = file_load(110);

//$file_temp_top = file_save_data($file_temp_top, 'public://firm_images/', FILE_EXISTS_REPLACE);

$node1 = new stdClass();  // Create a new node object
$node1->type = 'product_link';  // Content type
$node1->language = LANGUAGE_NONE;  // Or e.g. 'en' if locale is enabled
$node1 = node_submit($node1);
node_object_prepare($node1);  //Set some default values
$node1->status = 1;   // (1 or 0): published or unpublished
$node1->promote = 0;  // (1 or 0): promoted to front page or not
$node1->sticky = 0;  // (1 or 0): sticky at top of lists or not
$node1->comment = 1;  // 2 = comments open, 1 = comments closed, 0 = comments hidden

$node1->title = $form_state['values']['sales_name'] . '_1';
$node1->field_betingelser['und'][0]['value'] = $terms;
$node1->field_top_banner['und'][0] = array(
                'fid' => $file_temp_top->fid,
                'filename' => $file_temp_top->filename,
                'filemime' => $file_temp_top->filemime,
                'uri' => $file_temp_top->uri,
                'status' => 1,
                'display' => 1,
$node1->field_bund_banner['und'][0] = array(
                'fid' => $file_temp_bund->fid,
                'filename' => $file_temp_bund->filename,
                'filemime' => $file_temp_bund->filemime,
                'uid' => 1,
                'uri' => $file_temp_bund->uri,
                'status' => 1,
                'display' => 1,
$node1->field_powerd_by['und'][0] = array(
                'fid' => $file_temp_power->fid,
                'filename' => $file_temp_power->filename,
                'filemime' => $file_temp_power->filemime,
                'uid' => 1,
                'uri' => $file_temp_power->uri,
                'status' => 1,
                'display' => 1,
                'href' => $GLOBALS['root_url'] . '/new-products/',
$node1->field_terms_id['und'][0]['value'] = $form_state['values']['kon_id_terms'];

// Add author of the node
$node1->uid = $user->uid;

// Set created date
$node1->date = 'complaint_post_date';
$node1->created = strtotime('complaint_post_date');

$new_path = 'sales/' . $node1->field_terms_id['und'][0]['value'];
$node1->path = array('alias' => $new_path);

// Save the node

I'm assuming the content type you're saving is currently using the included Image field. This field does not allow you to include a url that the image links to (it does allow you to link to the image itself or the content that it belongs to).

Answering your question directly, it sure is (and there's many ways)!

You can use the module linkimagefield. I think this one is closest to what you're asking. It adds a field type that includes a url that the image can then link to.

However, if the url the image links to is to be used in other places too, then the module image_link_formatter might be better suited for the task. With this module, you would add a Link field to your content type (that can be shown by itself or hidden), and set up the image field's display (admin/structure/types/manage/[node_type]/display) to use the link field as the target.

  • I have tried linkimagefield, as suggested. My problem is that it does not save the field. Is there something i have to change in the $node1->field_top_banner to make it work with imagelinfield? I have tried to look in the database to see the difference between the two fields. But I can not see what to alter.
    – Espen
    Sep 3 '17 at 7:27
  • The code im using now is: '$node1->title = $form_state['values']['sales_name'] . '_1'; $node1->field_betingelser['und'][0]['value'] = $terms; $node1->field_top_banner['und'][0] = array( 'fid' => $file_temp_top->fid, 'filename' => $file_temp_top->filename, 'filemime' => $file_temp_top->filemime, 'uri' => $file_temp_top->uri, 'status' => 1, 'display' => 1, 'url' => $user->homepage, 'target' => '_self', 'class' => 'top_banner',);'
    – Espen
    Sep 3 '17 at 7:50
  • My bad! it was the fid that could not be recognized. Everything works now! Thank you very much for your suggestion.
    – Espen
    Sep 3 '17 at 8:27

The file field will handle the display of the file regarding to its settings in the FieldUI.

So referencing the fid is sufficient:

$node1->field_top_banner['und'][0]['fid'] = $file_temp_top->fid;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.