When I have updated my system to PHP 7, download count showed Never downloaded for all files even though they had a download count before.

I read log and found this:

Notice: Undefined property: stdClass::$Array in download_count_field_formatter_view() - line 31

and

Notice: Array to string conversion in download_count_field_formatter_view() - line 31

I fixed this by changing line 31 in file download_count.field.inc from

$item['downloads'] = db_query("SELECT COUNT(fid) from {download_count} where fid = :fid AND type = :type AND id = :id", array(':fid' => $item['fid'], ':type' => $entity_type, ':id' => $entity->$entity_info['entity keys']['id']))->fetchField();

to

$item['downloads'] = db_query("SELECT COUNT(fid) from {download_count} where fid = :fid AND type = :type AND id = :id", array(':fid' => $item['fid'], ':type' => $entity_type, ':id' => $entity->{$entity_info['entity keys']['id']}))->fetchField();

Now download count is shown, but I get another error:

Error: Call to undefined function fetchField() in download_count_field_formatter_view()

and

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'id' cannot be null: INSERT INTO {download_count} (fid, uid, type, id, ip_address, referrer, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6); Array ( [:db_insert_placeholder_0] => (censored) [:db_insert_placeholder_1] => 0 [:db_insert_placeholder_2] => node [:db_insert_placeholder_3] => [:db_insert_placeholder_4] => (censored) [:db_insert_placeholder_5]

I think the scope of the problem is bigger so could one of the developers try to use this module with PHP 7 so they can try to reproduce these bugs and possibly fix them.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Pylo created an issue. See original summary.

KlemenDEV’s picture

Issue summary: View changes
KlemenDEV’s picture

So after some more work, chagning line 191 of file download_count.module from

'id' => $entity->$entity_info['entity keys']['id'],

to

'id' => $entity->{$entity_info['entity keys']['id']},

removed these errors and now download count module works as it should with PHP 7. I need to do some more testing though. Could someone check if this (and previous changes) fixed the problem for them and post patch?

KlemenDEV’s picture

Issue summary: View changes
tamsoftware’s picture

Status: Active » Patch (to be ported)
Issue tags: +patch-to-be-reviewed
FileSize
1.81 KB

Pylo's code changes work here (PHP 7 and MySQL 5.7) so here is the patch.
Thanks !

DamienMcKenna’s picture

Version: 7.x-3.1 » 7.x-3.x-dev
Status: Patch (to be ported) » Needs review
Issue tags: -patch-to-be-reviewed +PHP 7.0 (duplicate)
Related issues: +#2798767: Entity id is NULL, cannot insert into download_count db table
FileSize
980 bytes

The first change is also fixed in #2798767: Entity id is NULL, cannot insert into download_count db table, so lets leave this one to focus on just the second change.

ydahi’s picture

patch in #5 worked for me on 7.x-3.1

RTBC. Should be committed.

kristofferwiklund’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
1.47 KB

We found another part that does not work with PHP 7.

Updated the patch

Gold’s picture

The patch at #8 includes the same fix as that described in "Entity id is NULL, cannot insert into download_count db table (2798767)" but this one spots the same sort of error in 2 locations.

I've closed that in favour of people bumping this one.

This is working for me and has been flagged RTBC for a while now.

Any chance of getting this merged in?

nithinkolekar’s picture

+1 RTBC

still error if file system is private

Sagar Ramgade’s picture

Status: Reviewed & tested by the community » Needs work
wmfinck’s picture

Please fix and commit this!

Gold’s picture

Hmm... Just retested this. I've created a node with 2 file fields. One in the private file system and one in the public. The private one is counting okay and the public one isn't. Will hopefully have some time in the morning to dig a little deeper.

Looking at issue #2977389 the public files are not counted anyway and xdebug does not take me into Drupal at all. From the point of view of counting private files the patch at #8 is working for me.

@nithinkolekar, can you give any more insight into the error you saw with the private file system? Any php error in Drupal? Anything in the server logs? Or just silently doesn't count it?

If you can't give more insight into your issue I'm inclined to set this back to RTBC.

@wmfinck, you could help the process by testing (and fixing) this yourself too. ;) Just saying...

nithinkolekar’s picture

@Gold

to reproduce follow these step.
1. Configure file field to have private dir
2. Set fields display settings format as "generic file with download count"
3. give View private files permission to anonymous.
4. upload file
5. try downloading the file as anonymous.
6. error in log : PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'fid' cannot be null: INSERT INTO {download_count}

Gold’s picture

Issue summary: View changes

Thanks @nithinkolekar.

I've followed what you described with the following exceptions;

Step 3: I don't have "View private files" as a permission.
Step 6: I don't see the error in my logs.

Can anyone else replicate this also?

@nithinkolekar, this would be happening in download_count_file_download_access_alter(), line 186 in download_count.module. Can you examine $file at that point and see what you can see?

Also, how are you standing up your environment? Are you using an existing site or standing up a clean site, checking out the module and patching it?

I'm standing mine up using a fresh download of Drupal via drush make which also checks out the module and patch being tested. The site is stood up like this for each test using drush $DRUSH_ROOT -y si standard.

Just looking at the patch at #8 again, I would also expect to see that error if the patch hadn't been applied. Can you double check that your download_count.module file has actually been patched correctly?

Gold’s picture

Status: Needs work » Reviewed & tested by the community

Being unable to replicate the issue with the patch applied and the lack of response I'm going to push this back to RTBC.

bubu’s picture

I can confirm that patch #8 works well with system files (.../system/files/...).

jweowu’s picture

Confirming the correctness of these PHP 7 compatibility fixes.

Refer to https://secure.php.net/manual/en/migration70.incompatible.php#migration7...

#8 should be committed.

iamweird’s picture

Patch #8 works good for me too.

Smesko’s picture

Thank you very much for all of you and your effort!!! :-)
This resolve big problem for us!

Patch #8 works good for us too! :-)

Best regards! :-)

joseph.olstad’s picture

Please commit

Sagar Ramgade’s picture

Status: Reviewed & tested by the community » Fixed
joseph.olstad’s picture

Thanks! I also see a new tagged release, double thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.