How to display data with a ManyToMany relation and a joint table? - symfony

i'm currently working on a symfony project and i have a problem.
This project is a website about a theater. There is therefore an "actors" entity and a "shows" entity.
Each show can have multiple comedians, and comedians can have multiple shows. So I generated a ManyToMany relationship, and got a "spectacles.actors" link table.
I don't understand how I can display in my twig, for each actor, the shows in which he is present.
enter image description here
I tried
{{ actors.spectacles.title }}
but its not working, i also tried a double for loop, but still not working. Can you help me please?
Thank you
Ok it doesn't work but I found why. In fact with my ManyToMany relationship, I got two binding tables: actors_spectacles and spectacles_actors. When I perform my fixtures, the spectacles_actors table is filled but not actor_spectacles.
But how to fill it? Since there is a certain order in fixtures, I am having a lot of trouble understanding how fixtures work with ManyToMany relationship, can you help me?

You should loop over each actor and each linked shows to the actor.
Let's say you fetched all the actors in your controller:
{% for actor in actors %}
<ul>
{% for spectacle in actor.spectacles %}
<li>{{ spectacle.title }}</li>
{% endfor %}
</ul>
{% endfor %}
ps: I advise you to use singular form for naming your entities ;)

Related

Theming Drupal 8 - Loop through all posted articles and print specific section

I am trying to create my first Drupal 8 theme. On the frontpage, I want to list all articles with the title, summary, date posted, etc.
I more or less achieved this using views. Basically {{ page.content }} in twig, but found it to be really inflexible and I didn't really get the results that I wanted. What I want to do is just to iterate through all the articles and print each section of that article "manually". For instance:
{% for page in pages %}
{{ page.content.title }}
{{ page.content.datePosted }}
{{ page.content.body }}
{% endfor %}
So that I can have more control of what is happening and not making a lot of configuration in the views module when deploying. What is the best soltuion to achieve this? Thanks!
I recommend to use Views. Configure you view (filter by content type, etc.), you have a lot of parameters to achieve what you need.
Set the view to show "Content" and give it a view mode (you can use the default teaser, full, or create your own view mode). Then you can create a custom template for this :
node--xxxx--teaser.html.twig
In order to know how to name your custom template file, enable Twig Debugging in sites/default/services.yml. Set the debug variable to true. And clear cache. Then, you will see in source code the template name suggestion like this :
<!-- FILE NAME SUGGESTIONS:
* node--1--full.html.twig
* node--1.html.twig
x node--oeuvre--full.html.twig
* node--oeuvre.html.twig
* node--full.html.twig
* node.html.twig
-->
In your twig, you can do that kind of templating :
{{ content.my_field_image[0] }}
<h2>{{ node.title.value }}</h2>
<p>{{ content.body }}</p>

Using alternative (translated) titles in dynamic menus in Lektor

I'm new to Lektor and I've been using this snippet in my layout to dynamically generate menus
{% for content in site.get('/').children %}
<li>{{ content.title }}</li>
{% endfor %}
It works fine to produce the alt url accordingly to the alternative (language) selected, but the title is still shown only in the primary language of the content instead of the translated one.
That means that when a say french alt is selected (and present in the url), menus are still i.e. Main, Contact instead of the french translated version.
I know I can work around this using a databag to hold a language mapping for the menus, but that would require to duplicate the title information in the databag and then make sure it stays in sync with whatever is in content.title.
Ideally what I'm missing is a filter for title where you can specify the alt you want to use, or some other method that I'm not aware of?
Naive as it may be, I didn't realize that you can pass the alt parameter in site.get until after I looked at the source code. So the way to get the defined child pages for a certain language is:
{% for content in site.get('/', alt=alt).children %}
<li>{{ content.title }}</li>
{% endfor %}
And since we already specified the alt parameter in the get, we don't need to filter the url anymore for the current alt.

CollectionType custom prototype via form_theme

