KnpMenuBundle - send Options and use them in Twig - symfony

I wrote the last days on my nav-bar-menu whit KnpMenuBundle. I want simply to give the template a few parameters on the way and then react on it. Is it possible? I tried this:
$menu->addChild('Registration', array('route' => 'fos_user_registration_register',
'icon' => array('glyphicon' => 'briefcase')));
And then I want to pick this in Twig template:
{% if icon['glyphicon'] is defined %}
<span class="glyphicon glyphicon-{{icon['glyphicon']}} "></span>
{% endif %}
I tried a lot, but nothing works. What can I try next?

Here is how i did it
The Menu class:
$menu->addChild('Home', array(
'route' => 'home',
'extras' => array('icon' => 'home')
));
My menu call in the template, i ask for a specific twig template
{{ knp_menu_render('MyBundle:Menu:primaryNav', {'template': 'MyBundle:Menu:primaryNav.html.twig'}) }}
In the twig template, i copy/paste the knpmenubundle template and edit some blocks. you may be able to do this with 'extends' and template's inheritance stuffs as well (probably a better idea).
Example of edited block for icon :
{% block spanElement %}
<a href="#" class="dropdown-toggle">
{% if item.extras.icon is defined %}<i class="icon-{{ item.extras.icon }}"></i>{% endif %}
<span class="menu-text"> {{ block('label') }}</span>
<b class="arrow icon-angle-down"></b>
</a>
{% endblock %}
What you needed was probably this "extras" field in the menu class :)

I don't know if this can still help but there's a much easier way to use font awesome with knp menu. There's an option where you can give a class to your link. You just need to create a child without a label an add setLinkAttribute. Ex:
$menu
->addChild('', array('route' => 'backend_index'))
->setLinkAttribute('class', 'fa fa-home');
This will create an "a" tag with the class fa fa-home. Even if Font Awesome documentation says the icons are designed for inline elements, it will work with "a" tag. You can then use some css to improve the design. In my case, I just changed font-size and line-height.

Related

Render as multiple bagdes with an AssociationField in EasyAdmin

I got an issue with that question.
I've make this to render a "string" for a ManyToMany relation :
->formatValue(function ($value, $entity) {
return implode(",",$entity->getCategories()->toArray());
})
and it works pretty good ! But I've a question !
How can I render many badges in Index ? Because this method render one unique badge with "Value 1, Value 2"... And I want to see 2 badges, one with "Value 1" and one other with "Value 2" in the same line.
Someone know how to do that ?
I hope my question is clear.
NoƩ
You need to create a custom template that does it.
Use easy admin ->setTemplatePath() method to override your field template.
Example:
->setTemplatePath('fields/yourEntity/categories.html.twig')
And your twig template loop through each values to render it with multiple badges:
{% for value in field.value %}
<span class="badge badge-info">
{{ value }}
</span>
{% else %}
<span class="badge badge-secondary">
None
</span>
{% endfor %}
You should get a badge for each categories, you could also customize how to render those badge (with different colors ?) by using {{ value }} and any of its method to render it differently.

How do I pass variables to theme template via custom controller drupal 8

I currently have a theme I purchased installed on my Drupal website.
Inside the theme folder there is a templates folder that contains:
page.html.twig
page--front.html.twig
a snippet from page.html.twig looks as follows:
<footer class="site-footer">
<div class="layout-container">
{% if page.footer_first or page.footer_second or page.footer_third %}
<div class="footer-top">
<div class="footer-top-inner">
{{ page.footer_first }}
{{ page.footer_second }}
{{ page.footer_third }}
</div>
</div>
{% endif %}
{% if page.footer_left or page.footer_right %}
<div class="footer-bottom">
<div class="footer-bottom-inner">
{{ page.footer_left }}
{{ page.footer_right }}
</div><!-- /.footer-bottom-inner -->
</div>
{% endif %}
</div>
</footer>
I have a custom module I created (business_listing) that loads different .html.twig templates I add these templates in business_listing.module:
function business_listing_theme($existing, $type, $theme, $path) {
return [
// business-listing-detail.html.twig
'business_listing_detail' => [
'variables' => ['data' => [], 'slides' => [], 'paths' => [], 'page' => []],
],
]
}
Basically I would like to know how I can add the markup for the footer dynamically from my custom modules controller.
I have a page called business-listing-detail.html.twig inside my custom modules templates folder. From what I understand business-listing-detail.html.twig uses/somehow extends page.html.twig in my theme/templates. What I would now like to know is how I can add the sections like {{page.footer_left}} to my business-listing-detail.html.twig or to page.html.twig using my controller? I have tried adding the footer using the following: `
$page['footer_left'] = "This should appear in the .footer-bottom div?";
$build = [];
$build['business_listing_detail'] = [
'#theme' => 'business_listing_detail',
'#data' => $record,
'#slides' => $slides,
'#paths' => $this->paths,
'#page' => $page
];`
in the controller function associated to my business-listing-detail page the hopes that page.html.twig will detect this page.footer_left and render display the footer, however this did not work. The variable exists in business-listing-detail.html.twig but the {% if page.footer_left %} in my themes page.html.twig is not fired. Please please please, any help or advice would be greatly appreciated <3
Basically all I am trying to do, is dynamically render a template for a specific page in my custom module, that allows me to implement/send markup to the sections/regions defined in my theme eg. featured_top, content_top & page.content
Kind regards,
Matt
It seems like you might be going down the wrong path... I'd highly suggest you read up about the Drupal 8 Block system, and then investigate custom blocks.

