Why I get error implementing webfor at AMP, Drupal - drupal

I get this error try to implement form in amp at Drupal for web form
Only XHR based (via action-xhr attribute) submissions are support for
POST requests.

This work for me
At the twig web/themes/custom//templates/webform/webform-custom.html.twig
<form action-xhr="{{ url('<current>') }}" {{ attributes }}>
{{ title_prefix }}
{{ children }}
{{ title_suffix }}
</form>

Related

Searching entire multisite post.link returns current blog url

On a WordPress Multisite installation using Timber the search form returns the correct post/pages but when using {{ post.link }} the slug is prepended with the current blogs url instead of the site the page is on.
Example:
Site 1: test.com/
Has page: foo with URL test.com/foo
Site 2 test.com/2/
Searching for "foo"
{{post.link}} returns: test.com/2/foo. Resuting in 404.
How can I get the correct URL for foo when searching on site 2?
Edit
Solution
// search.twig
{% for post in posts %}
{% do fn('switch_to_blog', post.blog_id) %}
<h2>{{post.title}}</h2>
{{post.preview}}
Read More
{% do fn('restore_current_blog') %}
{% endfor %}
// For anyone interested here is the form using Relevanssi plugin
<form role="search" method="get" id="searchform" class="searchform" action="{{site.link}}">
<input type="text" value="{{ function('get_search_query') }}" name="s" id="s" placeholder="Search..." />
<input name="searchblogs" type="hidden" value="39, 0">
<input type="submit" id="searchsubmit" />
</form>
// Note: If only one id is entered into the searchBlogsById form field {{ post.blog_id }} returns null.
// Add a non existing blog id '0' to get around this.
The reason this happens is because {{ post.link }} gets the permalink as soon as you call it. Timber uses get_permalink() behind the scenes, which always use the current site.
In order to make this work, you’d have to switch sites with switch_to_blog() before you use {{ post.link }}. And after you accessed all the data you need, you can use restore_current_blog() to switch back to the previous site.
{% do fn('switch_to_blog', blog_id) %}
{{ post.link }}
{% do fn('restore_current_blog') %}
Apparently, you’d have to know the ID of the site the post is from. If you use a plugin to search across your multisite, it should give you a possibility to access the current blog ID.
For example, if you use Relevanssi with its Multisite Search feature, then the current blog ID can be accessed through {{ post.blog_id }}. In that case, it would work like this:
{% do fn('switch_to_blog', post.blog_id) %}
{{ post.link }}
{% do fn('restore_current_blog') %}

twig form multiple block in symfony4

