Context module is great!!! WOW!!!
I love Context!!!

Here you have integration with taxonomy: context_taxonomy.module

<?php

// $Id$

/**
 * @file
 * Intergration with taxonomy
 */ 

/*
 * Module sponsored by...
 *   "Ingenia: Ingeniería e Integración avanzadas"
 *   http://www.ingenia.es
 */



/**
 * Implementation of hook_context_conditions(). 
 */
function context_taxonomy_context_conditions() {
  $items = array();

  // We fetch the list of vocabularies sort by weight for checking.
  $vocabularies = array();
  $result_taxonomies = db_query('SELECT vid, name FROM {vocabulary}');
  while ($vocabulary = db_fetch_array($result_taxonomies)) {
    $vocabularies[$vocabulary['vid']] = $vocabulary['name'];
  }
  $items['taxonomy'] = array(
    '#title'        => t('Taxonomy terms'),
    '#type'            => 'select',
    '#key_type'        => 'custom',
    '#options'         => array('NONE' => "-- ". t('None') ." --"),
    '#default_value'   => array('0' => 'NONE'),
    '#multiple'        => TRUE,
    '#multiple_toggle' => TRUE,
    '#description'  => t('Set this context when viewing a node published in terms of taxonomy or using the add/edit form of one of these nodes published in tems of taxonomy.'),
  );

  // We add terms of all taxonomies to the form's select.
  $result_terms_found = db_query('SELECT tid, vid, name FROM {term_data} ORDER BY vid');
  while ($term = db_fetch_array($result_terms_found)) {
    if (!isset($vocabularies[$term['vid']])) {
      drupal_set_message('Error. Exists a term without vocabulary asociated: '. $term['tid'], 'error');
    }
    $items['taxonomy']['#options'][$term['tid']] = $term['name'];
  }
  return $items;
}



/**
 * Element validate handler for setter textareas and texfields.
 */
function context_taxonomy_condition_text_validate($element, &$form_state) {
  // We not make validation (for now)...
}

/**
 * Implementation of hook_nodeapi().
 */
function context_taxonomy_nodeapi(&$node, $op, $teaser, $page) {  
  if ($op == 'view' && $page && menu_get_object() === $node) {    
    foreach ($node->taxonomy as $value) {    
      // We notify to context module: "node with this condition".
      // Context will activate the context if exist this condition in some context.
      context_set_by_condition('taxonomy', $value->tid);      
    }
  }   
}

I promise convert this module to order as "blocks in context"!

Comments

jim kirkpatrick’s picture

Status: Active » Needs review

JC Denton, THANK YOU!

This is a Great addition which works really well -- Please jmiccolis and yhahn add this functionality in soon as there's a lot of taxonomy-based websites and online stores that need this functionality!

It needs a minor tweak though: the list of taxonomy terms to select in taxonomy_context_conditions doesn't currently show the hierarchy or current order, which makes selecting the right one tough when you have the same names under different parent terms. Indeed, terms from different vocabularies are listed together too. Just putting some dashes in front of the term name to show hierarchy depth would solve that issue. I might even have a go myself when I get more time next week.

Apart from that, I've done some testing and it looks good so far. Thank you again!

Jim

jcdenton.dm’s picture

Ok. Thanks!!!

I am going to work in this change!!!

ddorian’s picture

yes im using it in my devel machine

mattiasj’s picture

I tried this out and it really filled a gap, and perhaps soon i can only rely on context instead of context+menutrails. A feature request would be to extend this module with context vocabulary because in some cases (a large news site with tons of free tagged terms) it would be great to choose a vocab instead.

jennycita’s picture

+1

mani.atico’s picture

Hey, great addon. It should include vocabularies and term pages too.

indytechcook’s picture

StatusFileSize
new1.8 KB

This is a little bit of a different approach. You can select a vocab or terms or both.

Cheers,
Neil

mattiasj’s picture

Awesome, will try #7 out asap.

pebosi’s picture

i'm using #7's patch and its working well for me. Thanks!

cknoebel’s picture

The #7 patch is working for me also. Very nice.

I changed '#type' => 'select' to '#type' => 'checkboxes' to make deselecting terms clearer.

Thanks for a terrific module and nifty patch.

johnpitcairn’s picture

