Fluid access other array in for loop - typo3

I have to arrays like:
$business = array(0 => 'Car', 1 => 'IT');
$counts = array(0 => 15, 1 => 33);
I assign both array in my fluid template and iterate over the array business.
<f:for each="{business}" as="b" key="key">
<li>
<f:link.action action="business" arguments="{current_business: b.uid}">
<f:if condition="{counts.key} > 0">
<f:then>
{b.title} {counts.key}
</f:then>
<f:else>
{b.title}
</f:else>
</f:if>
</f:link.action>
</li>
</f:for>
I don't get an ouput for {counts.key}, should i access this an other way?

This can be done using the v:variable.get-ViewHelper from the extension vhs. Instead of {counts.key} use
{v:variable.get(name: 'counts.{key}')}
or
<v:variable.get name="counts.{key}"/>.

Of course you can do it as Jost showed by manipulating data within the view , it's perfectly valid, on the other hand probably combining arrays within your controllers into associative array(s) will be just more comfortable, like:
$business = array(
0 => array('title' => 'Car', 'count' => '15'),
1 => array('title' => 'IT', 'count' => '33'),
);
view
<f:for each="{business}" as="b">
<li>
<f:link.action action="business" arguments="{current_business: b.uid}">
<f:if condition="{b.count} > 0">
<f:then>
{b.title} ({b.count})
</f:then>
<f:else>
{b.title} (no items)
</f:else>
</f:if>
</f:link.action>
</li>
</f:for>
Note: If in real case that's a collection of model objects, you can just add transient - count field into your model - without TCA and SQL declarations - in this case you will be able to set values "on the fly" on this field within the controller and use them as common model field in the views but they won't be saved to DB.

Related

Why my DataProcessing\MenuProcessor don't show level-3 and level-4 for my page tree

