I have a User entity and all users have many Category entities. I have a controller which accepts a Category slug as a parameter.
I would like to render a menu that lists all of the Category's of a User as links to that controller. I would also like to mark one as "current" if they are on that page already.
The KnpMenuBundle looks nice for this https://github.com/KnpLabs/KnpMenuBundle/blob/master/Resources/doc/index.md
But all of its examples use a static menu that is built up, rather than building the menu from dynamic items. I thought about getting the items in the Builder, but I already have them in the controller that ultimately renders the twig template, so I would rather somehow pass these Category's to the Builder and let it handle rendering a menu.
What is the best way to do this?
My other idea is to just do the menu myself in twig, but I would need extra logic to determine which page is "current"
I know that this might not be useful to you anymore but for others viewing the question maby this is helpful.
Since recently it is possible to send your own variables from twig to the builder.
It's done like this as noted in the Documentation:
{% set menuItem = knp_menu_get('AcmeDemoBundle:Builder:mainMenu', [], {
'some_option': 'my_value'
})
%}
{{ knp_menu_render(menuItem) }}
Related
I have a task of making some content of movies in admin panel in drupal
https://imgur.com/a/5eJp9ZQ
and then after that to use a custom module and controller to show those movies onto my custom twig.
I am unable to find a solution or hint how to do that, maybe i even did find something but because of my small knowledge of drupal i dont know what i have to do in order to achieve that.
I created a custom module, controller , routing and everything while following the hello world part of drupal documentation but after that i just frooze and i am unable to continue further
Can someone assist me on this task, will be greatly appreciated because i have been stuck on it for 2 days now
Drupal's way of getting content is by using views. You should create a view (Structure -> Views -> Add view from admin menu) to get that data. It's something like visual interface for creating database queries, and it's not hard as it might look at first sight.
Then, there are few way how to use that view.
View can create a page, or a block, so content will appear on the page
You can execute view from your controller and collect data view returned and pass it to twig template to render the data.
You could embed view directly from twig, so no need for any kind of coding inside page controller.
Option 1 with "page view" (it's display type is "page") is probably easiest one, but doesn't fulfill request of custom controller.
With option 2 you could use: views_get_view_result() call to get results:
https://api.drupal.org/api/drupal/core%21modules%21views%21views.module/function/views_get_view_result/8.2.x
It should look something like:
$view = Views::getView('view_machine_name');
$view->execute();
foreach ($view->result as $row) { // iterating trough rows of results
// Do something with $row
}
This option matches the best your request.
For option 3 you would have to Twig Tweak module to embed view from twig:
https://www.drupal.org/docs/8/modules/twig-tweak/twig-tweak-and-views
Beside that you could of course create custom database query, but that's not so "drupalish" way and queries can be pretty complicated since drupal's database structure is not the simplest one and you could get lost.
I'm using the EasyAdminBundle for SF and I have a show view for an Entity, where I list like id, username and password. What I would like to do is simply render one more row where I deliver the content from a custom template.
what is the easiest way? I didn't found a solution yet with YAML. Do I need to overwrite the whole show.html.twig ?
I'm currently experimenting with EasyAdminBundle:
I want to add a menu item that calls a custom controller and renders a view. The view is not based on an entity but should rather display the results of a csv import.
What I have done so far is:
create Controller and render the results into a new twig template.
add the menu item to config and call my Controller with the option
'route'
The twig template is pretty basic for now. It extends from "EasyAdminBundle:default:layout.html.twig" and overwrites the block "main" in order to display the import results.
Now my problem is that it does not seem to find the css for the layout twig?
My results show a plain html page without any formatting :(
Any hints or suggestions how I can render the view with all the styles from the default views?
Also note: I don't want to overwrite the default layout template for the whole backend and I also don't have a specific entity where I could assign a special layout file. Thanks for your help.
found the problem. So the setup as described was ok except that we had some internal project config (that I was not aware of), that would make the response to be returned as json instead of html.
I am using Drupal 8.
I am trying to create a block display to show a list of teasers with the taxonomy term of the current node, within a custom region.
The problem is that when I navigate to the page which contains the custom region no content is displayed.
Other blocks display fine when assigned to the custom region so I don't think its anything to do with my templates.
The same view also has a page display (basically the default taxonomy term view) which shows content correctly when i navigate to the node.
I am a relative Drupal newbie so may be missing something obvious here but as far as i can tell the taxonomy view works as a page but not as a region, so either it must be something to do with the block configuration, or Drupal is just not meant to work like this.
Can anybody suggest what might be causing this, or if this is a bad approach explain why?
are u using exposed filters in your view ?
For it to work in blocs , u need to configure in your view
ajax:on
By default for creating content a new page is opened. I want to do this inside a block. A block where there will be some fields like title body taxonomy terms and a create content button. How can I do this?
The form block module will do what you want.
You could you do the same thing in code, using hook_block to create a block. And using
drupal_get_form to render the form.
Try the Panels module.
http://drupal.org/project/panels
It will let you override and re-arrange the node add/edit form.
But, as Jeremy stated, to use this form on other pages of the site you will need to do something custom like hook_block and invoke drupal_get_form.
I agree with the answers from Jeremy and Kevin, but want to give another one.
You can use views to create the block! All you have to do is create a new view display with a block display. Add the fields you want to show, add a link field for the node/add/foo link and give it a node id filter or default argument. Here you have to choose the node id of the node to display in the block. Done!
What's nice with this solution? You can use almost every feature of views theming, drag and drop field ordering, adding new fields is cheap and no extra modules. ;)
Regards
Mike