How to create a dynamic submenu via a userfunction in TypoScript

Written by Johannes on January 27, 2012 Categories: TYPO3 Tags: , , ,

Your ads will be inserted here by

Easy AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

Let’s assume we have an extension, e.g. a news extension my_news that provides a list view of entries that can be filtered via categories via the query index.php?id=listViewID&tx_my_news['category']=categoryID. In our navigation we want to create a submenu under the list view page, so that our navigation looks like this:

  • Home
  • News
    • Category 1
    • Category 2
    • Category 3
  • About us
  • etc…

We can achieve this effect using stdWrap.cObject = CASE on our TMENU_ITEM:

lib.nav = HMENU
lib.nav {
#this is all basic knowledge to you
  entryLevel = 0
  special = directory
  special.value = 1
  1 = TMENU
  1 {
    wrap = <ul>|</ul>
    expAll = 1
    NO = 1
    NO.wrapItemAndSub = <li>|</li>
    ACT < .NO
  }
 
  2 < .1
  2 {
#from hereon it gets interesting
    wrap = <ul>|</ul>
    NO {
      stdWrap.cObject = CASE
      stdWrap.cObject {
        key.field = uid
 
#default: render default menu
        default = TEXT
        default {
          field = title
          typolink.parameter.field = uid
        }
 
#overwrite menu for news categories
        news = HMENU
        news {
          special = userfunction
          special.userFunc = Tx_MyNews_UserFuncs->news_menu
          special.targetPid = {$listViewID}
 
          1 = TMENU
          1 {
            NO = 1
            NO.ATagTitle.field = title
 
            ACT = 1
            ACT < .NO
          }
          2 < .1
          2 {
            wrap = <ul>|</ul>
            NO = 1
            NO.wrapItemAndSub = <li>|</li>
            ACT < .NO
          }
        }
      }
    }
    ACT < .NO
  }
}
 
# stdWrap.cObject = CASE requires hardcoded PID... so inherit the template here for configuration ###
lib.nav.2.NO.stdWrap.cObject.235 < lib.nav.2.NO.stdWrap.cObject.news
lib.nav.2.ACT < lib.nav.2.NO

The userfunction looks like this

class Tx_MyNews_UserFuncs {
  var $cObj;
 
  /**
  * Returns an array to render an HMENU for the news categories
  *
  * @param   string $content
  * @param   array $conf TypoScript configuration array
  * @return  array news categories menu
  */
  function news_menu ($content,$conf) {
    $GLOBALS['TYPO3_DB']->store_lastBuiltQuery = 1;
 
    // prepare db statements
    $table = 'tx_mynews_domain_model_category';
    $fields = 'title, uid';
    $where = '';
    $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($fields, $table, $where);
    $title = $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery;
 
    $category_menu = array( );
 
    while ( ($category = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) ) {
      // build category link
      $linkConf = array (
        'useCacheHash' => true,
        'parameter' => $conf['targetPid'],
        'additionalParams' => '&tx_my_news[category]=' . $category['uid']
      );
      $link = $this->cObj->typolink_URL( $linkConf );
 
      // add to menu
      $category_menu[] = array (
        'title' => $category['title'],
        '_OVERRIDE_HREF' => $link
      );
    } 
 
    $link = $this->cObj->typolink_URL( array( 'parameter' => $conf['targetPid'] ) );
    return array ( 
      array ( 
        'title' => 'News',
        '_OVERRIDE_HREF' => $link,
        '_SUB_MENU' => $category_menu
      ) 
    );
  }
}
3 Comments

3 Comments

Leave a Reply to Bernhard Eckl Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre user="" computer="" escaped="">