$watch function on filter in angularJS - angularjs-filter

I am having the {{ content | lang }} expression in my .html file to update the text on browser based on lang filter. But it is not updating the page when I changed the language.
When I tried with {{ (content | lang) || content }} expression it works as expected.
I am not sure what is the difference in both of these.
is there anyone facing the same issue?
I am using angular 1.4.2 version.

Related

How can I display image of dynamic field in drupal 8?

I am new to learning Drupal 8.
I need your help in how can i display dynamic images using Twig template. Also I have tried with below syntax.
{{ file_url(market.getFieldCollectionItem().field_turnpike_image.entity.uri.value|e) }}
{{ file_url(media.entity.field_image.entity.uri.value) }}
Using above syntax I could not display images using in twig template. Also with this syntax I have got some errors.
Please any one help me out to how can i display images using twig template.
Thank you.
Use the following snippet to get the image if you are in node twig template
{{ file_url(node.field_image['#items'].entity.uri.value) }}
even
{{ node.field_image.entity.url }}

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!

How to trigger a Twig Extension function from a twig variables content

I want to be able to pass into twig some html in a variable and render it as
{{ data.content | raw }}
But within that content variable I would like to be able to have content like this - pulled from a database:
<div>
<p>Text with a Twig Extension function call eg: {{ doSomething('112233') }}</p>
</div>
Is there any way to get that twig extension to fire having come from the Twig variable itself?
Thank you.
There is one possible solution. Look at template_from_string twig function. It says:
New in version 1.11: The template_from_string function was added in Twig 1.11.
The template_from_string function loads a template from a string:
{{ include(template_from_string(data.content)) }}
But doc also says:
The template_from_string function is not available by default. You must add the Twig_Extension_StringLoader extension explicitly when creating your Twig environment.
So, probably it won't work out of box.

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 :)

Adding a link inside translated content in Twig template

Inside a Twig template I would need to have a translated text that contains a link (the path should be generated by the Router, not statically embedded).
Twig does not allow to render a variable inside a trans block - I'm also aware of the following:
{% trans with {'%name%': 'Fabien'} from "app" %}
Hello %name%
{% endtrans %}
but I can't see how to use that to inject inside the translation a piece like this
privacy policy
(of course, the anchor text should be translated as well)
The approach I've taken is this:
In the translation file:
page.privacy.policy: Please read our %link_start%privacy policy%link_end%
In the twig file:
<p>{{ 'page.privacy.policy' | trans({'%link_start%' : '', '%link_end%' : ''}, 'account') | raw }}</p>
I'm not sure if this can be done using the block syntax you mentioned above as I found it didn't work unless I piped the result of the translation through the 'raw' filter. Also I use message domains to split the translations, hence the 'account' parameter.
I think the closest to your example would be:
<p>{{ 'Please read our %link_start%privacy policy%link_end%' | trans({'%link_start%' : '', '%link_end%' : ''}) | raw }}</p>
EDIT:
The only issue with this approach I've come across is where I need to programmatically follow a translated link in a unit test as there isn't a single translation representing the link text. Although messy I think it would be possible to get round this by providing a separate translation for the link text and substituting it's translated value into the full text as an additional variable.
rebdirdo's solution is not really safe as it's not escaping whole message. It is not working correctly for messages like "don't use <b> tag, use <strong> tag instead. %link_start%Here%link_end% you can find why.", because the tags will be not escaped and will be not visible.
working approach:
translation file:
advises.strong: don't use <b> tag, use <strong> tag instead. %link_start%Here%link_end% you can find why.
twig file:
{{ 'advises.strong'|trans|nl2br|replace({'%link_start%': '', '%link_end%': ''})|raw }}
Note the nl2br filter. It is necessary to put some filter there to make the raw filter working only for the link tags.
This is a better way:
{{ 'Please read our %privacy_policy%'|trans({
'%privacy_policy%': ' ' ~ 'Privacy Policy'|trans ~ ''
})|raw }}
Twig:
{{'body.term'|trans('%link_terms%' :app.request.getSchemeAndHttpHost()~path('terms')},'AcmeTerm')|raw }}
AcmeTerm.yml
body
term: >
<ul>
<li>ReadTerms.</li>
</ul>
where path('terms') is the route
like:
it__RG__terms ANY ANY ANY /it/termini-e-condizioni
en__RG__terms ANY ANY ANY /en/terms-and-conditions

Resources