I use Bootstrap_package v10 and Typo3 9, the menu processor doesn't show the level-3 and 4 for my pagetree.
I'm using the original templates from bootstrap package, the code is below:
10 = TYPO3\CMS\Frontend\DataProcessing\MenuProcessor
10 {
levels = 5
special = directory
special.value = 26969
expandAll = 1
includeSpacer = 1
as = mainnavigation
dataProcessing {
10 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor
10 {
references.fieldName = nav_icon
as = icon
if {
isTrue.stdWrap.cObject = COA
isTrue.stdWrap.cObject {
10 = TEXT
10.value = 1
10.if.isTrue = {$page.theme.navigation.icon.enable}
20 = TEXT
20.value = 1
20.if.isTrue = {$page.theme.navigation.dropdown.icon.enable}
}
}
}
}
}
and this is the fluid code :
<f:section name="MainNavigation">
<f:if condition="{menu}">
<ul class="navbar-nav">
<f:for each="{menu}" as="item">
<f:if condition="{item.spacer}">
<f:then>
</ul>
<ul class="navbar-nav">
</f:then>
<f:else>
<li class="nav-item{f:if(condition: item.active, then:' active')}{f:if(condition: item.children, then:' dropdown dropdown-hover')}">
<a href="{item.link}" id="nav-item-{item.data.uid}" class="nav-link{f:if(condition: item.children, then:' dropdown-toggle')}"{f:if(condition: item.target, then: ' target="{item.target}"')} title="{item.title}"{f:if(condition: item.children, then:' aria-haspopup="true" aria-expanded="false"')}>
<f:if condition="{theme.navigation.icon.enable} && {item.icon}">
<span class="nav-link-icon">
<f:if condition="{item.icon.0.extension} === svg">
<f:then>
<bk2k:inlineSvg image="{item.icon.0}" width="{theme.navigation.icon.width}" height="{theme.navigation.icon.height}" />
</f:then>
<f:else>
<f:image image="{item.icon.0}" alt="{item.icon.0.alternative}" title="{item.icon.0.title}" width="{theme.navigation.icon.width}" height="{theme.navigation.icon.height}" />
</f:else>
</f:if>
</span>
</f:if>
<span class="nav-link-text">{item.title}<f:if condition="{item.current}"> <span class="sr-only">(current)</span></f:if></span>
</a>
<f:if condition="{item.children}">
<ul class="dropdown-menu" aria-labelledby="nav-item-{item.data.uid}">
<f:for each="{item.children}" as="child">
<f:if condition="{child.spacer}">
<f:then>
<li class="dropdown-divider"></li>
</f:then>
<f:else>
<li>
<a href="{child.link}" class="dropdown-item{f:if(condition: child.active, then:' active')}"{f:if(condition: child.target, then: ' target="{child.target}"')} title="{child.title}">
<f:if condition="{theme.navigation.dropdown.icon.enable} && {child.icon}">
<span class="dropdown-icon">
<f:if condition="{child.icon.0.extension} === svg">
<f:then>
<bk2k:inlineSvg image="{child.icon.0}" width="{theme.navigation.dropdown.icon.width}" height="{theme.navigation.dropdown.icon.height}" />
</f:then>
<f:else>
<f:image image="{child.icon.0}" alt="{child.icon.0.alternative}" title="{child.icon.0.title}" width="{theme.navigation.dropdown.icon.width}" height="{theme.navigation.dropdown.icon.height}" />
</f:else>
</f:if>
</span>
</f:if>
<span class="dropdown-text">{child.title}<f:if condition="{child.current}"> <span class="sr-only">(current)</span></f:if></span>
</a>
</li>
</f:else>
</f:if>
</f:for>
</ul>
</f:if>
</li>
</f:else>
</f:if>
</f:for>
</ul>
</f:if>
</f:section>
The fluid is calling a page child but i don't know if it is recursive or not so it can showa all levels, what am I missing there, it seems like i'm the first one that having this issue ?
Thanks in advance for any help.
First you should verify if the data is available for more than the first two levels:
insert a <f:debug title="mainnavigation">{mainnavigation}</f:debug> in your template.
Then inspect your templates whether they are ready to display more than two levels.
I can imagine your templates show the first level, for second level a partial is called, but that partial does not call itself if necessary.
Except if you need some level specific markup (e.g. 'class="level1") you can build up menus by stacking the levels inside each other (giving stacked uls). So you either have a recursive call with stacked menus of the same markup or you define a partial for each level with individual markup (or you define a variable which contains the current level and call the partial recursive).
it's even worse: both levels are written out in the same template file, no partial (or section) is used.
I would change it to:
(I stayed with one file and instead of an additional partials I call a section, which can be in the same file)
<f:section name="MainNavigation">
<f:if condition="{menu}">
<ul class="navbar-nav">
<f:for each="{menu}" as="item">
<f:render section="subLevel" arguments="{item:item}" />
</f:for>
</ul>
</f:if>
</f:section>
<f:section name="subLevel">
<f:if condition="{item.spacer}">
<f:then>
<li class="dropdown-divider"></li>
</f:then>
<f:else>
<li class="nav-item{f:if(condition: item.active, then:' active')}{f:if(condition: item.children, then:' dropdown dropdown-hover')}">
<a href="{item.link}" id="nav-item-{item.data.uid}" class="nav-link{f:if(condition: item.children, then:' dropdown-toggle')}"{f:if(condition: item.target, then: ' target="{item.target}"')} title="{item.title}"{f:if(condition: item.children, then:' aria-haspopup="true" aria-expanded="false"')}>
<f:if condition="{theme.navigation.icon.enable} && {item.icon}">
<span class="nav-link-icon">
<f:if condition="{item.icon.0.extension} === svg">
<f:then>
<bk2k:inlineSvg image="{item.icon.0}" width="{theme.navigation.icon.width}" height="{theme.navigation.icon.height}" />
</f:then>
<f:else>
<f:image image="{item.icon.0}" alt="{item.icon.0.alternative}" title="{item.icon.0.title}" width="{theme.navigation.icon.width}" height="{theme.navigation.icon.height}" />
</f:else>
</f:if>
</span>
</f:if>
<span class="nav-link-text">{item.title}<f:if condition="{item.current}"> <span class="sr-only">(current)</span></f:if></span>
</a>
<f:if condition="{item.children}">
<ul class="dropdown-menu" aria-labelledby="nav-item-{item.data.uid}">
<f:for each="{item.children}" as="child">
<f:render section="subLevel" arguments="{item:child}" />
</for>
</ul>
</f:if>
</li>
</f:else>
</f:if>
</f:section>
Notice the changed markup for spacer in the first level!
Further changes might occur as I have not compared the code but concentrated on building up clean markup.
Increasing a 'parameter' for the recursion.
for an increasing value (level1, level2, level3...) you need a viewhelper in TYPO3 prior version 9:
this viewhelper can be realised in typoscript:
lib.math = TEXT
lib.math {
current = 1
prioriCalc = 1
}
then you can change the initial call to the SubLevel section to:
<f:render section="subLevel" arguments="{item:item,level:1}" />
Now you have a fluid variable level with the value 1.
the recursive call must be changed to:
<f:render section="subLevel" arguments="{item:child,level:{f:cObject(typoscriptObjectPath:'lib.math', data:'{level}+1')}}" />
for increasing values 2, 3, 4 ...
By default, MenuProcessor does not expand all levels. It will only show the branch you're on up to the level below the level you are on. If you want to show all levels for all branches you have to add expandAll = 1 to your configuration:
10 = TYPO3\CMS\Frontend\DataProcessing\MenuProcessor
10 {
levels = 5
expandAll = 1
...
}
Your Fluid template shows only 2 levels. By default in the Bootstrap package, you have a 2-level menu.
If you want to have more, you need to adjust the template to render more levels.
Here is the rendering of level 2 in your template:
<f:for each="{item.children}" as="child">
<f:render section="subLevel" arguments="{item:child}" />
</f:for>
If you want to render level 3, you need to go to the section subLevel and check if there are children of every item you set in arguments.
Example:
<f:if condition="{item.children}">
<ul>
<f:for each="{item.children}" as="level3Item">
...
</f:for>
</ul>
</f:if>
You should have some CSS for 5 levels of course. :)
I hope I could help you.
For levels beyond 2, I find it much easier to build in typoscript in your setup.typoscript.
lib.textmenu {
# We define the first level as text menu.
1 = TMENU
# The first level of this menu. 0 is rootline. -1 is 1 level up from rootline. 1 is 1 level down from rootline
entryLevel = 0
# UID's to exclude from the menu. Change this for each subsite, but will require a new template on each subsite.
excludeUidList = 21
# Turn spacers on, I use them to do sections of my menu.
1.SPC = 1
1.SPC.allWrap = <li class="a nav-item dropdown text-light bg-secondary SPC">|</li>
# Expand the whole menu - ie a sitemap
1.expAll = 1
# We define the normal state ("NO").
1.NO = 1
1.NO.wrap = <li class="nav-item NO">|</li>
1.NO.linkWrap = <li class="nav-item NO">|</li>
1.NO.ATagParams = class="nav-item dropdown NO"
# We define the active state ("ACT").
1.ACT = 1
1.ACT.wrapItemAndSub = <li class="nav-item active ACT">|</li>
# Get level 2 to get the format from level 1
2 <.1
# Do some customization for level 2
2.CUR = 1
2.CUR.wrapItemAndSub = <li class="nav-item dropdown active level2CUR">|</li>
2.CUR.ATagParams = class="active text-white"
# Make Levels 3 to 6 the same as level 1. I'll leave it to you to study all the typoscript wrapping.
3 < .1
4 < .1
5 < .1
6 < .1
}
Hello #Mohamed Masmoudi!
I am a newbie with TYPO3. I was asking myself the same question like you.
After a lot of research I found a solution which works for me (TYPO3 v10.4.12 and bootstrap-package) at least in desktop view mode.
But I don't understand what exactly is happening in mobile view. Maybe someone can give me a hint???
The sublevel is existing in source-code but is not showing up in mobile view.
First step was to modify the MenuProcessor in setup.typoscript
##########################
### DATA PREPROCESSING ###
##########################
dataProcessing {
1 = BK2K\BootstrapPackage\DataProcessing\ConstantsProcessor
1 {
as = theme
key = page.theme
}
10 = TYPO3\CMS\Frontend\DataProcessing\MenuProcessor
10 {
#modify these lines
levels = 5
expandAll = 1
includeSpacer = 1
as = mainnavigation
Second step modify the
/bootstrap_package/Resources/Private/Partials/Page/Navigation/Main.html
by adding the following lines to the fluid-template directly after the
<a href="{child.link}" class="dropdown-item{f:if(condition: child.active, then:' active')}"{f:if(condition: child.target, then: ' target="{child.target}"')} title="{child.title}">
...
</a>
here it comes:
<!-- 3rd Level -->
<ul class="dropdown-menu dropdown2-menu">
<f:for each="{child.children}" as="child2">
<li>
<a href="{child2.link}" class="dropdown-item{f:if(condition: child2.active, then:' active')}"{f:if(condition: child2.target, then: ' target="{child2.target}"')} title="{child2.title}">
<f:if condition="{theme.navigation.dropdown.icon.enable} && {child.icon}">
<span class="dropdown-icon">
<f:if condition="{child.icon.0.extension} === svg">
<f:then>
<bk2k:inlineSvg image="{child.icon.0}" width="{theme.navigation.dropdown.icon.width}" height="{theme.navigation.dropdown.icon.height}" />
</f:then>
<f:else>
<f:image additionalAttributes="{loading: 'lazy'}" image="{child.icon.0}" alt="{child.icon.0.alternative}" title="{child.icon.0.title}" width="{theme.navigation.dropdown.icon.width}" height="{theme.navigation.dropdown.icon.height}" />
</f:else>
</f:if>
</span>
</f:if>
<span class="dropdown2-text">{child2.title}<f:if condition="{child2.current}"> <span class="sr-only">(current)</span></f:if></span>
</a>
</li>
</f:for>
</ul>
<!-- End of 3rd Level -->
As you see I added a child2. You can add further sublevels like child3 etc by adding them after the particular <a>...</a>-tag
Don't forget to close all the open tags.
To separate the new sublevels by CSS I added a dropdown2-menu-Class to the tag.
Now you can add for example a .dropdown2-menu {margin-left: 50px} to your CSS.
Of course it is better not to touch the Bootstrap-Package. Instead use your own sitepackage. You can build one # https://sitepackagebuilder.com/
All of this is only to remember myself, what I was doing and maybe to help others... :-)

Add to the second position of a Fluid array

Currently I have this code that wraps every 2 items with <li></li>
<f:for each="{modules}" as="module" iteration="loop">
<f:cycle values="{0: 1, 1: 0}" as="header">
<li>
</f:cycle>
...stuff here...
<f:cycle values="{0: 0, 1: 1}" as="footer">
</li>
</f:cycle>
</f:for>
Lets say {modules} = [A,B,C,D] and output is something like this:
<li>AB</li>
<li>CD</li>
What I need to do is add custom html to the second position. So my desired output is:
<li>AX</li>
<li>BC</li>
<li>D</li>
How can I do this?
To your updated question I would say, you may want to solve this issue with your own Helper class before adding modules to the View through the Controller.
It is however possible to build it simple in the template:
Here is the documentation about the for ViewHelper.
For simplicity I would do the following:
<f:for each="{modules}" as="module" iteration="loop">
<f:if condition="{loop.isFirst}">
<f:then>
<li>
{module}
##extra_code##
</li>
</f:then>
<f:else>
<f:if condition="{loop.isEven}">
<li>
</f:if>
...stuff here...
<f:if condition="{loop.isOdd} || {loop.isLast}">
</li>
</f:if>
</f:else>
</f:if>
</f:for>
Note: The first element will be "odd" because isOdd takes the current interation (based on "cycle"), which starts with 1.
For the first element there is your own block. You can take the first module and write your own code. It will run only once.
From the second element to the last it will use the 2 element in one li logic. Here is only one interesting thing, that we need take isOdd OR isLast to always close the last li, even if you have an even number of elements.
You can do this like below.
<f:for each="{modules}" as="module" iteration="iteration">
<f:if condition="{iteration.isFirst}">
<f:then>
<li>
</f:then>
<f:else>
<f:if condition="{iteration.index}%2">
<f:then></f:then>
<f:else><li></f:else>
</f:if>
</f:else>
</f:if>
<!--
Here Your Content
-->
<f:if condition="{iteration.isLast}">
<f:then>
</li>
</f:then>
<f:else>
<f:if condition="{iteration.cycle}%2">
<f:then></f:then>
<f:else></li></f:else>
</f:if>
</f:else>
</f:if>
</f:for>

How to sort ViewHelper array?

I have the following template:
<f:for each="{matchday.matches}" as="match" iteration="matchIterator">
<f:if condition="{match.home_team._id} == {team}">
<f:render section="match" arguments="{match: match, is_home: 1, round_number: matchday.round_number}" />
</f:if>
<f:if condition="{match.away_team._id} == {team}">
<f:render section="match" arguments="{match: match, is_home: 0, round_number: matchday.round_number}" />
</f:if>
All entries are sorted by Id. matches has an attribute play_date which contains the correct date of the match.
How I can sort the array by date before output?
I already tried using SortViewHelper but it didn't work:
<f:for each="{matchday.matches->v:iterator.sort(order: 'ASC', sortBy:'play_date')}" as="match" iteration="matchIterator">

list items per property (here date) in the list view

i have a simple extbase extension with a model "item".
Each item has a title, a description and a date.
How can i list my items according to the date?
e.G.
24.06.17:
item 2
item 6
item 7
25.06.14:
item 1
...
i search the logic to realize this (in the view or in the controller)
In your code you can sort your record by date.
In fluid you can use the vhs viewhelpers set:
https://fluidtypo3.org/viewhelpers/vhs/master/Variable/SetViewHelper.html
Something likes (I don't test it):
<f:for each="items" as="item">
<f:if condition="{item.date}=={current}">
<f:then>
{item.title}
</f:then>
<f:else>
<br/>{item.date}: {item.title}
</f:else>
</f:if>
<v:variable.set name="current" value="{item.date}" />
</f:for>
You can use vhs viewhelpers like below.
<f:for each="{dataset -> vhs:iterator.sort(sortBy: 'date')}" as="item">
.........
.........
</f:for>

how to: find the length of a typoscript defined array in fluid?

I define an array in my extensions setup.txt typoscript like so:
settings{
halls {
0 = Default
1 = Mississauga
2 = Halifax
3 = Niagara Falls
4 = Oakville
5 = Pickering
}}
how in my fluid template can I find the length of the halls array? I want an if condition to do one thing if length is greater than 1 and another if not.
I am using typo3 v4.5.32 with extbase 1.3.
Thank you.
I tried this but it puts out two <th> tags instead of just one. The first if is meant to prove that there is more than one element, the next is meant to print out a <th> only if it is the first element.
<f:for each="{settings.halls}" as="hall" key="number" iteration="itemIteration">
<f:if condition="{itemIteration.isFirst != itemIteration.isLast}">
<f:if condition="{itemIteration.index == 0}">
<th>
<f:translate key="tx_bpscoupons_domain_model_coupon.hall" />
</th>
</f:if>
</f:if>
</f:for>
Why?
UPDATE: here is my work around for a lack of "is array length > 1" ferature in fluid
<f:for each="{settings.halls}" as="hall" key="number" iteration="itemIteration">
<f:if condition="{itemIteration.isFirst}">
<f:then>
<f:if condition="{itemIteration.isLast}">
// if both first and last there is only one so skip
</f:if>
</f:then>
<f:else>
<f:if condition="{itemIteration.isLast}">
//if there is more than one element in halls then eventually you'll get here once so print out th tags
<th>
<f:translate key="tx_bpscoupons_domain_model_coupon.hall" />
</th>
</f:if>
</f:else>
</f:if>
</f:for>
and to print out data:
<f:for each="{settings.halls}" as="hall" key="number" iteration="itemIteration">
<f:if condition="{itemIteration.isFirst}">
<f:then>
<f:if condition="{itemIteration.isLast}">
<f:then>
</f:then>
<f:else>
<f:if condition="{number} == {coupon.hall}">
<td>{hall}</td>
</f:if>
</f:else>
</f:if>
</f:then>
<f:else>
<f:if condition="{number} == {coupon.hall}">
<td>{hall}</td>
</f:if>
</f:else>
</f:if>
</f:for>
ugly but seems to work.
UPDATE 2 :
DOH!!! I missed total, just what I needed but I was looking for length or count, so this code is what I want:
<f:for each="{settings.halls}" as="hall" key="number" iteration="itemIteration">
<f:if condition="{itemIteration.total} > 1">
<f:if condition="{itemIteration.isLast}">
<th><f:translate key="tx_bpscoupons_domain_model_coupon.hall" /></th>
</f:if>
</f:if>
</f:for>
You can find a length of an array with the f:count ViewHelper. Just like other ViewHelper it can be used as an inline notation (You can find more here)
The correct code would be:
<f:if condition="{settings.halls -> f.count()} > 1">
<f:then>
//your code for then
</f:then>
<f:else>
//your code for else
</f:else>
</f:if>
<f:if condition="{f:count(subject: settings.halls)} > 1"> do something </f:if>
see update#2 above for the answer. to reiterate:
<f:for each="{settings.halls}" as="hall" key="number" iteration="itemIteration">
<f:if condition="{itemIteration.total} > 1">
<f:if condition="{itemIteration.isLast}">
<th><f:translate key="tx_bpscoupons_domain_model_coupon.hall" /></th>
</f:if>
</f:if>
</f:for>
For those of you who are looking for a custom viewhelper for this:
class ArrayCountViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
/**
* Get array count
*
* #param mixed $array
* #return string
*/
public function render($array) {
if (!is_array($array))
return 0;
return count($array);
}
}