Linking in Symfony - symfony

I am trying to create a link for FOSUser Bundle.. It seems pretty straight forward, but for some reason i am having a problem..
i want to create a "Login" link inside my twig.
<a class="Link" href="{{ url('/login') }}">Login</a>
which should direct to
http://localhost:9911/Symfony/web/app_dev.php/login as defined in my controller.
Am I doing this incorrectly?

If you want to link to routes you can just use {{ path() }} or {{ url() }} and use the name of the route as argument. I was just about to tell you that you should read the documentation, but it actually doesn't mention the login-route. Who would have guessed?
You can find out the name of a route by looking into Resources/config/routing/*.yml. In your case the route is named fos_user_security_login, therefore linking to this route is as easy as using:
login

Related

How to retrieve Domain from Email Address in Twig

With Twig, how can we get domain from an email address which is a string?
<p>{{ app.user }}</p>
<p>{{ to_display_domain_name_here }}</p>
Where app.user is xyz#gmail.com
Want to retrieve domain gmail or gmail.com from this.
You can create a custom twig filter and use regex to get domain.
Custom filter: http://symfony.com/doc/current/templating/twig_extension.html
Regex example: https://gist.github.com/Avidid/6114724
The best way to do it, in my opinion, would be create a custom Twig extension. You can find more info about how to do it in the official documentation.
For a quick and dirty way I would probably use the split filter which actually behaves like an explode in Twig:
{{ app.user|split('#')[1] }}

How to display errors in overriden FOSUserBundle Registration form?

I have overridden the registration .twig files and it works except when the user doesn't enter matching passwords. How do I display any errors that are thrown by the registration process?
You can render your form errors by using form_errors function, like this:
{{ form_errors(form) }}
{{ form_errors(form.someField) }}
You should read this official documentation How to Customize Form Rendering to understand in deep how Symfony rendering your form view and how to custom the template as you want.

How to get the field_image path with Twig in Drupal 8

To make an animated slider, I overwrite a fields view template file in my own template named: views-view-fields--slider.html.twig
Inside that file I have access to a fields array to print out all the fields that are in my content-type slide.
The problem is that I need to get the path of the image file instead of the image itself, because I need the path for CSS background-image styling.
The following doesn't work:
<div class="jumbotron " ref="" style="background-image:url(?????);">
{{ file_url(fields.field_image.entity.fileuri) }}
</div>
I tried everything I good find on Google:
fields.field_image.entity.uri
fields.field_image.entity.uri.value
fields.field_image.entity.url
fields.field_image.entity.url.value
etc.
Has anyone an idea how it is done in Drupal 8 ?
Since you are in views-view-unformatted you should already have access individualy to each field of your content type Slide with {{ row.content }} and because of that you have to do:
{{ file_url(row.content.field_image.entity.fileuri) }}
Try {{ file_url(row._entity.field_image.entity.uri.value) }}.
It works for me.
<div style="background-image:url({{ file_url(row._entity.field_image.entity.uri.value) }});"></div>
I found this module: image_url_formatter, so in View interface, I can use this to formatter my image filed as path.
It works perfect, but if I turned the twig debug on, it wouldn't work, because it will output debug annotation. I don't know how to solved it yet.
I use fields.field_image.content to show the path, I don't konw whether it's rignt.
Solution without extra module:
In your Views:
1.Add your image filed to the Advanced/Relationship
2.Then in the filed section, change to File Group, you can see URI field,add it, and make sure you check "Display the file download URI"
3.Add your new field in the twig template: {{ field.uri.content }}
Done!

Twig - Get URL for canonical tag

I'm looking to create a dynamic rel="canonical" tag in my application which pulls in the current URL but want to ensure any query parameters are removed. E.g http://www.example.com/test/?page=2 should have the canonical as http://www.example.com/test/, therefore {{ app.request.uri }} doesn't work as this pulls in ?page=2 as well.
Does anyone know how to pull in the absolute path of a page without the query parameters?
This will work,
{{ url(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')) }}
I just tried to dump baseUrl and can confirm that it does not work.
However, this works:
{{ app.request.getSchemeAndHttpHost ~ app.request.baseUrl ~ app.request.pathInfo }}
I know, it's not pretty but it does the job :)

Get current URI in Twig template

Does anybody know how to get the current URI in a Twig template?
I’ve read through the documentation and I’m unable to find the Twig function to do this.
{{ app.request.uri }}
If you want to read it into a view variable:
{% set uri = app.request.uri %}
The app global view variable contains all sorts of useful shortcuts, such as app.session and app.security.token.user, that reference the services you might use in a controller.

Resources