Special character issue in twig - php

I have try following code for custom block. it is working fine when I does add content from admin. but, just issue for special character.
admin\view\template\extension\module\theme_module.twig
<div class="tab-content">
{% for language in languages %}
<div id="tab8-language-{{ language.language_id }}" class="tab-pane">
<div class="form-group">
<div class="col-sm-10">
<textarea name="custom_block[{{ language.language_id }}][description]" data-toggle="summernote" data-lang="{{ summernote }}" class="form-control" id="input-description8{{ language.language_id }}">{{ custom_block[language.language_id].description ? custom_block[language.language_id].description }}</textarea>
</div>
</div>
</div>
{% endfor %}
</div>
admin\controller\extension\module\theme_module.php
if (isset($this->request->post['custom_block'])) {
$data['custom_block'] = $this->request->post['custom_block'];
} else {
$data['custom_block'] = $this->config->get('custom_block');
}
catalog\controller\common\header.php
$data['config_language_id'] = $this->config->get('config_language_id');
$data['custom_block'] = $this->config->get('custom_block');
catalog\view\theme\default\template\common\header.twig
{% set lang = config_language_id %}
{% if custom_block[lang]['description'] %}
{{ custom_block[lang]['description'] | convert_encoding('UTF-8', 'HTML-ENTITIES') }}
{% endif %}
when I does add content something like from admin: ľščťžýáíé
So, does Output: ľšÄťžýáíé

The correct way is to do this in the controller file.
For example in:
catalog\controller\product\category.php
Create your variable:
$data['my_var'] = html_entity_decode($data['my_var'][$this->config->get(‌​'config_language_id'‌​)]['description'], ENT_QUOTES, 'UTF-8');
And in catalog\view\theme\default\template\product\category.twig, echo it:
{{ my_var }}
Output:
ľščťžýáíé

I think you should do something like this
{{custom_block.lang.description | convert_encoding('UTF-8', 'HTML-ENTITIES')}}
You can also try
#Digicart suggestion

Related

How to put liquid code inside a php string

I want to put Liquid code in a metafield with value type string. This is the Liquid code:
{% unless shop.metafields.cmld == blank %}
{%- assign cmld = shop.metafields.cmld -%}
<div class="slider">
{%- for field in cmld -%}
<div>
<img src="{{ field | last }}" />
</div>
{% endfor %}
</div>
{% endunless %}
The Liquid code works fine when I try it on shopify.
The problem here is this error on the 1st row:
{%'(T_CONSTANT_ENCAPSED_STRING), expecting ')' in .....
Any suggestions? It looks like this:
$add_metafield= array(
"metafield" => array(
"namespace"=> $metafield_namespace,
"key"=> "something",
"value" => "{% unless shop.metafields.".$metafield_namespace." == blank %}{%- assign ".$metafield_namespace." = shop.metafields.".$metafield_namespace." -%}<div class="slider">{%- for field in ".$metafield_namespace." -%}<div><img src="{{ field | last }}" /></div>{% endfor %}</div>{% endunless %}",
"value_type" => "string"
)
);
Change from double to single quotes in <div class="slider"> and <img src="{{ field | last }}" />
changing it to -> <div class='slider'> and <img src='{{ field | last }}' />

Link to other page in Drupal 8

