1

I'm granting an avatar to the users on my website, based on their gender (cf. this answer). I've been able to achieve this both programatically and with 3 Rules.

This is my custom module:

function custom_user_presave (&$edit, $account) {
    $tid = $edit['field_geslacht']['und'][0]['target_id'];
    if ($account->is_new OR !isset ($account->roles[3])) {
        $avatar = taxonomy_term_load($tid);
        }
        else {
            $avatar = taxonomy_term_load(8);
        }
    $edit['field_avatar']['und'][0]['fid'] = $avatar->field_avatar['und'][0]['fid'];
    $edit['field_avatar']['und'][0]['width'] = $avatar->field_avatar['und'][0]['width'];
    $edit['field_avatar']['und'][0]['height'] = $avatar->field_avatar['und'][0]['height'];
    $edit['field_avatar']['und'][0]['alt'] = $avatar->field_avatar['und'][0]['alt'];
    $edit['field_avatar']['und'][0]['title'] = $avatar->field_avatar['und'][0]['title'];
}

This is one of my exported rules (the second only differs in the action part and the third one checks whether the user has a certain role):

{ "rules_avatar" : {
    "LABEL" : "Avatar",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "avatar" ],
    "REQUIRES" : [ "rules" ],
    "ON" : { "user_insert" : [], "user_update" : [] },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "account" ], "field" : "field_geslacht" } },
      { "data_is" : { "data" : [ "account:field-geslacht" ], "value" : "Man" } }
    ],
    "DO" : [
      { "data_set" : { "data" : [ "account:field-avatar:file" ], "value" : "35" } }
    ]
  }
}

I'm wondering which method is the fastest/most efficient. Can you help me?

  • Next time, you just need to edit your original question when it is on hold (see meta.stackexchange.com/questions/10582/…). I am going to delete the other one. – mpdonadio Mar 6 '14 at 13:40
  • @MPD: Okay, I was about to do that too. :) I don't like to edit questions on hold too much, because most of the time, they aren't reopened again. – Jeroen Mar 6 '14 at 13:42
0

I wouldn't focus too much on speed in situations like this because it's extensibility you will care about as the site grows in function. While your custom code is probably "faster" it's not the measure you should be deciding by since it'll be nanoseconds you're talking about and any good caching strategy will nullify most of that concern.

But to answer your question, your code would likely be faster especially if you're not defining as many hooks as the Rules module.

However, I'd advise using rules for the following reasons.

  • Rules are exportable
  • Rules are integrated with all the "big" modules out there.
  • Never program something you can borrow. :)

Hope this helps.

  • Thanks, that will certainly help! I'd like to see some more opinions, but I'll upvote your answer it the meantime. – Jeroen Mar 6 '14 at 13:38
  • No problem. Happy to contribute! Thanks for the upvote. :) – webkenny Mar 6 '14 at 13:39
  • 1
    I agree with your answer in general, but the the fact that "Rules are exportable" is not a reason to choose Rules over custom code. A custom module doesn't even need to be exported, since it's code already, and deploying a custom module is usually even easier than export-and-feature-revert. – marcvangend Mar 6 '14 at 13:43
  • I'm a best practices snob but you're not wrong, for sure. – webkenny Mar 6 '14 at 13:44
  • @marcvangend: Feel free to post a different answer if you don't agree! :) – Jeroen Apr 3 '14 at 20:53

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.