[TYPO3-dev] Creating a custom HMENU from userfunc with item states CUR / ACT

Lina Wolf liste at linawolf.de
Mon Jul 19 10:12:08 CEST 2010


I created a custom menu showing tt_news_cat. This works fine. The item 
states do not seem to work properly though.
If I set 'CUR' as state for the current category then it will not react to 
'ACT' anymore. Same is true for the diverse IFSUB states. It seems like in 
the custom userfunc menu the item states only work by name and the 
fallbacksystem for item states does not work. The only fallback that seems 
to work is the fallback to 'NO' state.

Code:
/**
 * Creates an HMENU from tt_news cat
 * @ params $content string the content to be changed
 * @ params $conf array configuration
 *  startUid int uid of the category witch should be root of the menu
 *  sourcePid int pid where the news categories are saved
 *  sortBy string clause to be used in ORDER BY statement
 *  recursive 1 / 0 if set menu is traversed recursively. Otherwise only one 
level to be more performant
 *  catPid int pid to be used for the standardised generated links
 * @params $startUid int default 0, to be used for recursive calls
 * @ returns HMENU ready nested array with states NO and ACT
 */
  function makeMenuArray($content,$conf, $startUid=0)    {
   if(!$this->cObj)
    $this->cObj = t3lib_div::makeInstance('tslib_cObj');
  $this->newsEnableFields = $this->cObj->enableFields('tt_news');
  $this->catEnableFields  = $this->cObj->enableFields('tt_news_cat');
  $currentCatId = t3lib_div::_GP('tx_ttnews');
  $currentCatId = intval($currentCatId['cat']);
   if($startUid==0)
    $startUid=intval($conf['startUid']);
   $filterBySourcePid = '';
   if($conf['sourcePid'])
    $filterBySourcePid = ' AND tt_news_cat.pid = 
"'.intval($conf['sourcePid']).'"';
   $sortByField = '';
   if($conf['sortBy'])
    $sortByField = ' ORDER BY '.$conf['sortBy'];
  $sql = 'SELECT tt_news_cat.*, count(tt_news.uid) AS news_count FROM 
tt_news_cat
   LEFT JOIN tt_news_cat_mm ON (tt_news_cat.uid = 
tt_news_cat_mm.uid_foreign )
   LEFT JOIN tt_news ON (tt_news.uid = tt_news_cat_mm.uid_local 
'.$this->cObj->enableFields('tt_news').')
   WHERE parent_category="'.$startUid.'" 
'.$this->cObj->enableFields('tt_news_cat').$filterBySourcePid.
   ' GROUP BY tt_news_cat.uid '.$sortByField;
  #t3lib_div::debug($sql);
  $res = mysql(TYPO3_db,  $sql);
  $catMenu = array();
  while($row = mysql_fetch_assoc($res)) {
   $state = 'NO';
   $hasSub = '';
   $subMenu = array();
   if($conf['recursive'])
    $subMenu = $this->makeMenuArray($content,$conf, $row['uid']);
   $countRec = $row['news_count'];
   foreach($subMenu AS $value) {
    if($value['ITEM_STATE'] != 'NO' && $value['ITEM_STATE'] != 'IFSUB')
     $state = 'ACT';
    $countRec += $value['news_count'];
   }
   // If 'CUR' is used here 'ACT' would not work anymore
   if($currentCatId == $row['uid'])
    $state = 'ACT';
   $linkConf = array(
    'parameter' => $conf['catPid'],
    'additionalParams' => '&tx_ttnews[cat]='.$row['uid'],
    'useCacheHash' => '1',
    'returnLast' => 'url',
   );
   $catItem = $row;
   $catItem['news_count_rec'] = $countRec;
   $catItem['_OVERRIDE_HREF'] =  $this->cObj->typolink("", $linkConf);
   $catItem['_ADD_GETVARS'] = '&tt_news[cat]='.$row['uid'];
   $catItem['_SUB_MENU'] =  $subMenu;
   $catItem['ITEM_STATE'] =  $state;
   if($conf['showEmpty'] || $countRec > 0){
    $catMenu[] = $catItem;
   }
  }
  return $catMenu;
  }

TypoScript:
page.10.marks.CATEGORIES = COA
page.10.marks.CATEGORIES {
  20 = HMENU
  20 {
    special = userfunction
    special {
      userFunc = tx_myExtCatMenuUserFunc->makeMenuArray
      catPid = 26
      sourcePid = 6
      sortBy = tt_news_cat.uid ASC
      showEmpty = 1
      recursive = 1
      startUid = 0
    }
    wrap = ...
    1 = TMENU
    1 {
      NO {
        allWrap = <div class="level1">|</div>
        linkWrap = <div class="news-catmenu-NO">|</div>
        ATagTitle.field = description
        ATagTitle.htmlSpecialChars = 1
      }
      ACT < .NO
      ACT = 1
      ACT.linkWrap = <div class="news-catmenu-ACT">|</div>
      # Fallback from CUR to ACT does not work...
     # CUR = 1
     # CUR.linkWrap = <div class="news-catmenu-CUR">CUR|</div>
    }
    2 ...
}







More information about the TYPO3-dev mailing list