Get parent of term

I might have missed it in the Timber Docs but is there a possibility to get the parent terms of a given taxonomy?
two methods depending on Twig vs. PHP and what exactly you're looking to do.
Consider a post which as been assigned a category of "Politics" (which is a child category of "News")
Example 1: Get a parent term in a Twig file
<p>Find more posts in {{ post.category.name }} and {{ post.category.parent.name }}</p>
<!--- outputs as ... --->
<p>Find more posts in Politics and News</p>
Example 2: Get the top-level terms from a taxonomy
$context['parent_categories'] = Timber::get_terms(array('taxonomy' => 'category', 'parent' => 0));
Use in Twig like...
Top-level categories for my site are...
{% for term in parent_categories %}
<li>{{ term.name }}</li>
{% endfor %}
<!-- Outputs as... -->
<li>News</li>
<li>Sports</li>
<li>Opinion</li>
Let's say you are trying to use the parent of a category in a twig view. That was the case I tried to solve when stumbling upon this post.
In category.php you can get the parent category by getting the parent category ID of the currently viewed category:
$category = new TimberTerm();
$context['category'] = $category;
if ($category->parent) {
$context['parent_category'] = new TimberTerm($category->parent);
}
$category->parent corresponds to the ID of the parent category. It is null if the category doesn't have a parent. You can then use the resulting category in your template.
<a class="category_{{parent_category.slug|lower}}" href="{{parent_category.link}}">
{{parent_category.title}}
</a>

How can I print Google Books api description?

Hie I am trying to get the synopsis and other items like author and published date printed. But I am Able to achieve this only with certain search terms, an error occurs with other words or terms
Key "description" for array with keys "title, subtitle, authors, publishedDate, industryIdentifiers, readingModes, pageCount, printType, categories, maturityRating, allowAnonLogging, contentVersion, imageLinks, language, previewLink, infoLink, canonicalVolumeLink" does not exist.
I am using symfony and twig. this is what the twig file looks like :
{% for item in items %}
<article>
<img src="{{ item.volumeInfo.imageLinks.thumbnail}}"/>
<h4>{{ item.volumeInfo.title}}</h4>
{{ item.volumeInfo.description }}
<strong> {{ item.volumeInfo.publishedDate }}</strong><br/>
<b>{{ item.volumeInfo.authors | join }}</b>
</article>
What am I doing wrong? why does this work only sometimes ? how can I make it work correctly all the time?
class GoogleBooksController extends Controller
{
public function getVolumeAction($title)
{
$client =new client();
$response = $client- >get("https://www.googleapis.com/books/v1/volumes?q=$title");
$data=$response->json();
$items=$data['items'];
return $this->render('BookReviewBundle:GoogleBooks:volume.html.twig', array('items'=>$items
// ...
)); }
Thanks
I belive the description field is not mandatory, so you can do follow
{% if item.volumeInfo.description is defined %}
{{ item.volumeInfo.description }}
{% endif %}

Set widget container attributes dynamically

I'm busy with templating my forms inside Symfony 2.0.
It is possible to add attributes to a formfield like this:
$form->add('name', 'text', array('attr' => array('class' => 'my_class')));
But how can I dynamically add attributes to the formfield widget? If i look to the form_div_layout.html.twig content, their is a
{{ block('widget_container_attributes') }}
which can load attributes, but I have no idea where I can add attributes to my FormBuilder in the Controller. Could anyone help me?
Thx!
You should do that in twig layer:
{{ form_row(form.name, {attr: {class: 'my_class'}}) }}

Resources