This patch adds a relationship handler back to the node table when using terms as the base table and a filter handler for term names.

Patched against DRUPAL-6--3, also patches successfully against the 2.6 stable release.

Comments

merlinofchaos’s picture

Isn't this handler going to always pull up all revisions?

yhahn’s picture

No, because it joins back from term_node to node on vid not node_revisions on nid. Since the node table only stores the latest vid/nid pair, you only get single nodes/latest revisions.

merlinofchaos’s picture

This is only going to be true if the handler uses an INNER join. In the case of a LEFT join you're going to get ghosts for revisions, I think, where the NID will be NULL because it doesn't exist.

yhahn’s picture

Good point -- this would mean changing the join entry for term_node then:

  $data['term_node']['table']['join'] = array(
    ...
    // links directly to node via vid
    'node' => array(
      'left_field' => 'vid',
      'field' => 'vid',
+     'join' => 'INNER',
    ),
    ...
  );

Are there any scenarios when you'd want a LEFT JOIN here?

Let me know what you think and I can reroll the patch.

yhahn’s picture

StatusFileSize
new1.25 KB

Rerolled patch: fix for bad key 'field' (is now 'base field') in the relationship handler definition.

Regarding INNER vs LEFT join, the fact that this is a relationship handler means that the join type can be determined by the site builder by using the "Require this relationship" checkbox. In addition, if the site builder adds *any* filters against the node table using the relationship the "ghost" rows will be gone as well. I think it should be fine to leave this up to the user, but if it needs to be restricted we could always provide a custom relationship handler as well (that simply forces the 'required' option).

rsoden’s picture

Status: Needs review » Reviewed & tested by the community

This patch applies and works as expected.

thill_’s picture

This might be out of the scope of the patch,

This causes duplicates without a way to filter them.
My use case here is filtering out terms without NID's associated with them.

I added the new relationship, and added a filter to NID is not empty.

The filter worked but i am getting the terms replicated because there is more than one node per term.
I did try the Distinct option in the view but that didn't help.

Term A - Node 1
Term A - Node 2
Term B - Node 3
Term B - Node 4

AntiNSA’s picture

subscribe

AntiNSA’s picture

If you can tell me how to get that list and a specific nid,

There is the group by mod that can put them together and count...

It cant do all terms like:

Node1 : Vocab A Term A , Vocab A Term B
Node2: Vocab A Term A

But if you have:

Term
nid

you can group by nid and then count,

Giving you
Term A 2
Term B 2

The problem is how do you get this:
Node1 : Vocab A Term A , Vocab A Term B
to be
Node1: Vocab A Terma
Node1:Node1 : Vocab A Term A , Vocab A Term B

Also I dont know if counting the same nid would work or if they all have to be unique.

Search for "Group by" module...

If you figure this out please post back here.

Are you saying if I apply this patch I can get those results using the standard exposed filter and all terms under fields?

marcushenningsen’s picture

Just tested the patch. I can confirm that it works.

I suppose it is expected that applying the relationship makes rows for every node for which the terms exist.

For me the distinct option works, as long as I only enable taxonomy fields.

marcushenningsen’s picture

Btw, using Views 2.6.

And thanks for the patch.

marcushenningsen’s picture

Status: Reviewed & tested by the community » Active

Hm, I'm experiencing new issues with this patch with both versions of Views.

The view doesn't filter by node type, and distinct doesn't apply. In order to reproduce you need a vocabulary called 'tags' with some terms and two content types (of which one is called 'blog') and some nodes of both types tagged with some terms.

Views 3 export:

