(Sub)-categories affix stacked sidebar in Bootstrap - symfony

I am trying to implement a navbar affix in Symfony2 with Bootstrap, but I have not managed to find any useful resources so far.
What I would like to do is having some Categories and SubCategories and when the user clicks on a Category, then the available SubCategories are popping up.
Initial state example:
+Category 1
+Category 2
+Category 3
+Category 4
After clicking on Category 1 link example:
+Category 1
-SubCategory 1
-SubCategory 2
-SubCategory 3
+Category 2
+Category 3
+Category 4
What I have so far is:
<div role="tabpanel" class="tab-pane" id="profile">
<div id="affix-nav" class="affix sidebar col-sm-3">
<ul class="nav sidenav" data-spy="affix">
{% for cat in categories %}
{% include 'ToolBundle:Default:sidebar_bit.html.twig' %}
{% endfor %}
</ul>
</div>
</div>
...and that is sidebar_bit.html.twig
<li>
{{ cat.getDefaultName }}
{% if no_subs is not defined %}
<ul class="nav">
{% for cat in cat.getChildren %}
{% include 'ToolBundle:Default:sidebar_bit.html.twig' with {'no_subs': true} %}
{% endfor %}
</ul>
{% endif %}
</li>
Thanks alot in advance and do not hesitate for additional info if is required.
UPDATE
Here is the jsfiddle: https://jsfiddle.net/0uwegh6k/2/

the fiddle example is messed up, but it looks like you have to add some js to make it work or you can use the boostrap 3 toggle link is here
How it works is like data-toggle="collapse" on the trigger and target it using href="collapseExample" or data-target="#collapseExample" as shown in the link above
<ul>
<li>
<a href="collapseExample" data-toggle="collapse" >Europe</a>
<ul class="nav" id="collapseExample">
<li>
England
</li>
<li>
Spain
</li>
</ul>
</li>
<ul>

Related

Drupal add extra fields to menu

I have some issue with drupal 9 and the menu. Basically i want to add some extra fields for menu link, like icon (image or svg upload) and some text for description. I use Simplify menu for the menus.
I already tried with Menu item extras module but i can't print the added fields in twig file.
Someone know how to fix this and get the fields value in twig template? (menu.html.twig)?
Thanks
You can print it in *.html.twig file, the below code I rendered the main menu like:
{% set items = simplify_menu('main') %}
{{ menu_item.NEW_FIELD }}
<nav class="nav">
<ul class="navbar-ul">
{% for menu_item in items.menu_tree %}
<li class="nav-item">
<a class="nav-link" href="{{ menu_item.url }}">{{ menu_item.text }}</a>
</li>
{% endfor %}
</ul>
</nav>

How to interpolate a variable in a twig loop to render a file?

I am looping through a list of social profiles to render the logo. Each social item has a field of svg_icon_code that looks like "twitter" or "facebook". I want to use this code to render the correct file. Currently I am just hardcoding in "twitter" resulting in all logos being the Twitter svg. How can I use the icon_code to render the correct svg?
{% for item in options.social_media_links %}
<li class="header__social-media-links__item">
<a href="{{item.link}}" target="_blank">
{% include 'component/svg-twitter-icon.twig' %}
</a>
</li>
{% endfor %}
You need to concat the icon like this,
{% for item in options.social_media_links %}
<li class="header__social-media-links__item">
<a href="{{item.link}}" target="_blank">
{% include 'component/svg-'~item.svg_icon_code~'-icon.twig' %}
</a>
</li>
{% endfor %}
If an icon doesn't have a template or the template is missing you can do this to render a default icon
{% for item in options.social_media_links %}
<li class="header__social-media-links__item">
<a href="{{item.link}}" target="_blank">
{% include [ 'component/svg-'~item.svg_icon_code~'-icon.twig', 'component/svg-default-icon.twig' ] %}
</a>
</li>
{% endfor %}

Drupal 8 remove extra divs and add custom classes in menu

