I work with a symfony embedded form to manage my translated fields for a given entity. My principal entity has a boolean field and I have multiple text fields for each translation.
I do not use the translatable doctrine extension and I do not want to use it.
In my FormType, I use a CollectionType to embed the translated fields in my form, and in the template, I use the form theme to customize the HTML.
This is my problem : I would like to group fields in my form to optimize UX but once I call the form_widget on my field, I cannot use it a second time. I would like to show 2 fields for a given language and then another field for another language further in the form. How can I solve my problem ?
This is a twig example that illustrate my problem.
{% form_theme form _self %}
{% block _service_translations_entry_widget %}
<div>
{% if name == 0 %}
<div class="s12 m6 l6">
{{ form_label(form.title) }}
{{ form_errors(form.title) }}
{{ form_widget(form.title) }}
</div>
<div class="s12 m6 l6">
{{ form_label(form.subtitle) }}
{{ form_errors(form.subtitle) }}
{{ form_widget(form.subtitle) }}
</div>
{% endif %}
{% if name == 1 %}
<div class="s12 m12 l12">
{{ form_label(form.desc) }}
{{ form_errors(form.desc) }}
{{ form_widget(form.desc) }}
</div>
{% endif %}
</div>
{% endblock %}
{% block body %}
{{ form_start() }}
{{ form_errors(form) }}
<div>
<div class="s12 m12 l12">
{{ form_label(form.doubleBlock) }}
{{ form_errors(form.doubleBlock) }}
{{ form_widget(form.doubleBlock) }}
</div>
</div>
<div id="block-1">
{{ form_widget(form.translations) }}
</div>
<div>
<div class="s12 m6 l6">
{{ form_label(form.activedStyle) }}
{{ form_errors(form.activedStyle) }}
{{ form_widget(form.activedStyle) }}
</div>
<div class="s12 m6 l6">
{{ form_label(form.checkoutOption) }}
{{ form_errors(form.checkoutOption) }}
{{ form_widget(form.checkoutOption) }}
</div>
</div>
<div id="block-2">
{{ form_widget(form.translations) }}
</div>
{{ form_widget(form.save) }}
{{ form_rest(form) }}
{{ form_end(form) }}
{% endblock body %}
As you say, you can’t reuse a field.
My suggested approach would be to add an additional field to the form, translations_extra, which either isn’t mapped to or persisted by the entity. Extract the data in the controller on form submission and then add it to the translations collection before persisting.
Related
I am new to Drupal, and I'm creating a custom theme based on a Bootstrap Bario subtheme on Drupal 8.
I can update the CSS with no problem, but I first need to clear the cache before reloading the page (which gets annoying, is there a way to avoid that?).
This is how the theme's regions are arranged:
I added the help: 'Help' region in the .info.yml file, under the line footer_fifth: 'Footer Fifth', and the added Help region appears in the Block Layout page.
I then modified the relevant parts of templates/_page.html.twig like so:
From this
{% block footer %}
<div class="{{ container }}">
{% if page.footer_first or page.footer_second or page.footer_third or page.footer_fourth %}
<div class="site-footer__top clearfix">
{{ page.footer_first }}
{{ page.footer_second }}
{{ page.footer_third }}
{{ page.footer_fourth }}
</div>
{% endif %}
{% if page.footer_fifth %}
<div class="site-footer__bottom">
{{ page.footer_fifth }}
</div>
{% endif %}
</div>
{% endblock %}
to this
{% block footer %}
<div class="{{ container }}">
{% if page.footer_first or page.footer_second or page.footer_third or page.footer_fourth or page.footer_fifth %}
<div class="site-footer__top clearfix">
{{ page.footer_first }}
{{ page.footer_second }}
{{ page.footer_third }}
{{ page.footer_fourth }}
{{ page.footer_fifth }}
</div>
{% endif %}
{% if page.help %}
<div class="site-footer__bottom">
{{ page.help }}
</div>
{% endif %}
</div>
{% endblock %}
As you can see, I added or page.footer_fifth to the first if statement, moved {{ page.footer_fifth }} to the site-footer__top div, and added {{ page.help }} in its place.
After that, I go and clear the cache, but the block regions are still the same. If I inspect my block in the Footer Fifth region, it is still in the <div class="site-footer__bottom">.
Did I miss something?
Thank you in advance.
I think the template file name is wrong. See:
templates/_page.html.twig usually should be:
templates/page.html.twig
the complete path of your template should be "/themes/custom/yourthemename/".
PS: theme debug will help you in case of any doubt.
https://www.drupal.org/docs/theming-drupal/twig-in-drupal/debugging-twig-templates
I want to show contact form which i am using into
but shorcode not showing form on frontend
How can I show contact form 7 with .twig file?
<div class="our_form">
{{ wp.do_shortcode('[contact-form-7 id="766" title="Contact form 1"]') }}
</div>
<form action="{{ post.link }}" class="contact-form" method="post" {{ form_enctype(form) }}>
<div class="contact-form__row">
{% spaceless %}
{{ form_row(form.name, {}) }}
{{ form_row(form.email, {}) }}
{{ form_row(form.subject, {}) }}
{% endspaceless %}
</div>
{{ form_row(form.message, {}) }}
{{ form_widget(form) }}
<button class="btn btn--primary caps">Send</button>
</form>
how about running this like that:
{% filter shortcodes %}
[contact-form-7 id="766" title="Contact form 1"]
{% endfilter %}
Reference: https://timber.github.io/docs/guides/wp-integration/#shortcodes
http://symfony.com/doc/current/cookbook/form/form_collections.html
Symfony2 embedded forms rendering in a twig template
I am aware of how I might render the two forms on an individual field basis - but what I want to render the main form and sub-form in more expedient way:
{{ form_start(form) }}
{{ form_widget(form.base) }}
{{ form_widget(form.child1) }}
{{ form_widget(form.child2) }}
{{ form_end(form) }}
Possible? Or do I have to render each of the fields individually if I need this type of control?
EDIT | HERE IS WHAT I HAVE
<div class="box box-primary">
{{ form_start(form) }}
<div class="nav-tabs-custom">
<ul class="nav nav-tabs">
<li class="active">Master</li>
<li>Stock</li>
</ul>
<div class="tab-content">
<div class="active tab-pane" id="activity">
{{ form_row(form.partNumber) }}
{{ form_row(form.partDescription) }}
</div>
<div class="tab-pane" id="timeline">
{% for stock in form.inventoryStock %}
<div>{{ form_widget(stock.onHandQuantity) }}</div>
<div>{{ form_widget(stock.batchNumber) }}</div>
<div>{{ form_widget(stock.serialNumber) }}</div>
{% endfor %}
</div>
</div>
</div>
{{ form_end(form) }}
</div>
You should just need to do this:
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
See the Rendering the Form section of the Symfony documentation. As long as everything is defined properly, Symfony will render all of the parts of the form for you, included forms that are embedded.
You often will need to use the capabilities for extra granularity but it's not required.
Also, it's absolutely possible to do what you stated - just give it a try and you'll see that you can.
I am displaying a form with Symfony2 and Twig, and need to display some fields in a dynamic order using a loop over an array. The code looks like this:
{% for activity in activities %}
<div class="check">
{{ form_label(tags_form.chactivity{{ activity.id }}) }}
{{ form_widget(tags_form.chactivity{{ activity.id }}) }}
</div>
{% endfor %}
But of course the {{ activity.id }} does not fit here.
How can I use the activity.id (integer) inside the {{ form_label(...) }} and {{ form_widget(...) }} expressions?
You can do this using the attribute function:
{{ form_label(attribute(tags_form, 'chactivity' ~ activity.id)) }}
So, what I need to do is to output current element's ID in each field_row. What I came to is overriding Symfony's default field_row block with the following code:
{% block field_row %}
{% spaceless %}
<div class="clearfix" id="{{ form.get('name') }}-row">
{{ form_label(form) }}
<div class="input">
{{ form_widget(form) }}
</div>
</div>
{% endspaceless %}
{% endblock field_row %}
However, the {{ form.get('name') }} construct seems pretty awkward to me and I'm sure there's a more civilised way of doing this. Anyone?
Do you mean the id generated by symfony? then it's just:
{{ id }}