Relationships currently have to define the base table and field they join to in their hook_views_data().

However, there are a number of use case where a relationship that has a variable right hand table is useful: eg, Flag module joining the {flagging} table to the entity base table for the entity type that's being flagged. #1694956: define a views relationship from {flag_content} towards entities.

The base table is set in the definition for the following reasons:

1. the query() method makes use of the definition
2. the UI to add fields/filters/etc uses this to compile a list of base tables on the view, to know which handlers to show
3. the UI for a handler's options uses this to compile a list of relationships on the view that the handler may be placed on

1 can be sidestepped by a custom relationship handler. 2 can be sidestepped by the handler class's init() method setting the right hand table and field in $this->definition.

3 however doesn't work, because the handler's init() is called slightly too late.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

joachim’s picture

Status: Active » Needs review
FileSize
913 bytes

With this patch, my custom relationship handler class can do this:

  function init(&$view, &$options) {
    $this->definition['base'] = 'node';
    $this->definition['base field'] = 'nid';


    parent::init($view, $options);
  }

and not need to set these in hook_views_data(). Thus in theory these could be picked up from the handler options.

joachim’s picture

It turns out that step 1 is not even necessary -- if the init() spoofs the base table and field, the query() is none the wiser and works as normal!

There's a working patch at #1694956: define a views relationship from {flag_content} towards entities that makes use of the patch here to provide a variable-base relationship from the {flagging} table to whichever entity the flag is on.

diwant’s picture

I came here from a Userpoints issue (http://drupal.org/node/1668498). I don't understand enough about views and userpoints to know if this is what that issue needs. Can someone who knows a bit more about views/userpoints shed some light please?

joachim’s picture

Issue summary: View changes

Updated issue summary.

Chris Matthews’s picture

Issue summary: View changes

The 6 year old patch to admin.inc applied cleanly to the latest views 7.x-3.x-dev and if still applicable needs to be reviewed.

Checking patch includes/admin.inc...
Hunk #1 succeeded at 4422 (offset 105 lines).
Applied patch includes/admin.inc cleanly.