Currently the {{page.primary_menu}} created the extra divs and default d8 classes as below:
<div class="region region-primary-menu">
<nav role="navigation" aria-labelledby="block-demo-main-menu-menu" id="block-demo-main-menu">
<h2 class="sr-only" id="block-demo-main-menu-menu">Main navigation</h2>
<ul class="menu menu--main nav navbar-nav">
<li class="first">
Home
</li>
<li>
Home
</li>
<li class="last">
ABOUT US
</li>
</ul>
</nav>
</div>
However, I want to generate the menu structure as like:
<ul id="top-menu" class="nav navbar-nav navbar-right mu-main-nav">
<li>HOME</li>
<li>ABOUT US</li>
<li>MENU</li>
<li>RESERVATION</li>
<li>GALLERY</li>
<li>OUR TEAM</li>
<li>BLOG</li>
<li>CONTACT</li>
</ul>
I've created a file name demo.theme and pasted the code but it did not give me the expected result.
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Template\Attribute;
/**
* Implements hook_preprocess_HOOK() for HTML document templates.
*
* Adds body classes if certain regions have content.
*/
function demo_menu_tree(&$variables) {
return '<ul id="top-menu" class="nav navbar-nav navbar-right mu-main-nav">' . $variables['tree'] . '</ul>';
}
Any suggestion?
Make sure you have twig debugging enabled, it will make your life a lot easier, by adding comments to your mark up (which you can see inline in the web inspector). Using those comments you can figure out what you should name your theme file.
Create a new custom twig file in the /templates directory of your theme like so themes/[your-theme-name-here]/templates/menu.html.twig. As a starting point I'd suggest either using the default classy theme menu.html.twig template, or clone use the file referenced inline in the markup comments of your site when you have twig debugging enabled.
Edit the menu.html.twig file to meet your needs, something like this:
{% import _self as menus %}
{#
We call a macro which calls itself to render the full tree.
#see http://twig.sensiolabs.org/doc/tags/macro.html
#}
{{ menus.menu_links(items, attributes, 0) }}
{% macro menu_links(items, attributes, menu_level) %}
{% import _self as menus %}
{% if items %}
{% if menu_level == 0 %}
<ul id="top-menu" {{ attributes.addClass('nav navbar-nav navbar-right mu-main-nav') }}>
{% else %}
<ul class="menu">
{% endif %}
{% for item in items %}
{%
set classes = [
'menu-item',
item.is_expanded ? 'menu-item--expanded',
item.is_collapsed ? 'menu-item--collapsed',
item.in_active_trail ? 'menu-item--active-trail',
]
%}
<li{{ item.attributes.addClass(classes) }}>
{{ link(item.title, item.url) }}
{% if item.below %}
{{ menus.menu_links(item.below, attributes, menu_level + 1) }}
{% endif %}
</li>
{% endfor %}
</ul>
{% endif %}
{% endmacro %}

Count the number of items in an array using Twig while in an IF statement

I'm building an help centre for an application and I want to be able to display the number of topics within a specific category. At the moment, this is what I have:
{% for cat in cats %}
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion" data-toggle="collapse" data-parent="#helpcategories" href="#category{{cat.id}}">
{{cat.category}}
{% for top in tops %}
{% if top.category == cat.id %}
<span class="badge pull-right">
{{ tops|length }}
</span>
{% endif %}
{% endfor %}
</a>
</h4>
</div>
<div id="category{{cat.id}}" class="panel-collapse collapse">
<div class="panel-body">
<ul class="nav nav-pills nav-stacked">
{% for top in tops %}
{% if top.category == cat.id %}
<li>{{top.title}}</li>
{% endif %}
{% endfor %}
</ul>
</div>
</div>
</div>
{% endfor %}
As you can see, I use Twig to sort out the topics in to their respective categories. As you can also see, in the area that I want to display the number of topics within a category I am using {{tops|length}}. However, this returns the number of topics in total, not per category.
How can I get Twig to count the number of times a topic appears in a category?
I would suggest not using your templating language to build those counts but to do it in your application before you get to the template because that would enable you to display total counts before pagination if you ever decide to paginate.

Rendering different controller view in sitelayout in symfony 2

I have a layout which has a sidebar. Inside sidebar, there is a block for displaying list of categories. I have called a controller (fetches list of categories) to be rendered inside this block. Here is how my sidebar looks like:
Here is my main layout file containing sidebar:
<!-- siteLayout.html.twig -->
<div id="sidebar">
{% block sidebarBlock1 %}
{% render "TestBundle:Index:categoryList" %}
{% endblock %}
</div>
TestBundle:Index:categoryList fetches list of categories from database and returns as below:
<ul>
<li>Category 1</li>
<li>Category 2</li>
</ul>
All my other views extends siteLayout.html.twig. What i want is that when users loads this url "/category/1" i want to add css class to li tag.
For example if someone clicks /category/1 then the output should be
<li class="active">.......</li>
How can I achieve this?
<li {% if app.request.attributes.get('_route') == 'category_view' %}
class="active"
{% endif %}>
</li>
Check route and set class if route matches your category route. Replace category_view with the route name of /category
{%if app.request.server.get('REQUEST_URI')== path('viewCategoryItems', {'slugName': category.slugName})%} class="active"{%endif%}
This worked for me

Resources