I can't seem to change the view mode of a taxonomy term when using my custom condition. When debugging the code it seems that at the point at with hook_entity_view_mode_alter is fired, the contexts are not active. Debugging the context through context ui shows me that the context is active and working correctly, but context_reaction.view_mode.inc does not ever get past $contexts = $this->get_contexts() in this module.

I have a custom condition that I am calling from the taxonomy term page in hook_entity_view that checks if the taxonomy term has children on not.

// Only fire on taxonomy term page belongs to.
if ($entity->tid == menu_get_object('taxonomy_term', 2)->tid && $plugin = context_get_plugin('condition', 'taxonomy_children')) {
    $plugin->execute($entity);
  }

This is the condition

class rawpixel_search_context_condition_taxonomy_children extends context_condition {
  /**
   * Returns a an array of optional values associated with a condition.
   *
   * This method allows site builders to further specify the meaning of a
   * condition that is being included in a context definition. In this example,
   * site builders are asked whether they want to select a condition that is
   * true when a node ID is odd, even, or "each." Typically the options
   * associated with a condition appear as checkboxes in the context editing
   * form.
   */
  function condition_values() {
    $values = array(
      'true' => 'True',
      'false' => 'False',
    );
    return $values;
  }

  /**
   * Specify the contexts in which this condition has been met.
   *
   * The execute() method is invoked by an "integration point"
   * within a Drupal hook. It invokes the condition_met() method for
   * each context that includes and meets its condition.
   */
  function execute($value) {
    $term_children = taxonomy_get_children(arg(2), $value->vid);
    if (!empty($term_children)) {
      foreach ($this->get_contexts('true') as $context) {
        $this->condition_met($context, 'true');
      }
    }
    else {
      foreach ($this->get_contexts('false') as $context) {
        $this->condition_met($context, 'false');
      }
    }
  }

}

I can only assume that the condition is not firing in time for this module to do it's thing. Is there another hook I could perhaps use, or could we look into using entity_view_alter, rather than entity_view_mode_alter, as I know that this hook fires in time and evaluates the condition correctly.

Comments

michaelmallett created an issue. See original summary.

sherakama’s picture

http://www.drupalcontrib.org/api/drupal/contributions!context!context.ap...

Without testing I would guess that hook_entity_view is fairly late in the game so your custom context would not have been evaluated yet. If you can I would use the hook linked above. It is one of the two default hooks with the context api. In order to ensure that your condition plugin is evaluated this module adds its own call to that hook.

Let me know if that works.

michaelmallett’s picture

That hook did indeed work, thanks for that.

I unfortunately now have an issue with it affecting any other entities on the page, but I will investigate and create a seperate issue.

michaelmallett’s picture

Status: Active » Closed (works as designed)