Strategy for learning to use Views in modules

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
ehowland's picture

I started learning views with the tutorials and the great api docs. I decided to follow the strategy suggested in a post that suggests using the user interface to create and then export the views structure. My problem is that I want to create a table view including data from one of my added tables.

These data do not show up in the list of fields to add. That makes sense since there is no place to add my table into the graphical UI. So I tried this strategy:

  1. Create a base view with only node fields (nid, title).

  2. Export this view.

  3. Add in a join statement to the exported view and then try to import the result back into the UI:

        $table['join'] => array(
          'left' => array(
            'table' => 'node',
            'field' => 'nid'
          ),
          'right' => array(
        'table' => 'node_volunteerop'
            'field' => 'nid'
          ),
        );

    That gives me a white screen of death. So I try the other interpretation of the API docs which suggests that node is implicit and always the left most element in the join.
        $table['join'] => array(

          'right' => array(
      'table' => 'node_volunteerop'
            'field' => 'nid'
          ),
        );

Same thing.

3b. I am thinking that maybe it does not know about my module so I try to alter the requires attribute i.e. $view->requires = array(node, volunteerop); but this gives me an error
You don't seem to have the following requirements: volunteerop
. I also try to add the name of the table not the name of the module, but that is not any better (even if I do not add a join).

Will some variant of this strategy work or is the UI only able to deal with CCK fields?

Comments

More hooks

Crell's picture

You can expose any node-related tables to Views, but you have to do so yourself with the hook_views_tables() hook. CCK provides that hook for CCK fields automatically, but if you're adding stuff to a node that is not a CCK field then you will have to implement your own hook_views_tables() hook in your module, otherwise Views won't know how your fields relate to the node in the first place. See the handbook for how that hook works. That holds true regardless of how you're defining the view. (Using the UI and then exporting it is the Right Way, IMO, so you're on the right track there. You're just missing step 1. :-) )

Cheers.

Thanks, but still not quite there.

ehowland's picture

Thanks, this solves my white screen of death problem. I was mixing in some of the keywords from hook_views_default_views() into hook_views_tables(). It was only when I also added a join commend that it killed the pages so I was over-focused on the join command. Your comment got me much farther along. I am still not, however, getting my fields into the UI. Here is my test hook_views_tables().

<?php
function volunteerop_views_tables() {
 
$tables['volunteerop_view'] = array(
   
"name" => "node_volunteerop",
   
"join" => array(
           
"type"=>"inner",
     
"left" => array(
       
"table" => "node",
       
"field" => "nid"
     
),
     
"right" => array(
       
"field" => "nid"
     
),
    ),
   
"fields" => array(
     
"dayweek" => array(
       
'name' => "dayweek",
       
'handler' => '',
       
'sortable' => true,
        ),

    ),
   
"sorts" => array(
    ),
   
"filters" => array(
    )
  );
 
       return
$tables;
}
?>

Now I need to know how to tie this table to the UI. I think this new table (after the join) is called volunteerop_view. I want the UI to get data from the table and put it in the dropdown box that includes the node fields.

Taking a working view (created in the UI that only includes fields from the base node) , exporting it and using it for a volunteerop_views_default_views() does not result in a listing either in the views admin interface nor am I successful in going straight to the exposed link.

And in the view created with the UI. If I export, change and reimport the view It works until I change this part of that view

<?php
     $view
->field = array (
        array (
         
'tablename' => 'node',
         
'field' => 'nid',
      
'label' => '',
        ),
          array (
            
'tablename' => 'node',
            
'field' => 'title',
            
'label' => '',
            
'handler' => 'views_handler_field_nodelink',
            
'options' => 'link',
    ),

     );
?>

by adding:

<?php
 
array (
   
'tablename' => 'volunteerop_view',
   
'field' => 'dayweek',
   
'label' => '',
   
'handler' => '',
  ),
?>

then it breaks the query perhaps because of not getting the requires field right. The behavior of the first post (3b) has not changed for either the name of the module (volunteerop) or the name of the table (volunteerop_view).

tutorials and the great api docs.

fatweasel's picture

I started learning views with the tutorials and the great api docs.

Which tutorials and great api docs?

I find views easy to put up something that sorts but very confusing to get to do exactly what one wants.

I created a content type which a call a "superwiki" (which is basically a wiki page about a term but has other rich-media type content like photos, videos, etc, associated with the term)

What I want to do should be simple ... if I go to mysite.com/superwiki it'll list all these node types

... since the key of a wiki is a term I want to set it up such that if i go to mysite.com/superwiki/term

i can get only the superwiki node that equals that term but also pull in all sorts of other content based on feeding the term from the url to a bunch of other modules that aggregate data based on that term

... is there a tutorial that explains how to build this into the superwiki node type or through views?

thanks

docs

Specifics

fatweasel's picture

Here is a concrete example of the problem. I created a "gene" type which is supposed to be a little editable wiki type for a gene.

Then a created a view called gene_terms

http://www.genesaurus.com/gene_terms

that displays them

the first gene there is KIF2C

but if I click on it then it goes to http://www.genesaurus.com/node/1031

what I want to do is set up the view or write directly in to the gene node type

that if I click on it then it goes to http://www.genesaurus.com/gene_terms/KIF2C

and it'll go to the little editable info on KIF2C but at the same time I can use the term KIF2C to pull in lots more info on KIF2C

... is there a tutorial on how to do this?

I suppose I could pull up KIF2C from node/1031 but that seems really ugly ... this doesn't seem like it should be hard but it's not clicking for me

Thanks

Views Developers

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week