4

I want to be able to use a CCK field as my node body without affecting interoperability with Views generated by independent modules. Using hook_data_alter(), I have attempt to re-target the Views node body handler to look at the handler CCK has generated for my field, field_body. Unfortunately, I get some errors when I try to make use of my proposed solution.

/**
 * Implementation of hook_views_data_alter().
 */
function fieldbody_views_data_alter(&$data) {
  // When node_revisions table is added, join in content_field_body too.
  $data['node_revisions']['table']['join']['content_field_body'] = array(
    'left_field' => 'vid',
    'field' => 'vid',
  );

  // Use field_body handler info for node body.
  $data['node_revisions']['body'] =
    $data['node_data_field_body']['field_body_value'];

  // Really emphasize that we are using a different table.
  // Since this isn't doing anything, it may be redundant.
  $data['node_revisions']['body']['table'] = 'content_field_body';

  // A couple tweaks so it's a friendly Node Body replacement in the UI.
  $data['node_revisions']['body']['group'] = t('Node');
  $data['node_revisions']['body']['help'] =
    t('Full body text of the node.');
}

This code appears to work, but when I try to use the handler in practice, I get a warning:

user warning: Unknown column 'node_revisions.field_body_value' in 'field list' query: SELECT DISTINCT node.nid AS nid, node_revisions.field_body_value AS node_revisions_field_body_value, node_revisions.field_body_format AS node_revisions_field_body_format, node.type AS node_type, node.vid AS node_vid FROM node node LEFT JOIN node_revisions node_revisions ON node.vid = node_revisions.vid LIMIT 0, 10 in ../workspace/oa/profiles/openatrium/modules/contrib/views/includes/view.inc on line 771.

My interpretation of this is that the code is not pulling in the content_field_body table when the field is used, instead it just tries to use node_revisions.field_body_value.

Looking for any answers that resolve this problem or suggest a better strategy for my goal.

EDIT: Ugly Fallback Option

Use hook_views_default_views_alter() to scan every View for a defined body field, and swap it out for a standard CCK field. However, this will break all kinds of things, and not be very performance on cache clears.

1 Answer 1

1

As the database field you are using is external to the node_revisions table, you should probably declare a relationship, in the same way done from Views for the uid field.

// uid field
$data['node_revisions']['uid'] = array(
  'title' => t('User'),
  'help' => t('Relate a node revision to the user who created the revision.'),
  'relationship' => array(
    'handler' => 'views_handler_relationship',
    'base' => 'users',
    'base field' => 'uid',
    'label' => t('user'),
  ),
);
4
  • Unfortunately the goal is to have a View definition see no difference between using the standard Node Body or the CCK field. Run-time joins are fine, but defined relationships defeat the goal.
    – Grayside
    Mar 28, 2011 at 17:45
  • What I am suggesting is that the code is not working because the external field is not reported to be a relationship. I could be wrong, but a relationship forces Views to load the other table. In your case, Views doesn't know which is table containing the field you are using as node body.
    – apaderno
    Mar 28, 2011 at 17:57
  • I thought that was supposed to be covered by setting the 'table' element.
    – Grayside
    Mar 29, 2011 at 22:57
  • I can be wrong, but probably Views doesn't allow to have more than one table definition. The code I reported is the definition used by Views for the uid field. The module could have used the same code you are trying to use for your module, but it didn't; I think there is a reason, behind that.
    – apaderno
    Mar 30, 2011 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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