Steps to reproduce:

  • Create a content type with a link field (link-u) with linkit enabled and unlimited occurrences.
  • Add another link field (link-1) with linkit enabled and only 1 occurrence
  • Create a new content item
  • Click 'Add another item' for the link-u field
  • Click 'Search' for the link-1 field
  • Choose a node and click Insert Link
  • The link is inserted into the second occurrence of link-u, not the link field associated with the search button (link-1)

Analysis of problem:

The problem is in linkit.field.js, attach(). When the ajax call is made to 'Add another item', this function is executed again on the results of the ajax call. Based on what I know about the behavior and scoping rules regarding javascript closures, the code at line 27 should have captured the instance variable passed as a parameter on the $each call at line 15. What seems to be happening though, is that the instance variable is behaving like a C-style pointer and during the second call to attach() (as a result of the ajax call), the value captured by the link-1 click handler is having it's contents replaced with a new instance (the final added unlimited link field). When the click handler runs for the single link field Linkit search, it is using the wrong instance variable. This was confirmed by a number of console.log calls to watch what was happening.

My solution is to make a copy of the instance variable within the closure and use that in the click handler.

Tested on Chrome 51, Chrome Canary 54 and Firefox 47 and 47.0.1 on OSX.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dnmurray created an issue. See original summary.

dnmurray’s picture

dnmurray’s picture

Status: Active » Needs review
dnmurray’s picture

Fix patch filename.

dnmurray’s picture

  • anon committed 17b79ce on 7.x-3.x authored by dnmurray
    Issue #2766221 by dnmurray, anon: Search selection populating wrong...
anon’s picture

Status: Needs review » Fixed

Thanks for the patch.

Status: Fixed » Closed (fixed)

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