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
Related
I have rendered all my pictures from gallery, but need to get them to slider in my twig file.. is that possible without using any plugin, please???
My code, php:
<?php
$context = Timber::get_context();
$context["post"] = Timber::get_post();
Timber::render( 'pages/single/single-one-collection.twig', $context, CACHE_TIME);
My twig:
{% for picture in post.get_field('oc_gallery') %}
<div class="section" id="section{{ loop.index }}">
<img src="{{ TimberImage(picture).src | resize(400, 266)}}" alt="" />
</div>
{% endfor %}
So i'm trying to gererate a frontend user list, but i'm having trouble reaching the avatar url.
i'm able to get all user names but when i try to do the same with the profile picture the fallback is shown.
The {{ user.avatar.url }} is working on the user page when someone is signed in.
I've tried to look for the query used on the backend to get the user avatar on the preview, but i was not able to find it.
I don't know if this is relevant but i'm using https://octobercms.com/plugin/netsti-uploader for frontend users to upload their avatars. It's working since if i upload it on the frontend the backend user preview shows the right avatar
This is what i am using to get all users:
CODE:
use October\Rain\Auth\Models\User;
function onInit() {
$this['activatedUsers'] = User::whereIsActivated(true)->get();
}
MARKUP
<div>
{% for user in activatedUsers %}
<div class="card list">
{% if user.avatar %}
<img class="userimg" src="{{ user.avatar.url }}">
{% else %}
<img class="userimg" src="assets/images/user.png">
{% endif %}
<p class="name"><span class="rank-title">NAME</span><br>{{ user.name }} {{ user.surname }}</p>
{% if user.last_login %}
<p><span class="rank-title">LAST UPDATE</span><br>{{ user.last_login }}</p>
{%endif%}
</div>
{% endfor %}
All help is appreciated, thanks
try to use it like that.
use RainLab\User\Models\User;
function onInit() {
$this['activatedUsers'] = User::whereIsActivated(true)->get();
}
Markup
{% for user in activatedUsers %}
<div class="card list">
{{ user.avatar.path }}
</div>
{% endfor %}
Have a look at October\Rain\Database\Attach\File class to see available methods :
getThumb($w,$h,$options) - Generates and returns a thumbnail path
getPath() - Returns the public address to access the file
getLocalPath() - Returns a local path to this file. If the file is stored remotely,it will be downloaded to a temporary directory.
getDiskPath() - Returns the path to the file, relative to the storage disk
e.g :
{{user.avatar.getThumb(200,200, { mode : 'crop' } )}}
install "Frontend File Uploader for Model" plugin
insert {% component 'imageUploader' %} in your markup
insert
function onInit()
{
$user = Auth::getUser();
if($user){
$component = $this->addComponent(
'NetSTI\Uploader\Components\ImageUploader',
'imageUploader',
['modelClass'=>'RainLab\User\Models\User','modelKeyColumn'=>'avatar', 'deferredBinding' => false]
);
$component->bindModel('avatar', $user);
}
}
on your code section
You can query the backend user model with with 'avatar'.
use Backend\Models\User;
...
$user = User::where('id', $author_id)->with('avatar')->first();
...
<h1>{{ user.avatar.path }}</h1>
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)}}' />
I try to display an image which is in web/uploads/img
this image has the following name: Screenshot_2014-12-24-23-01-17.png
1/ when doing this, it works perfectly:
<img src="{{ asset('uploads/img/Screenshot_2014-12-24-23-01-17.png') }}" />
2/ when doing the following (the path of the image is in a variable) nothing is displayed :
<img src="{{ asset(array_image_WebPath[t]) }}" />
( with t = 1 and array_image_WebPath[1] = uploads/img/Screenshot_2014-12-24-23-01-17.png)
The fact is that I need to store my webpath of my images in an array. So what is the proper way of doing it?
EDIT: I tried with objects directly doing the following: {% for image in liste_images %} '<'img src="{{ asset(image.webpath)}}"/> {% endfor ‰} AND IT WORKS but I need to use my array to get the right image at the right place.
Go with a for...in:
{% for image in array_image_WebPath %}
<img src="{{ asset(image) }}" />
{% endfor %}
And access the loop index with either loop.index or loop.index0 [respectively 1 and 0 based].
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.