3

I'm running into limitations of views. For example, I would like to add additional logic and formatting to a view shown in a block. Is there a good way of taking the existing SQL and formatting from the view to create a custom module?

For example: I want to show a table with taxonomy terms, including their hierarchy. Parent terms will have links to one one, children terms will have links to a different view. For children terms I want to show the number of nodes with that term, the number of comments, etc. This can currently not be done using views or would be rather complex to setup.

Rather than starting from scratch writing a module is there a good way to use a similar view and reuse the query and the formatting?

1
  • Can you provide a more specific example of what you're trying to do?
    – nmc
    Commented Oct 13, 2011 at 0:22

3 Answers 3

4
+50

You could also use something like this:

$view = views_get_view('view_name');
$view->set_display('block_display_name'); 
$view->set_arguments(array($arg1, $arg2)); //only if you need arguments
$view->execute();
foreach($view->result as $res) {
  // extra stuff here
}
2
  • what should be put in $view->set_display('');? Is that the name of the display as it appears in the url when editing?
    – flickerfly
    Commented Oct 8, 2013 at 2:33
  • yes, that's what you need to put there.
    – Stefan
    Commented Oct 13, 2013 at 11:35
2

You can create a small module implementing hook_block() to create a block and as Stefan said you can then load and manipulate the view, and then call new views with arguments inside.

You create a first view that list the terms and a second that list the content based on the term.

function mymodule_block($op = 'list', $delta = 0, $edit = array()) {
  switch($op) {
    case 'list':
      $blocks = array();
      $blocks['myblock'] = array(
        'info' => 'My block administrative title',
        // other options
      );
      return $blocks;
    break;
    case 'view':
      $view = views_get_view('my_view_name'); // view id
      $view->set_display('block_1'); // select display name
      $view->set_arguments(array($arg1, $arg2)); // used to pass arguments to the view
      $view->execute(); // execute the view
      foreach($view->result as $res) { // you go through each of the view result (default to 10)
        // print your term, and load the second view and pass the term as argument, then execute the view.
      }
      return array(
        'subject' => 'Block title', // Block title
        'content' => $content // Your block content. 
      );
    break;
  }
}

Hope it will help!

2
  • Only one note here. In Drupal 7 you need to use 2 different hooks for blocks: hook_block_info() and hook_block_view()
    – Stefan
    Commented Oct 14, 2011 at 13:43
  • Could someone provide an example of this in Drupal 7?
    – flickerfly
    Commented Oct 8, 2013 at 1:28
2

Have you considered using a template file such as views-view-unformatted.tpl.php and do the login in there or possibly use a preprocess function?

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.