$view = new view;
$view->name = 'blog_terms';
$view->description = '';
$view->tag = 'blog';
$view->view_php = '';
$view->base_table = 'term_data';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('relationships', array(
  'vid' => array(
    'label' => 'node',
    'required' => 1,
    'id' => 'vid',
    'table' => 'term_node',
    'field' => 'vid',
    'relationship' => 'none',
  ),
));
$handler->override_option('fields', array(
  'name' => array(
    'label' => '',
    'alter' => array(
      'alter_text' => 0,
      'text' => '',
      'make_link' => 1,
      'path' => 'blog/tags/[name]',
      'link_class' => '',
      'alt' => '',
      'prefix' => '',
      'suffix' => '',
      'target' => '',
      'help' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'strip_tags' => 0,
      'html' => 0,
    ),
    'empty' => '',
    'hide_empty' => 0,
    'empty_zero' => 0,
    'link_to_taxonomy' => 0,
    'exclude' => 0,
    'id' => 'name',
    'table' => 'term_data',
    'field' => 'name',
    'relationship' => 'none',
  ),
  'node_count' => array(
    'label' => '',
    'alter' => array(
      'alter_text' => 1,
      'text' => '([node_count])',
      'make_link' => 0,
      'path' => '',
      'link_class' => '',
      'alt' => '',
      'prefix' => '',
      'suffix' => '',
      'help' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'strip_tags' => 0,
      'html' => 0,
    ),
    'set_precision' => FALSE,
    'precision' => 0,
    'decimal' => '.',
    'separator' => ',',
    'prefix' => '',
    'suffix' => '',
    'link_to_term' => 0,
    'exclude' => 0,
    'id' => 'node_count',
    'table' => 'term_node_count',
    'field' => 'node_count',
    'relationship' => 'none',
  ),
));
$handler->override_option('sorts', array(
  'node_count' => array(
    'order' => 'DESC',
    'id' => 'node_count',
    'table' => 'term_node_count',
    'field' => 'node_count',
    'relationship' => 'none',
  ),
  'name' => array(
    'order' => 'ASC',
    'id' => 'name',
    'table' => 'term_data',
    'field' => 'name',
    'relationship' => 'none',
  ),
));
$handler->override_option('filters', array(
  'vid' => array(
    'operator' => 'in',
    'value' => array(
      '1' => '1',
    ),
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'vid',
    'table' => 'term_data',
    'field' => 'vid',
    'relationship' => 'none',
  ),
  'node_count' => array(
    'operator' => '>',
    'value' => array(
      'value' => '0',
      'min' => '',
      'max' => '',
    ),
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'node_count',
    'table' => 'term_node_count',
    'field' => 'node_count',
    'override' => array(
      'button' => 'Override',
    ),
    'relationship' => 'none',
  ),
  'type' => array(
    'operator' => 'in',
    'value' => array(
      'blog' => 'blog',
    ),
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'type',
    'table' => 'node',
    'field' => 'type',
    'relationship' => 'vid',
  ),
));
$handler->override_option('access', array(
  'type' => 'none',
));
$handler->override_option('cache', array(
  'type' => 'none',
));
$handler->override_option('title', 'Blog Tags');
$handler->override_option('items_per_page', 20);
$handler->override_option('distinct', 1);
$handler->override_option('style_plugin', 'list');
$handler->override_option('style_options', array(
  'grouping' => '',
  'type' => 'ul',
));
$handler->override_option('row_options', array(
  'inline' => array(
    'name' => 'name',
    'node_count' => 'node_count',
    'title' => 'title',
  ),
  'separator' => '',
));
$handler = $view->new_display('block', 'Block', 'block_1');
$handler->override_option('block_description', 'Blog Tags');
$handler->override_option('block_caching', -1);
drewish’s picture

Status: Active » Needs review

restoring the status

merlinofchaos’s picture

#12: Um. You didn't state what issues you are experiencing. =(

merlinofchaos’s picture

Status: Needs review » Fixed

Ok, I played with this a little and I'm not sure what #12 was about, but filtering seems to work for me. I did change the wording of the help text on the relationship, which might be part of the problem because it says 'the node' suggesting it will only retrieve one. It will in fact relate all nodes with that term, which may or may not be what you want. It certainly is what I want sometimes, so this relationship is something that's been needed for awhile. Thanks for the patch, and I committed this to all branches.

marcushenningsen’s picture

Hm, I can't seem to remember myself, sorry about the vague description.

Status: Fixed » Closed (fixed)

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