How to limit showPrevNext in news to categories? - typo3

In the TYPO3 CMS 9.5.18 LTS with tx_news 8.3.0 we use the following extension Typoscript:
plugin.tx_news.settings{
# what is allowed to overwrite with TS
overrideFlexformSettingsIfEmpty := addToList(categories)
overrideFlexformSettingsIfEmpty := addToList(categoryConjunction)
# ids of categories
categories = 3
# category conjunction mode
categoryConjunction = or
}
I wonder why I have to add categories to overrideFlexformSettingsIfEmpty to get the result below. Never the less this post is more about how to achieve that the prev/next links (settings.detail.showPrevNext) does respect the category definition at all.
Our customer has 3 categories for news. If I go to a single page that does has a one category limitation (for the detail and the list page) I still e.g. can go "forward" to newer news in a total different category. However the list page only shows the news of that one selected category.
<f:if condition="{paginated.prev}">
<n:link newsItem="{paginated.prev}" settings="{settings}" class="ts-prev">
{paginated.prev.title}
</n:link>
</f:if>
Wasn't that never the case? Do I have to add some Typoscript or make a change in Fluid? The original code uses this settings variable as argument which contains the category limitation.

Okay I've had a look into the GeorgRinger\News\ViewHelpers\SimplePrevNextViewHelper and there aren't any limitation for the current chosen categories.
So here is what I did:
register a new optional argument categories to the viewhelper
add categories="{settings.categories}" to the simplePrevNext tag in the Detail.html
add an 'extra where' for the main query in the getNeighbours function
add the content for the additional where ( I did that first in the getNeighbours function )
Extra where:
if( is_array($newsOfCategory) && count($newsOfCategory) > 0 ){
$extraWhere[] = $queryBuilder->expr()->in('uid', $queryBuilder->createNamedParameter($newsOfCategory, Connection::PARAM_INT_ARRAY));
}
Content for the additional where:
if( is_string($categories) && preg_match('/^[0-9]+(,[0-9]+)*$/',$categories) ){
$categories = explode(',', $categories);
$tmpCon = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_category_record_mm');
$tmpQB = $tmpCon->createQueryBuilder();
$rows = $tmpQB
->select('uid_foreign')
->from('sys_category_record_mm')
->where(
$tmpQB->expr()->in('uid_local', $tmpQB->createNamedParameter($categories, Connection::PARAM_INT_ARRAY))
)
->andWhere(
$tmpQB->expr()->like('tablenames', $tmpQB->createNamedParameter('tx_news_domain_model_news'))
)
->execute()->fetchAll();
if( is_array($rows) && count($rows) > 0 ){
foreach($rows as $row){
$newsOfCategory[] = $row['uid_foreign'];
}
}
}
May be someone can use that in the future or the will integrate that to the repository.

Related

Get tx_news fal_media via TypoScript to use in fluid page template

I want to use the first image out of a new entry to include in the page header.
Therefore i need the news fal_media data in TypoScript to pass it to my page fluid template.
I managed to get the first image as a file path by this TypoScript code:
lib.newsimage = FILES
lib.newsimage {
references {
table = tx_news_domain_model_news
uid.data = GP:tx_news_pi1|news
fieldName = fal_media
}
maxItems = 1
renderObj = IMG_RESOURCE
renderObj.file {
import.data = file:current:publicUrl
}
stdWrap.ifEmpty = {$settings.ext.news.imageDummy}
}
But is there a better way to do it, especially to get the data as an array of FileReferences? With a FAL FileReference i could also take crop an meta data into account.
You might use a FilesProcessor
example based on the manual:
10 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor
10 {
as = news_images
references {
fieldName = fal_media
table = tx_news_domain_model_news
}
sorting = datetime
sorting.direction = descending
}
Although I can't imagine that this will be the correct way as there is no guarantee that the first news you select by this is the first news shown on the page. News have multiple conditions to be shown aside of basic visibility: ordering by publishing time, archived state, top-news state, groups or categories, ...
Maybe it would be easier if you use a news plugin with a special template and don't forget to enable showing of news multiple times (by default each news gets shown only once in a page)

