0

I'm trying to change the image field 'Badge' when an article is situated at the top of a view 'Top 3' after it has been updated. This is my custom module:

function top_node_update($node) {
    $top = views_get_view_result('top_3');
    $first = $top[0]->nid;

    if ($first == $node->nid) {
        $node->field_badge[0]['value'] = '/default/files/images/gouden_rozet.png';
        field_attach_update('node', $node);
    }
}

Unfortunately, nothing happens when I edit and save an article.

What's going wrong and how can I solve this?


Edit: Everything seemed perfectly fine after @undersounds answer, because the image fields were correctly changed by my custom module. I used the same piece of code to also edit the images of the second and third node in the view:

<?php

function top_node_update($node) {
  $top = views_get_view_result('top_3');
  $first = $top[0]->nid;
  $second = $top[1]->nid;
  $third = $top[2]->nid;

  if ($first == $node->nid) {
    $image = file_get_contents('public://images/gouden rozet.png');
    $file  = file_save_data($image, 'public://images/gouden rozet.png',FILE_EXISTS_REPLACE);
    $node->field_badge[LANGUAGE_NONE]['0']['fid'] = $file->fid;
    field_attach_update('node', $node);
  }

  if ($second == $node->nid) {
    $image = file_get_contents('public://images/zilveren rozet.png');
    $file  = file_save_data($image, 'public://images/zilveren rozet.png',FILE_EXISTS_REPLACE);
    $node->field_badge[LANGUAGE_NONE]['0']['fid'] = $file->fid;
    field_attach_update('node', $node);
  }

  if ($third == $node->nid) {
    $image = file_get_contents('public://images/bronzen rozet.png');
    $file  = file_save_data($image, 'public://images/bronzen rozet.png', FILE_EXISTS_REPLACE);
    $node->field_badge[LANGUAGE_NONE]['0']['fid'] = $file->fid;
    field_attach_update('node', $node);
  }
}

I started playing around a bit to see whether everything worked and discovered this: when a certain node has for example the 'bronzen rozet' image and moves to the first row in the view, the image is changed to 'gouden rozet' - as it should be - but the bronzen rozet.png file is also deleted in my images file!

How can I prevent this?

  • @undersound: I've added the line after the field_attach_update, but it still doesn't work. – Jeroen Jan 30 '14 at 18:16
2
function top_node_update($node) {
      $top = views_get_view_result('top_3');
      $first = $top[0]->nid;
      $second = $top[1]->nid;
      $third = $top[2]->nid;

  if ($first == $node->nid) {
    $image = file_get_contents('public://images/badges/gouden rozet.png');
    $file  = file_save_data($image, 'public://images/gouden rozet.png',FILE_EXISTS_REPLACE);
    $node->field_badge[LANGUAGE_NONE]['0']['fid'] = $file->fid;
    field_attach_update('node', $node);
  }

  if ($second == $node->nid) {
    $image = file_get_contents('public://images/badges/zilveren rozet.png');
    $file  = file_save_data($image, 'public://images/zilveren rozet.png',FILE_EXISTS_REPLACE);
    $node->field_badge[LANGUAGE_NONE]['0']['fid'] = $file->fid;
    field_attach_update('node', $node);
  }

  if ($third == $node->nid) {
    $image = file_get_contents('public://images/badges/bronzen rozet.png');
    $file  = file_save_data($image, 'public://images/bronzen rozet.png', FILE_EXISTS_REPLACE);
    $node->field_badge[LANGUAGE_NONE]['0']['fid'] = $file->fid;
    field_attach_update('node', $node);
  }
}

You should also try to prevent using functionality twice. So instead of the code above you can also write something like:

function top_node_update($node) {

  $top = views_get_view_result('top_3');
  $first = $top[0]->nid;
  $second = $top[1]->nid;
  $third = $top[2]->nid;

  if ($first == $node->nid) {
    $img_name = 'gouden rozet.png';
  }

  if ($second == $node->nid) {
    $img_name = 'zilveren rozet.png';
  }

  if ($third == $node->nid) {
    $img_name = 'bronzen rozet.png';
  }

  $image = file_get_contents('public://images/badges/' . $img_name);
  $file  = file_save_data($image, 'public://images/' . $img_name, FILE_EXISTS_REPLACE);
  $node->field_badge[LANGUAGE_NONE]['0']['fid'] = $file->fid;
  field_attach_update('node', $node);

}

  • Although your code works for the first node in my view, a problem occurs when I use it for the second and third node in the view too. I thought it would be better to solve it here instead of starting a new question, so I've updated my question. Can you please take a look at it? – Jeroen Jan 31 '14 at 10:52
  • That's because the source location of the image is the same as the destination so it gets overwritten. Creating a source folder (public://images/badges/) will prevent this. Updated my answer – undersound Feb 1 '14 at 10:02
  • Yes, that's a very nice solution. It works now. Thanks a lot! – Jeroen Feb 1 '14 at 11:48

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.