In Drupal 7, I used following codes to link to other pages. I have "Service" block and inside that block , I write like this.
<?php
global $base_url;
global $base_path;
$link = $base_url . '/sites/all/themes/bootstrap_business/images';
?>
<div><img alt="" src="<?php print $link?>/customer.png" /></div>
<p> Service</p>
and I save text format with PHP.
But for now Drupal 8, we don't have Text format option "PHP" and also I don't know how to write codes to connect with other page.
Anyone help me please? Thanks.
In drupal 8 you can use hook_preprocess_HOOK() to pass variables to twig files and call your variables like
<header class="main-header">
{{ title_prefix }}
{% if page.header and logged_in %}
{{ page.header }}
{% endif %}
{% if not logged_in %}
<a href="{{ front_page }}" title="{{ 'Home'|t }}" rel="home" id="logo" class="logo">
<img src="{{ base_path }}themes/custom/mytheme/logo-login.png" alt="{{ 'Home'|t }}" />
</a>
<h2 class="login-logo">{{ site_name }}</h2>
{% endif %}
{{ title_suffix }}
</header>
Please see https://www.drupal.org/docs/8/theming/twig/functions-in-twig-templates for more details
You can also include other pages using
{# this template is located in templates/layout.html.twig #}
{% extends "layout.html.twig" %}
{# this template is located in templates/user/profile.html.twig #}
{{ include('user/profile.html.twig') }}
See: https://www.drupal.org/docs/8/theming/twig/functions-in-twig-templates
In your example, you need to specify your theme directory. Just use:
<img src="/{{ directory }}/images/xyz.jpg">
Here, {{directory }} will resolve to directory of your current theme.
For preparing links to other fields. see above mentioned drupal page

Laravel - Redirect with variable to twig page

I've made a mail function with an try/catch situation. It checks if the mail template exists, if not it needs to return redirect / back to the page with a message(return redirect('dashboard')->with('status', "Mail doesn't exists");)
Is it possible to redirect with a variable using a twig page? Or is there just something that I am doing wrong?
This is my mail function(DashboardController.php)
public function MailContact($date_id, $contact_id)
{
try
{
$contact = Contact::find($contact_id);
$date = Date::find($date_id);
$mail = Mails::where('datetype_id', $date->datetype_id)->first();
$html = $mail->html;
Mail::to($contact->email)->send(new Anniversary($html));
return redirect('dashboard');
}
catch(\Exception $ex )
{
return redirect('dashboard')->with("Mail doesn't exists");
}
}
This is the twig page('dashboard.twig')
<div class="box-body no-padding" style="width: 100%; padding-top:20px;">
<ul class="telephoneKingList" style=" display: inline-block;
width: 100%;">
{% for dates in datestodayUser %}
{% if dates is not empty %}
<li data-id="{{ dates.id }}">
{{ dates.contact.fullname }}, {{ dates.contact.relation.company }} <br>
{{ dates.description }}<br>
{# Place where Error needs to be #}
<a role="button" id="edit-button" href="/dashboard/mail/{{ dates.id }}/{{ dates.contact.id }}" class="btn btn-primary btn-sm"><i class="fa fa-envelope"></i></a>
</li>
<hr>
{% endif %}
{% else %}
<li>
Geen data beschikbaar.
</li>
{% endfor %}
</ul>
You can do
return redirect('dashboard')->with(['message'=>"Mail doesn't exists"]);
and then in your twig
{{message}} will give you the message
Use
redirect('dashboard')->withErrors(["Mail doesn't exists"])
and in your twig you need to access the errors using
{{ errors.first() }}
Laravel has a middleware included out of the box called ShareErrorsFromSession which shares the session errors with all views.

symfony 3 - url image from database

I wanted to display the url of an image from bdd in my file twig I receive an error of the image like the:
this is my bdd :
and my view from twig :
{% extends '#App/layout.html.twig' %}
{% block title %} Illustration {% endblock %}
{% block body %}
<div class="col-md-4 col-md-offset-4 text-center">
<h2> {{ imageFormation.image.nom }}</h2>
<hr>
<img src=" {{ imageFormation.image.url |raw }} "/><br><br>
</div>
{% endblock %}
Can you help me solve my problem please?
i dont know why i cant get the real image.
edit :
/**
* #Route("/formation/{formation}" , name="image")
* #param $image
* #return \Symfony\Component\HttpFoundation\Response
*/
public function imageFormationAction($formation){
$doctrine = $this->container->get('doctrine');
$em = $doctrine->getManager();
$imageRepository = $em->getRepository('AppBundle:Formation');
$imageFormation = $imageRepository->findOneByid($formation);
return $this->render('#App/formation/formationImage.html.twig', array(
'imageFormation' => $imageFormation
));
}
Your image url must be relative to web directory, like this fichier\Mention.jpg, not the image's path in your file system C:\xampp\htdocs\Symfony\MonProjetCv\web\fichier\Mention.jpg.
Save a relative path to your image file in the database (url equal fichier\Mention.jpg), and use asset function in your twig file:
{% extends '#App/layout.html.twig' %}
{% block title %} Illustration {% endblock %}
{% block body %}
<div class="col-md-4 col-md-offset-4 text-center">
<h2> {{ imageFormation.image.nom }}</h2>
<hr>
<img src=" {{ asset(imageFormation.image.url); }} "/><br><br>
</div>
{% endblock %}
Can you please try this way?
<img src='{{ asset("IMAGE_DIRECTORY_PATH/"~ image.url)}}' />

Exception: The merge filter only works with arrays or hashes in "HerzultForumBundle:Topic:new.html.twig"

I'm improving my web application by adding more languages. I've done it following this tutorial: https://coderwall.com/p/eiqd_g
Everything seemed to work fine, but now I've figured out that when trying to create a new post on the forum (HerzultForumBundle), I get the above exception.
I can see in the debugger:
in kernel.root_dir/cache/dev/classes.php at line 8422
function twig_array_merge($arr1, $arr2)
{
if (!is_array($arr1) || !is_array($arr2)) {
throw new Twig_Error_Runtime('The merge filter only works with arrays or hashes.');
}
return array_merge($arr1, $arr2);
}
at twig_array_merge (null, array('_locale' => 'en'))
in kernel.root_dir/cache/dev/twig/c7/1/9188032d83474dae4ab5fad0cdaf278f4614c031cf7a5531428c5812bd57.php at line 200
echo ">
\t <a href=\"";
// line 81
echo twig_escape_filter($this->env, $this->env->getExtension('routing')->getPath($this->getAttribute($this->getAttribute((isset($context["app"]) ? $context["app"] : $this->getContext($context, "app")), "request"), "get", array(0 => "_route"), "method"), twig_array_merge($this->getAttribute($this->getAttribute((isset($context["app"]) ? $context["app"] : $this->getContext($context, "app")), "request"), "get", array(0 => "_route_params"), "method"), array("_locale" => (isset($context["locale"]) ? $context["locale"] : $this->getContext($context, "locale"))))), "html", null, true);
echo "\">";
echo twig_escape_filter($this->env, (isset($context["locale"]) ? $context["locale"] : $this->getContext($context, "locale")), "html", null, true);
echo "</a>
So it seems like the twig_array_merge() function is getting a null value as first parameter. And the HerzultForumBundle:Topic:new.html.twig template seems to be the cause.
The HerzultForumBundle:Topic:new.html.twig template holds the below code:
{% extends 'HerzultForumBundle::layout.html.twig' %}
{% block title %}New Reply{% endblock %}
{% block content %}
<div class="forum post_new">
<ul class="crumbs">
<li>Forum</li>
<li>{{ topic.category.name }}</li>
<li>{{ topic.subject }}</li>
<li>New Reply</li>
</ul>
<div class="main">
<h2>New Reply</h2>
<form action="{{ url('herzult_forum_topic_post_create', { 'slug': topic.slug, 'categorySlug' : topic.category.slug }) }}" method="post">
{{ form_widget(form) }}
<div>
<button type="submit" name="reply">Add post</button>
</div>
</form>
</div>
<div class="side">
<p>Back to the topic</p>
</div>
</div>
{% endblock %}
My guess is that the exception comes from this line:
<form action="{{ url('herzult_forum_topic_post_create', { 'slug': topic.slug, 'categorySlug' : topic.category.slug }) }}" method="post">
But don't know how to solve it. Any idea? The forum was working perfectly before.
Well, here was my problem, in the base template:
<ul id="languages">{% for locale in ['en', 'fr', 'es', 'de'] %}
<li {% if locale == app.request.locale %}class="active"{% endif %}>
{{ locale }}
</li>
{% endfor %}
</ul>
So figured that app.request.get('_route_params') was null. I tried:
<ul id="languages">{% for locale in ['en', 'fr', 'es', 'de'] %}
<li {% if locale == app.request.locale %}class="active"{% endif %}>
{% if app.request.get('_route_params') %} {{ locale }}{% endif %}
</li>
{% endfor %}
</ul>
And yes! It works. I won't have translations on the forum but never mind, I think it's pretty understandable in english.
I had a similar bug "The merge filter only works with arrays or hashes in ..." in the template. whatever line it was, whatever change i did the line of the error moved but the message remained the same..
It appeared after I added i18n compatibility to my site only on the page where there was a form.
the reason was me using a ->forward() to another template depending on the presence of error or not.
So this error is indeed linked to routing problem.
I ended up rendering the template instead of forwarding to another page.

Categories