1

Edit: solved, see first reply for answer.

I am trying to write my views filter handler to extend the views_handler_field_term_node_tid class. I believe I have done everything correctly but I'm having no luck. In my module I have implemented hook_views_api()

/**
* Implements hook_views_api().
*/
function mymodule_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', mymodule') . '/views',
  );
}

Called the handler.inc file and the mymodule.views.inc in the module .info file:

files[] = views/mymodule.views.inc
files[] = views/mymodule_handler_field_term_node_tid.inc

This is the contents of that file:

class mymodule_handler_field_term_node_tid extends views_handler_field_term_node_tid {

  function option_definition() {
    $options = parent::option_definition();
    $options['convert_spaces'] = array('default' => FALSE, 'bool' => TRUE);

    return $options;
  }

  /**
   * Option to replace spaces with hyphens.
   */
  function options_form(&$form, &$form_state) {

    parent::options_form($form, $form_state);

    $form['convert_spaces'] = array(
      '#title' => t('Convert spaces in term names to hyphens'),
      '#description' => t('This allows links to work with Views taxonomy term arguments.'),
      '#type' => 'checkbox',
      '#default_value' => !empty($this->options['convert_spaces']),
    );

  }

  function render_item($count, $item) {
    return $item['name'];
  }

}

And finally I have implemented hook_views_data_alter() in the mymodule.views.inc

/**
 * Implements hook_views_data_alter()
 */
function mymodule_views_data_alter(&$data) {
  $data['node']['term_node_tid'] = array(
    'title' => t('Taxonomy terms on node'),
    'help' => t('Relate nodes to taxonomy terms, specifying which vocabulary or vocabularies to use. This relationship will cause duplicated records if there are multiple terms.'),
    'relationship' => array(
      'handler' => 'views_handler_relationship_node_term_data',
      'label' => t('term'),
      'base' => 'taxonomy_term_data',
    ),
      'field' => array(
      'title' => t('All taxonomy terms'),
      'help' => t('Display all taxonomy terms associated with a node from specified vocabularies.'),
      'handler' => 'mymodule_handler_field_term_node_tid',
      'no group by' => TRUE,
    ),
  );

}

So the weird issue is that running dpm($data); in my mymodule_views_data_alter function before I alter the $data['node']['term_node_tid'] brings up no reference to that data at all. That is being originally defined in taxonomy.views.inc and should be existing in the $data array because I have added the Content: All taxonomy terms field to my test view. I've been pulling out my hair for the last couple hours trying to figure this out with no luck.

1 Answer 1

0

You must adjust your module's weight so that it runs after views. Use hook_module_implements_alter or Modules weight

And then you can also change your code in MYMODULE.views.inc to something simpler:

function MYMODULE_views_data_alter(&$data) {
  $data['node']['term_node_tid']['field']['handler'] = 'MYMODULE_handler_field_term_node_tid';
}
1
  • Thank you so much, you were dead on about the weight thing. I guess that makes sense seeing as my module is extending an existing field/handler instead of creating my own from the module. It would have to execute after views to modify code from views. It's always something simple :) Thanks a million!
    – corning
    Commented Aug 13, 2014 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.