How to get page categories in Typoscript (and use

2019-04-14 03:56发布

问题:

I would like to read out a page's system categories for further use with tx_news (to display news that have the same categories as the page - as tx_news is using system categories).

I was looking for a native solution, hopefully via getText, something like: plugin.tx_news.settings.categories.data = page:categories but that doesn't seem to exist yet

Also, I tried to simplify the query by using sys_category_records_mm, which contains all the information needed for that case, but TYPO3 complains that "there is no entry in the $TCA array":

lib.categoryUid = CONTENT
lib.categoryUid {
  wrap = kategorien:|
  table = sys_category_record_mm
  select {
    selectFields = uid
    where = uid_foreign = {TSFE:id}
    where.insertData = 1
  }
  renderObj = TEXT
  renderObj {
    field = uid
    wrap = |,
  }
}

So that would be nice, but it's not allowed.

回答1:

Here's a solution that works in my setup. The editor chooses categories for the page, and gets all news items that belong to the category.

temp.categoryUid = CONTENT
temp.categoryUid {
  table = pages
  select {
    // dontCheckPid doesn't exist for CONTENT objects, so make it recursive from root page (or pidInList.data = leveluid:-2
    pidInList = {$pidRoot}
    recursive = 99
    selectFields = sys_category.uid as catUid
    join = sys_category_record_mm ON pages.uid = sys_category_record_mm.uid_foreign JOIN sys_category ON sys_category.uid = sys_category_record_mm.uid_local
    where = sys_category_record_mm.tablenames = 'pages' AND sys_category_record_mm.uid_foreign = {TSFE:id}
    where.insertData = 1
    // not necessary for this use case
    // orderBy = sys_category.sorting
  }
  renderObj = TEXT
  renderObj {
    field = catUid
    // Hack: if there are no cats selected for a page, all news are displayed
    // so I just pass a catUid that's quite unlikely
    wrap = 999999,|,
  }
}


lib.newstest = USER
lib.newstest {
      userFunc = tx_extbase_core_bootstrap->run
      extensionName = News
      pluginName = Pi1
      switchableControllerActions {
            News {
              1 = list
            }
      }
      settings < plugin.tx_news.settings
      settings {
            limit = 5
            orderBy = datetime
            orderDirection = desc
            detailPid = {$pidNachrichtenDetail}
            overrideFlexformSettingsIfEmpty := addToList(detailPid)
            startingpoint = {$pidNachrichtenRecords}
            // for use in my fluid template
            // pluginTitle = {$llAktuell}
            // latest = 0
            // recordType = aktuell
            // https://forge.typo3.org/issues/52978
            useStdWrap = categories
            categories.override.cObject < temp.categoryUid
            categoryConjunction = or
      }

      view =< plugin.tx_news.view
}

What is unclear to me still is if recursive = 1 in the select has no setback. Actually, I don't want to check for the current page's parent uid at all, but WHERE pages.pid IN ({current pid}) is always inserted automatically. Therefore the recursive = 1.



回答2:

I found this example (in German) and modified it to display the category UIDs.

The following TypoScript will display the UIDs of all categories for the current page seperated by a comma.

10 = CONTENT
10 {
  table = pages
  select {
    uidInList.field = uid
    pidInList = 1 # UID or list of UIDs, where your categories are saved 
    selectFields = sys_category.uid as catUid
    join = sys_category_record_mm ON pages.uid = sys_category_record_mm.uid_foreign JOIN sys_category ON sys_category.uid = sys_category_record_mm.uid_local
    where = sys_category_record_mm.tablenames = 'pages' AND sys_category_record_mm.uid_foreign = {field:uid}
    where.insertData = 1
    orderBy = sys_category.sorting
  }
  renderObj = TEXT
  renderObj {
    field = catUid
    wrap = |,
  }
  # HACK
  # If category is empty, the mechanism below won't work
  # As long as I don't know how to query if this is empty or not,
  # just add an imaginary extra category!
  wrap = 12345,|
}

Unfortunately you have to set pidInList manually to a list of UIDs, where your categories are stored.



回答3:

Looking for the cat-id of a page: I did it this way:

lib.cat = CONTENT
lib.cat {
  wrap = |
  table = sys_category
  select {
     pidInList = 1
     selectFields  = sys_category.uid, sys_category.title
     max = 1
     join = sys_category_record_mm ON(sys_category_record_mm.uid_local = sys_category.uid)
     where = sys_category_record_mm.tablenames='pages'
     andWhere.dataWrap = sys_category_record_mm.uid_foreign = {TSFE:id}
  }
  renderObj = COA
  renderObj {
     10 = TEXT
     10 {
        field = uid
        wrap = class="category|
     }
     20 = TEXT
     20 {
        field = title
        case = lower
        stdWrap.noTrimWrap = | |"|
     }
  }
}