Nice patch, working well. I have a fairly roll-my-own solution providing features similar to the Domain module, using the Virtual Site module and assigning nodes to domains by taxonomy. This patch allows me to define contexts per domain. Thanks. Hope this makes it into an official release.

bohz’s picture

Thanks for the great work!
Tested both (module and patch in #7) and they work well.
Is there any chance to port this to context 3?
Alas, i'm not a coder, but the implementation of conditions in context 3.x seems to me quite different than in 2.x.
I mean, I tried but without any success. :(
It would be really great to have taxonomy-based layouts.

Cheers

jmiccolis’s picture

Status: Needs review » Needs work

I'm a bit worried about adding this functionality generally because of the vastly different ways the taxonomy system is used from site to site. On some sites terms are like another content type (supported by things like NAT) and on others they capture a site's hierarchical structure.

Technically however, my main concern is on sites with very large free tagging vocabularies. On such site presenting all terms in a single list isn't useful (and occasionally not possible!). If something like this goes into the module we'd need to support such sites.

johnpitcairn’s picture

Good point ... maybe this needs to be presented as a separate module, with its own admin page that lets you select which taxonomy vocabularies will be presented as context conditions?

Then I think it's up to the site dev/admin to determine whether to enable the module, depending on their specific taxonomy implementation.

bohz’s picture

I understand and agree that the range of potential uses of taxonomy system goes far beyond the scope of this module.
And also that there may be some situations where taxonomy-based contexts may be unpractical or even "dangerous" (even though it's really up to the developer to make the right choices with such a flexible framework).

Nevertheless, at least from my limited experience, taxonomy is often used to define "where you are in the site/content" that is, to some extent, a semantic analogue of context.
I mean, to me the most natural way to divide a drupal site into sections is to use taxonomy. The problem with this is that it's difficult to control; with context it would be much much easier. Not to mention the possibility to define layouts on the fly.

In my opinion this would be a great additional feature for context even if limited to single hierarchy vocabularies or even to a single, dedicated vocabulary to define context conditions.
A separate module would be OK too.
Or maybe integrate taxonomy with context through spaces?

Anyway, thanks so much for this (already) wonderful module and for the great work with many others!

Cheers

jmiccolis’s picture

@johnpitcairn & @bohz - It sounds like we're all on the same page regarding the issue here. One quick note: spaces does already support taxonomy integration. However I can see why that could be both overkill for some sites & not enough for others. I do think we should keep this discussion going and see what we could do to make this possible.

The idea of having a setting which controls which terms / vocabularies are exposed to the context UI is an ok starting point, but it I'd like to hear out other approaches as well.

johnpitcairn’s picture

Alternative: maybe this has already been discussed (?) but an approach that would allow integration with taxonomy and much else might be to provide a custom php condition field, similar to the blocks module. If the user-entered php returns true, the condition is met.

Downside: it would require coding skills from the user, might require the use of eval(), and user-entered php is pretty infamous for breaking sites...

indytechcook’s picture

I'd be happy to create a new module for this or add a little js/ajax to the interface to allow for selection the vocab first. It's not that much code and a new module might be over kill.

As far as context 3, I haven't even touched it yet ;) It looks OO so probably very easy to extend.

I just made great use of extending feeds objects and being that these are both dev seed using ctools, it should be cake.

Give me some direction on where you would like me to spend my time: a new module, update the UI or port to context 3.

Cheers,
Neil

bohz’s picture

@#16: Thanks, I wasn't aware of spaces_taxonomy. i'm giving it a test.
For other approaches, I may think of "modes" where the user might choose among 1 of:
- dedicated vocab mode: one "special" vocabulary controls taxonomy contexts (each term has a different context; but what to do for terms that do not have own context set up?);
- vocab mode: each vocabulary has a different context (shared by all of its terms);
- term mode: each term can have its own context, independently from the vocab and from other terms (user should enter, rather than select, the term or tid in the UI);
- term queue mode (?): arbitrarily configured group of terms can share the same context;
don't know if any of these makes sense, though. I'm not an expert at all!

@#17: from a mere "configurator" point of view I don't think the PHP code-only approach would be very appealing. MHO, of course! ;)

@#18: subscribe!!

Cheers

yhahn’s picture

Version: 6.x-2.0-beta7 » 6.x-3.0-alpha2
Status: Needs work » Fixed

Port of this patch and test committed to 3.x: http://drupal.org/cvs?commit=326378

Status: Fixed » Closed (fixed)

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