Problem/Motivation

When removing a field collection item via Delete link on entity view, method deleteHostEntityReference() unsets the delta value, but does not sorted again the deltas. IE: Having deltas [0, 1, 2, 3, 4, 5] and removing 1 and 3 via Delete link on entity view, results on deltas [0, 2 ,4 ,5]. Doing the same task via entity form, results on [0, 1, 2, 3].

Proposed resolution

To keep consistency and avoid messed delta values, delta values could be sorted in deleteHostEntityReference() after removal.

Remaining tasks

Patch
Review

User interface changes

None

API changes

None

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

plopesc’s picture

Issue summary: View changes
Status: Active » Needs review
FileSize
585 bytes

Attaching simple patch that sorts deltas before saving host entity after removal.

Regards.

plopesc’s picture

Bump.
This is a simple patch that improves module's consistency

Den Tweed’s picture

FileSize
862 bytes

This was for me a much needed patch as I needed the delta's of the field collection items in a view listing to make a #item/total items field. As the field collection entity code has now been moved to a separate file I've updated the patch to work with the most recent dev. Works as a charm!

Chris Matthews’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll, +Needs rework

The patch in #3 does not apply to the latest 7.x-1.x-dev and may be too old to reroll, but I went ahead and tagged the issue accordingly.

Checking patch field_collection.entity.inc...
error: while searching for:
    $delta = $this->delta();
    if ($this->item_id && isset($delta)) {
      unset($this->hostEntity->{$this->field_name}[$this->langcode][$delta]);
      // Do not save when the host entity is being deleted. See
      // field_collection_field_delete().
      if (empty($this->hostEntity->field_collection_deleting)) {

error: patch failed: field_collection.entity.inc:434
error: field_collection.entity.inc: patch does not apply
Den Tweed’s picture

Den Tweed’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll

New patch against latest release