The image does not get attached in a profile2 when a new user account is created. The account profile page had to be visited and resaved in order that the image could be loaded and shown. Can it be fixed?

Comments

drusite created an issue. See original summary.

miechiel’s picture

Status: Active » Needs review
StatusFileSize
new4.92 KB

I created a patch that could solve this issue. In the old version the module was using the hook_field_attach_submit() which means that the a form with images must be submitted. I changed this to work with the hook_entity_presave().

On a clean Drupal install with profile2 when a user is being created the profile2 random image fields are being created as well.

I also made sure the files are being copied to the proper directory so that a user can not (per accident) delete an image from the random image field folder.

drusite’s picture

I applied this patch and there are some errors
Notice: Undefined variable: type in function image_field_random_field_attach_submit() (string 55 in file /sites/all/modules/image_field_random/image_field_random.module).
Notice: Undefined variable: type in function image_field_random_field_attach_submit() (string 59 in file /sites/all/modules/image_field_random/image_field_random.module).

They are related to $entity_info = entity_get_info($type); and $field_instances_info = field_info_instances($type, $type);
Is it normal that there are two $type ?

drusite’s picture

When a node is submitted, these errors appear (Advanced Forum content, maybe it is true for all content types).

danielphenry’s picture

Status: Needs review » Needs work

So I think the new hooks are probably ok.

But by why did you duplicate the file instead of just adding a file usage like the code previously did? Duplicating the file wastes server space and is less performant than just adding an entry to the file usage table.

  1. Imagine you have 20 random files and attach them to 500 nodes. That's 520 images instead of 20. Then when image cache gets to them it will cache each of the 500 separately instead of using the same cache entry for all. That 1020 images instead of 40 for just the first entry. Thats an additional file per node for every cache entry. This could get out of hand real fast and just isn't necessary.
  2. As far as performance duplicating a file is not much but it is certainly heavier than a database call. And since each node has a different file it the image cache is much less likely to be present.

Did you have a good reason for this change? I'm open to hearing it but it's got to justify the above mentioned issues. I could see this as an optional setting in case someone wants to use image cache_actions on the files or something like that but certainly not the default behavior.

  • miechiel authored 1920ee9 on 7.x-1.x
    Issue #2696517 by miechiel: Update Profile2
    
danielphenry’s picture

Status: Needs work » Fixed

I took a hard look at your patch and most of the changes are unnecessary or a step backwards.

  1. You deleted comments that are still applicable to the code. - Please don't do that. I like comments.
  2. You removed support for the is_empty function. Not sure if image field uses this but other module might. it's just a good thing to have.
  3. You copied the file instead of taking advantage of the file_usage table (see explanation above).
  4. You renamed a variable unnecessarily making the diff twice as big as it needed to be. - Just kind of annoying.

I've discarded all of these changes.

I have applied these changes:

  1. Change from hook_field_attach_submit to hook_entity_presave - excellent solution. Tested, works great. Thanks.
  2. Change file->status = 1 to FILE_STATUS_PERMANENT - can't believe I missed this one. Thanks.
  3. While the is_empty should not have been removed the empty tust was not being evaluated. I mixed in your fix to the existing code more cleanly. Good catch.

This fix is checked into the 7.x-1.x. Someone try it out and confirm it works before I spin up a bata2 release.

drusite’s picture

Where is 7.x-1.x version? I verified https://www.drupal.org/node/1913234/release and it doesn't exist there.

danielphenry’s picture

Sorry never rolled out a release you would just have to check out the branch directly from the git repo: https://www.drupal.org/project/image_field_random/git-instructions

I'll try to get a dev release rolled up.

danielphenry’s picture

The latest dev release now has this patch in it. Please let me know if it works out for you.

danielphenry’s picture

Status: Fixed » Closed (fixed)

closing properly