Symfony2 form collection — Show only the newest - symfony

I have a form with a Parent (Entity) and Children (Record).
A Parent has many children.
So far so good.
What I need is, that if I edit a Parent I want to show only the newest child as a form collection and not all of them.
Right now with the creation of such I just make it like this:
$entity = new WikiEntity();
$record = new WikiRecord();
$entity->addWikiRecord($record);
And with this I have just one form collection — obviously.
Now I am looking for a method to have only the newest Record (child) to edit and all of the older one shouldnt be there.
Just to say what my goal is (maybe there are other solutions), I want to create something like a wiki, so everytime the Parent gets edited a new revision gets created, so I can get an older version or something. So the people don't need to see all of the versions (what form collection does) but only the newest one (which will be created each time you edit it).

I see there is no option to edit only the newest WikiRecord on symfony2 form collection type. But it can be done by manipulating form output in template.
So, here is how I could do using twig template.
{% for record in edit_form.wikiRecord %}
{% if loop.length > 1 %}
{% if loop.last %}
{{ form_label(record.aFieldName) }}
{{ form_widget(record.aFieldName) }}
{% else %}
{{ form_widget(record.aFieldName, { 'attr': {'style': 'display: none'} }) }}
{% endif %}
{% else %}
{{ form_row(record) }}
{% endif %}
{% endfor %}
I used this for a more or less similar situation where I have to make only the latest children editable. So, this work-around is working. :)

Okay I found an Answer for my problem.
I just manipulate my getWikiRecords() function to only return the newest (In the parent function)
So now in the parent Entity for the children it looks like this:
public function getWikiRecords() {
$oneRecord = new ArrayCollection();
$oneRecord[] = $this->wikiRecords->last();
return $oneRecord;
}
Of course if I want all the Children I now have to use a different get function.

Related

Set a new value depending on what the original value is?

In Drupal views, I have a twig variable available to me called {{ name }}. This stores a list of taxonomy terms depending on the type of content used. I currently have a function running something like this that works:
{% if name == "Tax1" %}
Download
{% elseif name == "Tax2" %}
Download
{% endif %}
However, I feel like there is a better way for me to go about doing this. As an example isn't something like this supposed to work?
{%
set newval = [
name == "Tax1" ? 'file1.pdf' : 'file2.pdf'
]
%}
Download
Pretty much what I'm trying to state above is, if the value of name is equal to "Tax1", print out file1.pdf, else print file2.pdf.
I have very basic twig knowledge and I haven't touched it in a couple years so if anyone could help me out with this, that would be great.
Thanks to #DarkBee for the help. I must have been overthinking as the answer was fairly simple.
The below solution worked for what I was initially asking:
Download
However, I ended up needing a third default option if not the first two so the below code is what I ended up using:
{% if name == "Tax1" or name == "Tax2" %}
Download
{% else %}
<span class="stat">Other Option</span>
{% endif %}

Rendering a choice field type (symfony2) as buttons

I have a problem which I cannot solve. How shall I approach the following: I have a choice field with 4 different entries, which I am rendering perfectly in a select form (twig). However, I would like to show the 4 entries as 4 separate buttons. How do I change the input type of those 4? Or do I have to do anything totally different?
Thanks for your help
-AS
edit (new following question):
Alexandru, thanks for your help. That was very helpful.
I added in the fields.html.twig the following:
{% block _appbundle_action_Style_widget -%}
{% for child in form %}
<input type="button" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />
{% endfor %}
{%- endblock _appbundle_action_Style_widget %}
However, when I render it it tells me that there is an array to string conversion, which somehow does not work. When I delete the widget attributes, value, and checked parts, the buttons are getting rendered without those then.
You have a few options to choose from:
You can create a new field type, let's call it "button_group", that will extend the "choice" type and write the custom Twig for it, something like this:
https://github.com/symfony/symfony/blob/2.7/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig#L95
You can use the "choice" field type with the option: expanded => true. And create a custom form template for it only for the form you need it in:
http://symfony.com/doc/current/cookbook/form/form_customization.html#what-are-form-themes
Ok I solved my problem. In order to access the value variable I have to use the following {{ child.vars.value }}. With this I can now take the variable and put it into the value tag.
Thanks for your help.

Symfony2 - get value displayed to user for choice field in twig

I am trying to display the "label" (ie, value displayed to user) of a choice field in my twig template. I'm using Symfony 2.3. I just need the label associated with the actual current value of the field.
I finally figured out a way to do it that requires looping through every choice option, but am now wondering if there is a more efficient way? Here is my working code:
{% for choice in form.myfield.vars.choices %}
{% if choice.value == form.myfield.vars.value %}
{{ choice.label }}
{% endif %}
{% endfor %}
I'm looking for something like this (non-working code):
{{ form.myfield.vars.choices[form.myfield.vars.value].label }}
Is there a more efficient way, or is my solution the best possible?

SonataAdmin: replace ID in breadcrumbs

How can I replace Object's ID in SonataAdmin breadcrumbs by some other text?
If I set __toString() in my document, it works only for editing. When I attempt to create new record, there is something like MyDocument:0000000000e09f5c000000006a48ef49 in the last breadcumb.
I'm searching for a method which allows me to set some text as the last breadcump if Document::toString() returns null.
This behaviour is implemented directly in the entity:
public function __toString()
{
return $this->getFoo() ? : '-';
}
Bundles are using variants of this, including return (string)$this->getFoo(); or $this->getFoo() ? : 'n/a'; etc.
Related question: toString method for SonataAdminBundle Listing in Symfony2
BTW something cool to know, you can completely customize the breadcrumb via a Twig template:
{% block sonata_breadcrumb %}
{% set _breadcrumb %}
<li>Home</li>
<li>Library</li>
<li class="active">Data</li>
{% endset %}
{{ parent() }}
{% endblock %}

symfony2 - twig - how to render a twig template from inside a twig template

I have a xxx.html.twig file which shows a page, but when I want to refresh the page with different data and just update it with new data, I have a select and a submit button for it.
The thing is that I don't know how do I call an action in the controller which I pass parameters to from my twig and call for new data and then I render the same twig template again with new parameters.
How do I do so?
Here are a few different ways:
{{ render(app.request.baseUrl ~ '/helper/test', {"hostid2": hostid } ) }}
or
{% include 'MyCoreBundle:Helper:test.html.twig' with {"hostid2": hostid } only %}
or
{% render controller("MyCoreBundle:Helper:test", {'hostid2': hostid}) %}
Symfony 2.1:
{% render 'YourBundle:YourController:yourAction' with {'var': value} %}
Symfony 2.6+:
{{ render(controller('YourBundle:YourController:yourAction', {'var': value})) }}
And, of course, read the documentation.
I think some parts are depricated here.
To make the include work in latest Symfony 3.1.10, I solved it like this:
{% extends 'base.html.twig' %}
{% block body %}
{{ include('AppBundle:Default:inner_content.html.twig') }}
{% endblock %}
Note: include() with parentheses.
Then all the variables are included from the parent template. If you like to restrict some variables in the child template, you use with ... only (look over)

Resources