I have a form with block of the different input field.
When I use, standard twig function I get one block:
{{ form(form) }}
If I want to change something inside form I use, for example:
{{ form_start(form) }}
{{ form_widget(form.firstName) }}
{{ form_widget(form.lastName) }}
{{ form_end(form) }}
And everything is fine with this, but here I use JS for adding multiple block of the same field in the form (like can be possible to add multiple person in one form). When I want to edit data, I catch all data from the DB, of course, and want to show blocks in the twig.
{{ form_start(form) }}
{# somehow start loop data from the DB here #}
<div class='block'>
{{ form_widget(form.firstName) }}
{{ form_widget(form.lastName) }}
</div>
{# somehow end loop data from the DB here #}
{{ form_end(form) }}
Is it possible in the Twig, or I should use old school here?
For all who google for same question, answer is here:
http://symfony.com/doc/current/form/form_collections.html

Symfony : easy way to format FORMS with CSS?

We're building a web site with Symfony 2. We generate a unique URL and send it by email to user who forgot their password, so they can reset their password.
We're building a simple form to reset a password. We have two labels ('Enter your new password' and 'Enter your new password again') with a textbox beside each.
We wanted the textbox to align with each other.
Lazy solution was to figure out two strings of the same length (!)
But I would have wanted to format them with CSS or put them in a table ...
Is that possible at all with Symfony's form ? I read documentation about customizing templates, but when we tried the solution proposed by Symfony's docs the widgets (textboxes) were not rendering ...
Here are some pics of the issue :
Crooked textboxes
Lazy solution
Here is the code of the twig where I think(!) the formatting should be done
{% block blockPrincipal_mp %}
<h1>{{ titre }}</h1>
{{ form_start(form) }}
<div class="containerForm">
<div class="error">
{{ form_errors(form) }}
</div>
{{ form_rest(form) }}
{{ form_end(form) }}
{% for flashMessage in app.session.flashbag.get('success') %}
<div class="confirm"><p> {{ flashMessage }}</p></div>
{% endfor %}
</div>
{% endblock %}
You can render all the different elements of the forms individually as opposed to just rendering it all at one time with form_rest(form) as you have in your example. form_rest() is going to render whatever hasn't been rendered yet. And up to this point, All that's been rendered are the errors.
I don't know what your form property's names are but here's an example:
{{ form_start(form) }}
<div class="form_errors">{{ form_errors(form) }}</div>
{# output all pieces of the username element individually #}
<div class="form_label">{{ form_label(form.username) }}</div>
<div class="form_input">{{ form_widget(form.username) }}</div>
<div class="form_errors">{{ form_errors(form.username) }}</div>
{# output all pieces of the password element individually #}
<div class="form_label">{{ form_label(form.password) }}</div>
<div class="form_input">{{ form_widget(form.password) }}</div>
<div class="form_errors">{{ form_errors(form.password) }}</div>
{{ form_rest(form) }}
{{ form_end(form) }}
This way you can control what HTML wrappers surround each piece of your form elements.
Note that you can also output the username and password fields by doing...
{{ form_row(form.username) }} {{ form_row(form.password) }}
...and it will still output the label, widget and errors but will use the default layout for those form types that is defined in your twig templates. So you have more control of rendering the parts if you do them individually.
This is great for custom forms and custom templates, however you can also override the default form element's layout if you want more control over how individual form elements are rendered throughout your site, by extending the form fields template.
https://symfony.com/doc/current/form/form_customization.html
The RepeatedType field can be dispayed separately:
{{ form_row(form.password.first) }}
{{ form_row(form.password.second) }}
or more controlled:
{{ form_label(form.password.first) }}
{{ form_widget(form.password.first) }}
{{ form_label(form.password.second) }}
{{ form_widget(form.password.second) }}

Symfony2: setFlash with basic html in the message

I'm trying to set a flash message for my template that includes very basic anchor to another page in the app.
Using the standard method apparently won't do the job:
$this->get('session')
->setFlash('message', 'Some link');
I found some suggestions in Symfony's 2.0 forums, but they don't work either:
$this->get('session')
->setFlash('message', sprtintf('Some %s', 'link'));
$this->get('session')
->setFlash('message', sprtintf('Some %s', link_to('routeHere', 'link')));
Edit
In my template I render all flashes in the most common way:
{% for label, flash in app.session.getFlashes() %}
<div class="message {{ label }}">
{{ flash }}
</div>
{% endfor %}
What am I doing wrong here?
The content of {{ flash }} is escaped by Twig automatically. You need to use the raw filter like
{{ flash|raw }}

How to assign class to invalid Symfony 2 form field, rendered with Twig?

Lets assume that few fields values of my form, build on Symfony 2 and rendered with Twig are not valid and I received validation errors. I want not only to see this errors, but also assign special class to each invalid field.
For example:
<input type="text" class="error">
How can I do that? As I understand, there is need to redeclare my form template. Is there any working example how to assign attributes in case of concrete field validation failure.
All I found now, is that I need to set this class in form template:
{% set attr = attr|merge({'class': attr.class|default('') ~ (errors|length > 0 ? ' error' : '') }) %}
But what I don't understand is how to specify exact field? Any help appreciated.
This works for me:
<div class="input{{ form_errors(form.expiry) == '' ? '' : 'error' }}">
{{ form_widget(form.expiry) }}
</label>
You could also do
{{ form_widget(form.expiry, {'attr': {'class': form_errors(form.expiry) == '' ? '' : 'error'}}) }}
If you use
{{ form(form) }}
for showing your form, I am quite sure you can not accomplish what you want, or at least I am not aware of the possiblity.
If you use something like this
{{ form_row(form.task) }}
{{ form_row(form.dueDate) }}
I am still quite sure you can not get what you want.
My solution for what you need would be to make something like this:
{{ form_start(form) }}
{{ form_errors(form) }}
<div>
{{ form_label(form.task) }}
{{ form_errors(form.task) }}
{{ form_widget(form.task) }}
</div>
<div>
{{ form_label(form.dueDate) }}
{{ form_errors(form.dueDate) }}
{{ form_widget(form.dueDate) }}
</div>
<input type="submit" />
{{ form_end(form) }}
and to simply get information about validation errors from form object and then to replace {{ form_widget(form.task) }} with something like this
{{ form_widget(form.task, {'attr': {'class': 'error'}}) }}
in case that field task failed the validation.
Even more slow and time consuming solution would be to make small twig files that each and every one would actualy represent "your" design for view of each form field and then to call those "little twigs" with an argument which would again come from form object which contains those data about bad validation.
You can read more about form rendering where you actualy make your own form field designs here
http://symfony.com/doc/current/cookbook/form/form_customization.html

Resources