TYPO3: configure available types for tx_news content element relation

Is it possible to configure which content element types are available for the content elements relation in tx_news records?
I have a backend layout which allows only gridelements in tt_content. The regular content elements are only allowed within a grid elements container.
Currently I face the problem, that in news records it's only possible to add grid elements:
I'd like to allow only some specific content elements in news records.
Update:
The above mentioned restriction is done via TSConfig for the BackendLayout:
mod.web_layout.BackendLayouts {
1 {
title = Standardseite
config {
backend_layout {
colCount = 1
rowCount = 1
rows {
1 {
columns {
1 {
name = Content
colPos = 0
allowed = gridelements_pi1
}
}
}
}
}
}
}
}
When I remove the line allowed = gridelements_pi1, all content element types are available again.
But regardless of the allowed setting for backend layouts I'd like to have just a small subset of content element types available for news records.
Sometimes the solution can be so simple and obvious! Thx for hint, Georg Ringer!
Just override the settings for the news sys folder:
[45 in tree.rootLineIds]
# this changes the allowed CTypes. Add more as a comma separated list
mod.web_layout.BackendLayouts.1.config.backend_layout.rows.1.columns.1.allowed = textmedia
# this sets the default CType to prevent an error with INVALID VALUE ("text")
TCAdefaults.tt_content.CType = textmedia
[global]
By the way, an even better solution would be to use TCEFORM.tt_content.CType.removeItem = ..., but this would require to update the list each time you add a new CType.

TYPO3/Typoscript : render sql query as array

I am using TYPO3 6.2.
On my website, i make a SQL query this way :
lib.bloc_top = COA
lib.bloc_top.10 < styles.content.get
lib.bloc_top.10.select.selectFields = header
lib.bloc_top.10.select.where = ( deleted = 0 && hidden = 0 && tx_gridelements_container = 2571 && CType = 'header' )
All works but instead of outputting the results as HTML code, I would like to render it in an array that I would use in my FLUID template this way :
<f:for each="{car}" as="el">
<li>Brand : {el}</li>
</f:for>
Is it possible ?
Thanks for your help :)
You should not select the container by a specific UID, but create a "car" container type via Gridelements CE backend layout instead.
Anyway, to get the data into an array, you don't have to do anything special, since this is automatically done by styles.content.get under the hood.
While styles.content.get uses the default tt_content setup, you can change that via renderObj as described here: https://docs.typo3.org/typo3cms/TyposcriptReference/6.2/ContentObjects/Content/
To get the data into your Fluid template you just have to replace the default renderObj with a FLUIDTEMPLATE https://docs.typo3.org/typo3cms/TyposcriptReference/6.2/ContentObjects/Fluidtemplate/Index.html
lib.bloc_top.10.renderObj = FLUIDTEMPLATE
lib.bloc_top.10.renderObj {
file = path/to/your/template/file.html
}
Since the loop is handled by the CONTENT object of styles.content.get, you can skip the f:for part in your template though.
Usually any kind of data is provided within the cObj->data array, so something like <h1>{data.header}</h1> should do the job.
And even with CONTENT there should be a counter, since there is https://docs.typo3.org/typo3cms/TyposcriptReference/DataTypes/Gettext/Index.html#cobj
To get any available data you should use <f:debug>{_all}</f:debug> in your Fluid template.

How to set TYPO3 7 metatags from TypoScript RECORDS?

