Problem/Motivation

When running PHP 7.2, the each() function is now deprecated as per the php.net migration notes for 7.2:

https://secure.php.net/manual/en/migration72.deprecated.php#migration72....

Deprecation notice raised:

Deprecated function: The each() function is deprecated. This message will be suppressed on further calls in FieldCollectionItemEntity->fetchHostDetails() (line 302 of sites/all/modules/field_collection/field_collection.entity.inc).

Proposed resolution

In field_collection.entity.inc, replace:

list($this->hostEntityType, $data) = each($result);

with:

$this->hostEntityType = key($result);
$data = current($result);

Arcane note: technically, if we wanted to keep absolute parity with the code we're replacing, we would use next() rather than current() since the call we're replacing, each(), advances the internal array pointer, which next() does but current() does not. However, the array is not used further, meaning that advancing the array pointer is an incidental byproduct of using each() rather than something we need to retain, and using next() for no reason would only be confusing.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

reallifedigital created an issue. See original summary.

barry_fisher’s picture

Status: Active » Needs review
StatusFileSize
new564 bytes

Patch attached for review.

bkosborne’s picture

Issue tags: +PHP 7.1, +PHP 7.0 (duplicate)
bkosborne’s picture

Category: Bug report » Task
Status: Needs review » Reviewed & tested by the community
Issue tags: -@deprecated, -PHP 7.1 +PHP 7.2

Looks good to me

bkosborne’s picture

Here's a version of the patch that applies cleanly against 7.x-1.0-beta12. The original patch is the one that should be committed. Has a .txt extension which I think prevents the tests from running.

mrgoodfellow’s picture

After applying this patch and when editing a draft of a new node I am getting the following error:

Notice: Undefined index: in FieldCollectionItemEntity->fetchHostDetails() (line 394 of /data/drupal/sites/all/modules/contrib/field_collection/field_collection.module).

any ideas?

czigor’s picture

StatusFileSize
new562 bytes

Just a reroll for latest dev.

herved’s picture

StatusFileSize
new1.82 KB

I can confirm the notice reported by mrgoodfellow when applying patch #5 on 7.x-1.0-beta12.
Latest dev added https://cgit.drupalcode.org/field_collection/commit/?h=7.x-1.x&id=c1f704... from #2382089: Logic issue with fetchHostDetails() causes false "the host entity cannot be changed" error in updateHostEntity() which adds a check on empty results.

Patch #7 looks good to me with latest dev, but I think the logic in there could be simplified a bit.
Here's an attempt, let me know if that adds any value.

herved’s picture

Status: Reviewed & tested by the community » Needs review

Status: Needs review » Needs work

The last submitted patch, 8: field_collection-2936874-php72-8.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

herved’s picture

Status: Needs work » Needs review
StatusFileSize
new1.82 KB

Arg.. small confusion there.
This should work.

heyyo’s picture

Thanks for the patch. To make it work with issue https://www.drupal.org/project/field_collection/issues/2833061
I had to keep the other modifications from the other patch:

        if ($result) {
          $this->hostEntityType = key($result);
          $data = current($result);
          end($data);

          // The entity ID or revision ID if we are querying for revisions.
          $data_array_keys = array_keys($data);
          $id = key($data);
          $this->hostEntityId = $data ? $data_array_keys : FALSE;
          $this->hostEntityRevisionId = $in_use ? FALSE : $id;
        }
adam1’s picture

Dear @heyyo, I am not very savvy in coding, so: how can I apply your combination? Is your above if-statement the combined version and I can just replace the code in the original module with this statement?

mrgoodfellow’s picture

Patch from #11 is working well with my testing.

Do we have a timeline for this update to be included with a PHP 7.2 compatible release?

lamp5’s picture

Patch from #11 works well. Thanks.

nedjo’s picture

Issue summary: View changes
StatusFileSize
new548 bytes
new525 bytes

Changes were introduced in #8 and #11 with the following comment:

Patch #7 looks good to me with latest dev, but I think the logic in there could be simplified a bit.
Here's an attempt, let me know if that adds any value.

To answer the question of whether this change adds value: it may be true that the logic of the existing code could be improved, but if so that should be done in a follow-up issue as it's unrelated to the bug here. Introducing such changes here make it a lot more difficult to determine whether the patch successfully resolves the reported bug without introducing new ones. Here, we should do the minimum needed to update the deprecated code.

Attaching an updated patch. The only change from the version in #7 is to use current() alongside key(). The interdiff is relevant to the patch in #7.

Arcane note: technically, if we wanted to keep absolute parity with the code we're replacing, we would use next() rather than current() since the call we're replacing, each(), advances the internal array pointer, which next() does but current() does not. However, the array is not used further, meaning that advancing the array pointer is an incidental byproduct of using each() rather than something we need to retain, and using next() rather than current() for no reason would only be confusing.

nedjo’s picture

kevineinarsson’s picture

Status: Needs review » Reviewed & tested by the community

#16 works. Marking this issue as RTBC.

samuel.mortenson’s picture

StatusFileSize
new571 bytes

Here's a re-roll of #16 for the beta12 tag.

sickness29’s picture

Re-roll of #16 for the beta11 tag.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 20: field_collection-php72-beta11-2936874-20.patch, failed testing. View results

jacob.embree’s picture

Status: Needs work » Reviewed & tested by the community

Resetting to RTBC in accordance with #18. The following two patches are not against the latest dev.

Patches not against the latest dev should be marked "Do not test" from the select list next to the file name before upload.

max lovric’s picture

StatusFileSize
new731 bytes

Here is patch for 7.x-1.0-beta11 version :)

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 23: field_collection-each-2936874-23.patch, failed testing. View results

sjerdo’s picture

Status: Needs work » Reviewed & tested by the community

Resetting to RTBC per #16. The patch in #23 is not against the latest dev and equals the patch in #20.

@Max Lovric please read the comments, since the patch was already supplied. Also your patch uses an incorrect base.

mrgoodfellow’s picture

In order to get all count() and each() errors resolved on this module for PHP 7.2 Compatibility I had to apply both patches:
field_collection-2936874-php72-11.patch.txt

as well as: field_collection-php72-countable-2992575-17.patch.txt

from: https://www.drupal.org/project/field_collection/issues/2992575

  • nedjo committed d5456f3 on 7.x-1.x authored by Max Lovric
    Issue #2936874 by samuel.mortenson, Max Lovric, sickness29: PHP7.2 -...
nedjo’s picture

Status: Reviewed & tested by the community » Fixed

Thanks, applied!

Status: Fixed » Closed (fixed)

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

Joe Schwartz’s picture

So, can someone please point me to the final Drupal 7 PHP 7.2 patch?