0

I have a taxonomy vocabulary with 4 terms. One of the content types has a term reference field to tag a node with a single term from that vocabulary. Now, I want to have Views (3.5) generate a navigation block for me which would list the terms in the following way: skip item 4 and merge item 2 & 3 into a custom item labeled, say "Item 2 & 3", as in:

Item 1
Item 2 & 3  [linked accordingly: taxonomy/term/2+3]

Of course, with eventually 2 items in a block, I could do that statically, I know. However the vocabulary may grow and would like to have a scalable, dynamic solution. Hence my question: is that possible?

1
  • You might be able to do it using Panels and a Context View. So your nav block would be a panels region, and each chunk of results would be View Context > View rows panels content item with a rule to define the number of rows and offsets. I did a write-up explaining Context Views for this unrelated question, the instructions and 3rd bullet are the relevant parts. I'm not sure I understand what you're trying to do well enough to judge if it would be dynamic enough for you: you might need custom code using Views hooks. Commented Nov 13, 2012 at 23:47

1 Answer 1

1

I think the easier and simpler solution would be to create a custom block from your custom module using hook_block_info and hook_block_view. By using these hooks, your block will be listed in the block administration page - admin/structure/block.

<?php
    /*
     * Implementation of hook_block_info()
    */
    function mymodule_block_info() {
        $blocks['yourblock'] = array(
        'info' => t('Your Module Name'),
        'cache' => DRUPAL_NO_CACHE
        );
        return $blocks;
    }
    /*
     * Implementation of hook_block_view()
    */
    function mymodule_block_view($delta = '') {
        $block = array(); # Declare array

        switch ($delta) {
            case 'yourblock' :         
                // Block View
                $block['subject'] = 'Your Block Title';
                $block['content'] = _mymodule_taxonomy_content();
            }
        }
        return $block;
    }
    /*
     * Create block content as you need
    */    
    function _mymodule_taxonomy_content( ) {
        $result = $query = db_select('taxonomy_term_data', 't')
                            ->join('taxonomy_vocabulary', 'v', 't.vid = v.vid')
                            ->condition('v.machine_name', 'your_tag_machine_name')
                            ->orderBy('t.tid')
                            ->execute();    
        $content = '';
        foreach ($result as $term) {
            // you now have $term['vid'], $term['name'], etc. here
            // loop and build conditions here to suit your need
            // Item 1
            // Item 2 & 3  [linked accordingly: taxonomy/term/2+3] 
            // For example,
            // $content .= l($term['name'], 'taxonomy/term/'.$term['vid']); 
            // This is up to you.
        }

        return $content;
    }

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.