I want to customize the way an entry of a CollectionType in a form will be rendered, that is the way the prototype is generated, I need my own HTML. So I need to do it with the Twig form_theme, but it doesn't work.
I am currently using Symfony 4.1.2.
Here is what I have for now:
In the template that renders the whole form:
{% form_theme form.additionalEmails.vars.prototype 'form/additionalEmail.html.twig' %}
In the form/additionalEmail.html.twig file:
{% block form_row %}
<div style="margin: 10px 0">
{{ form_label(form) }}
{{ form_widget(form.children.value) }}
<div>Recevoir les mails de type : {{ form_row(form.children.isUsedForAdminCommChannel) }}{{ form_row(form.children.isUsedForInfosPratiquesCommChannel) }}{{ form_row(form.children.isUsedForAgendaCommChannel) }}</div>
</div>
{% endblock %}
If I do that I get this error:
Key "value" does not exist as the array is empty.
What is weird is that if I replace the above code by this (by commenting the old one and adding the new line):
{{ dump(form.children) }}
I see that form.children is an array with 4 items and one named "value" so it is clearly broken.
Your help would be much appreciated, as I'm just an amateur dev trying to make a website for a youth movement ^^
Best Regards,
[EDIT]
Here is the form that includes the CollectionType:
https://gist.github.com/MrPOC/a4af94cc5e577a1244dac59de5ea3506
The field is named "additionalEmails"
Just so I understand you correctly. You have a Form which contains a CollectionType and you want to change the prototype of that CollectionType?
If yes could you please post the Form with the CollectionType?
EDIT: Ok so here's how to overwrite the prototype template for a CollectionType.
First we have to find out what your forms unique_block_prefix is. You can find this by going to the page where this form is rendered, you then click in the symfony debug bar on the form icon. Then you have to expand the View variables block and look for unique_block_prefix.
I assume your block prefix could look like this _user_type the name of the block you would overwrite would then be _user_type_additionalEmails_entry_widget (for more details please read the link provided below)
You can replace the widget part with row or label depending on what you want to overwrite (I would suggest trying to overwrite them all to see what exactly changes because I don't know of the top of my head)
In twig you would then take your block prefix and overwrite the prototype field in the same way you would overwrite any other field
{% block '_user_type_additionalEmails_entry_widget' %}
//Content
{% endblock %}
What I wrote down here is what I found out after reading https://github.com/symfony/symfony-docs/issues/6056 (read HeahDude' comment specifically)
I hope this helps!

Is there way in Shopify to categorize "Collections" in lists?

Say I want to show 2 different collection lists(just the titles with links to their relative pages) on 2 different pages in Shopify; is there a way in Shopify that can be done? As far as I can see, either I show all "Collections" on a page or none at all. Is there no option to categorize them so that I can use liquid to only show certain collections?
The only other option to this I found is to hard code those collection menus on the page. But the issue with that is, any new collection I create, I will have to manually add it in to the code if I want to show it.
UPDATE
If you want to display all collections after a certain collection, once you find that handle, just have it parse the rest of them after it finds the handle.
Sorry my liquid is a little rusty so this is pseudo code.
{{ liquidtempvar = 0 }}
{% loop through collections %}
{% if thiscollection.handle == 'firstcollectioniwanttodisplay' %}
{{ liquidtempvar = 1 }}
{% endif %}
{% if liquidtempvar eq 1 %}
{{ loop through this collection and every collection after }}
{{ collection.product.handle }}
{{ endloop }}
{% endif %}
{{ endloop }}
Something like that...
END UPDATE
If you want to display a list of the items in a collection as links, then you would need to parse the collections object in the template of the page where you want to display them.
You would want to use the collection handle to qualify/filter which collections you wanted to display.
Using liquid loop over each collection, if the handle matches the collection you want, loop over the collection and display the links.
If you are looking to do this as a menu through the navigation, I think you can probably do it using query strings in the URL.
collection.next_product
and
collection.prev_product
will return the URL of the next and previous products in the collection.
Collection Navigation
Collection Object Reference

symfony2: index in individual form field for collection type

After hours of searching, I found a solution how to apply themes to individual fields for collection types here: symfony2: Applying theme to individual field for collection type (thanks #TroodoN-Mike).
So a custom theme for a collection type could be:
{% block _author_tags_entry_label %}
{# ... #}
{% endblock %}
The word "entry" replaces the row index, which works fine, but I need the Row index inside the custom block, something like {{ entry }}. Any hints?
Thanks
Chris
Ok, I found the solution. I wasn't aware of the fact, that you can pass custom variables to the form theme like this:
{{ form_row(form.field, {'customVariable': 'anyValue'}) }}

Resources