Currently, When Ctools is building relationship contexts from fields, it assumes that the keys in the field schema will be the same as the entity id key from the entity it references. For example it assumes this:

  array(
    // ...
    'foreign keys' => array(
      'node_reference' => array(  // some join identifier
        'table' => 'node',  // entity table
        'columns' => array('nid' => 'nid'), // entity id key, relating field key
      ),
    ),
  );

However, the relating field key can be arbitrarily named something different (such would be the case for Entity Reference):

  array(
    // ...
    'foreign keys' => array(
      'node_reference' => array(  // some join identifier
        'table' => 'node',  // entity table
        'columns' => array('nid' => 'target_id'), // entity id key, relating field key
      ),
    ),
  );

The provided patch will use the foreign keys provided by the field to determine which key to reference for building the relationship context. I have tested this across custom field types with foreign keys, References module, and a patched copy of Entity Reference. #1340748: Add CTools relationship also depends on this.

Comments

helior’s picture

StatusFileSize
new5.21 KB

Patch

helior’s picture

StatusFileSize
new12.77 KB

As suggested by EclipseGc, I added a static cache in ctools_field_foreign_keys() for performance.
Also, it might seem like a bigger patch because I was able to remove an if statement and shift over a huge block of code over.

merlinofchaos’s picture

Status: Needs review » Needs work

I fear that my patching spree today has made this patch not apply.

Can you reroll this?

helior’s picture

Status: Needs work » Needs review
StatusFileSize
new12.72 KB

Here's an updated patch.

aspilicious’s picture

I'm not sure how much merlin cares about trailing whitespaces but this patch has some.

merlinofchaos’s picture

I do care about trailing whitespace but I can also convince my editor to remove it for me.

Still it's best if patches can be posted without it. I don't always remember to do it.

helior’s picture

Status: Needs review » Needs work

I'm curious as to what tools you both use to track white spaces. Regex? Some plugin or feature in your editors? Simply passing it through the Coder module?

aspilicious’s picture

Dreditor: http://drupal.org/project/dreditor
and any good text editor can strip whitespaces on save. (with some configuration)

helior’s picture

Status: Needs work » Needs review
StatusFileSize
new12.76 KB

Thanks for the tip aspilicious! :)
Hope this one is better.

helior’s picture

Oh, and in case anyone else is also curious about this, here's a macro I found for Textmate to remove white space on save. Handy!
http://mjijackson.com/2009/01/textmate-macro-remove-trailing-space-and-save

damien tournoud’s picture

Seems that this patch is related to #1418740: Entity from field relationship plugin should check the target column rather than the source column. I'm unclear on what the exact relationship between the two is.

fabsor’s picture

StatusFileSize
new12.21 KB

I'm coming from #1418740: Entity from field relationship plugin should check the target column rather than the source column, where I found another issue with the current implementation. I think both approaches needs to be incorporated in one patch to actually get this working properly (at least for me). The attached patch is a combination of #1418740 and the patch posted in #9, and it works well for me in NodeStream where we have a lot of entity reference fields.

karens’s picture

Status: Needs review » Reviewed & tested by the community

I tried this out using the latest development version of Entity Reference and the latest development version of CTools. Without this patch, there is no way to add a relationship for an entity reference to a Page Manager panel. With the patch the relationship shows up and works correctly.

henrijs.seso’s picture

KarenS is right. Tested this too.

merlinofchaos’s picture

Status: Reviewed & tested by the community » Fixed

Committed! Thanks for the work and the testing!

Status: Fixed » Closed (fixed)

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