{"id":1037,"date":"2022-08-30T15:11:20","date_gmt":"2022-08-30T15:11:20","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/pointing-the-views-handler-for-node-body-at-another-field-handler-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:11:20","modified_gmt":"2022-08-30T15:11:20","slug":"pointing-the-views-handler-for-node-body-at-another-field-handler-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/pointing-the-views-handler-for-node-body-at-another-field-handler-collection-of-common-programming-errors\/","title":{"rendered":"Pointing the Views handler for Node Body at another field handler-Collection of common programming errors"},"content":{"rendered":"<p>I want to be able to use a CCK field as my node body without affecting interoperability with Views generated by independent modules. Using <code>hook_data_alter()<\/code>, I have attempt to re-target the Views node body handler to look at the handler CCK has generated for my field, <code>field_body<\/code>. Unfortunately, I get some errors when I try to make use of my proposed solution.<\/p>\n<pre><code>\/**\n * Implementation of hook_views_data_alter().\n *\/\nfunction fieldbody_views_data_alter(&amp;$data) {\n  \/\/ When node_revisions table is added, join in content_field_body too.\n  $data['node_revisions']['table']['join']['content_field_body'] = array(\n    'left_field' =&gt; 'vid',\n    'field' =&gt; 'vid',\n  );\n\n  \/\/ Use field_body handler info for node body.\n  $data['node_revisions']['body'] =\n    $data['node_data_field_body']['field_body_value'];\n\n  \/\/ Really emphasize that we are using a different table.\n  \/\/ Since this isn't doing anything, it may be redundant.\n  $data['node_revisions']['body']['table'] = 'content_field_body';\n\n  \/\/ A couple tweaks so it's a friendly Node Body replacement in the UI.\n  $data['node_revisions']['body']['group'] = t('Node');\n  $data['node_revisions']['body']['help'] =\n    t('Full body text of the node.');\n}\n<\/code><\/pre>\n<p><strong>This code appears to work, but when I try to use the handler in practice, I get a warning:<\/strong><\/p>\n<pre><code>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.\n<\/code><\/pre>\n<p>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.<\/p>\n<p>Looking for any answers that resolve this problem or suggest a better strategy for my goal.<\/p>\n<p><strong>EDIT: Ugly Fallback Option<\/strong><\/p>\n<p>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.<\/p>\n<ol>\n<li>\n<p>As the database field you are using is external to the <code>node_revisions<\/code> table, you should probably declare a relationship, in the same way done from Views for the <code>uid<\/code> field.<\/p>\n<pre><code>\/\/ uid field\n$data['node_revisions']['uid'] = array(\n  'title' =&gt; t('User'),\n  'help' =&gt; t('Relate a node revision to the user who created the revision.'),\n  'relationship' =&gt; array(\n    'handler' =&gt; 'views_handler_relationship',\n    'base' =&gt; 'users',\n    'base field' =&gt; 'uid',\n    'label' =&gt; t('user'),\n  ),\n);\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:17:07. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1037","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1037","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1037"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1037\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}