I'm trying to override the page.meta.og:title in TYPO3 7.5 with a TypoScript RECORDS object.
The following TypoScript snippet does not seem to work unfortunately:
[globalVar = GP:tx_myext_pi1|article > 0]
temp.newsTitle = RECORDS
temp.newsTitle {
dontCheckPid = 1
tables = tx_myext_domain_model_article
source.data = GP:tx_myext_pi1|article
source.intval = 1
conf.tx_myext_domain_model_article = TEXT
conf.tx_myext_domain_model_article {
stdWrap.field = title
stdWrap.wrap = |
}
}
# Overrides the template pageTitle
page.10.variables.pageTitle >
page.10.variables.pageTitle < temp.newsTitle
# Overrides the meta og:title
page.meta.og:title >
page.meta.og:title < temp.newsTitle
[global]
I get:
<meta name="og:title" content="RECORDS">
While the override of the page title works for me.
Are there any ways to achieve this with TypoScript?
RECORDS only works within a cObject.
https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/Records/Index.html
[globalVar = GP:tx_myext_pi1|article > 0]
page.meta{
og:title {
attribute = property
stdWrap.cObject = RECORDS
stdWrap.cObject {
source = {GP:tx_myext_pi1|article}
source.insertData = 1
tables = tx_myext_domain_model_article
conf.tx_myext_domain_model_article= TEXT
conf.tx_myext_domain_model_article.field = title
}
}
[global]
While it's your ext what prevents you from adding it directly within action instead of manipulating with some weird TS? ;)
public function showAction($article) {
$ogTitle = trim(htmlentities($article->getTitle()));
$GLOBALS['TSFE']->getPageRenderer()->addMetaTag('<meta name="og:title" content="' . $ogTitle . '">');
// ... rest of action
}
Also take a look to Georg Ringer's News extension to see how he uses metaTagViewHeplper (actually he's doing the same but in VH) - you can use it to collect other og tags on the view like og:image and others.
Edit:
(More about preventing duplicated entries)
Keep in mind that duplicated meta combinations it's not a bug, it's a feature according to OGP Arrays spec ;) Actually fact that you can not declare two same meta tags with TypoScript is a bug, reason? TypoScript is not a programming language, it's just configuration table (array to be strict). As we know in PHP in associative array later key overrides earlier. While we're in topic of og:* metas we need to remember that sometimes they are repeated per page and it's perfectly valid, i.e: og:image.
You as a programmer has much more power within your action than in TS, even if you are using some ready-to-use ext which fills og:title from the pages records, nothing prevents you from discarding it with ... simple trick in TS, in your TS add a condition:
[globalVar = GP:tx_myext_pi1|article > 0]
page.meta.og:title >
[end]
and then make sure that you're adding it in your showAction as showed at the beginning.
Finally this way you do not need to make expensive lookup from TS site for each model that has a single view (believe me I know what that means)
BTW, I agree that there should be solid API for this, but I wrote some ext for one of my project for these things, that was matter of hours not even days, if I'll find it, I'll publish it to TER.
I was to quick with posting my question :)
It could be achieved by using the register, here is a example:
[globalVar = GP:tx_myext_pi1|article > 0]
page.9 = LOAD_REGISTER
page.9 {
newsTitle.cObject = RECORDS
newsTitle.cObject {
dontCheckPid = 1
tables = tx_myext_domain_model_article
source.data = GP:tx_myext_pi1|article
source.intval = 1
conf.tx_myext_domain_model_article = TEXT
conf.tx_myext_domain_model_article {
stdWrap.field = title
stdWrap.wrap = |
}
}
}
page.10.variables.pageTitle >
page.10.variables.pageTitle = TEXT
page.10.variables.pageTitle {
data = register:newsTitle
}
page.meta.og:title >
page.meta.og:title {
attribute = property
override.data = register:newsTitle
}
[global]

How to assign two instances of the same plugins to different markers in a template

I need to insert two instances of tt_news on the same page and assign each instance of the plugin to a marker in the template. The two instances would be inserted in a backend column not rendered by my template, like border.
Since inserted plugins have a visible id on the backend, I was thinking about something like this
page.10.marks.NEWS1 < plugin.tt_news.idxxx
page.10.marks.NEWS2 < plugin.tt_news.idyyy
How can I achieve this ?
Hm... You could either insert content elements at the marker, like so
temp.contentElement = RECORDS
temp.contentElement {
tables = tt_content
dontCheckPid = 1 # if necessary
}
page.10.marks.NEWS1 < temp.contentElement
page.10.marks.NEWS1.source = UID_OF_NEWS_PLUGIN_ELEMENT
or, you could simply completely configure the plugin in ts
temp.news1 < plugin.tt_news
temp.news1 {
# configure
}
temp.news2 < plugin.tt_news
temp.news2 {
# configure
}
page.10.marks.NEWS1 < temp.news1
Hope that helps