Change Sonata Admin Logo based on database record - symfony

I have a module for manage images inside my Sonata Admin. I want to display those images where sonata logo is placed, how I can do that? I have the code for get the images in my controller and also the template to display the image but don't know how to use this from Sonata, any advice?

You can override the sonate base template (like you overide any other template) or any block listed inside of it.
there is a block called logo and it looks like this
{% block logo %}
<a href="{{ url('sonata_admin_dashboard') }}" class="brand">
<img src="{{ asset(admin_pool.titlelogo) }}" alt="{{ admin_pool.title }}" />
{{ admin_pool.title }}
</a>
{% endblock %}
In combination with a Twig-Extension it should be no problem to fetch the image out of the database

Related

Drupal overwrite view default markup

I am new in Drupal 8 and trying to build a custom theme. Overwriting a theme works well for paragraphs, blocks etc. but not for views. I need your help to tell me how to override the markup of a view. I already created a the .twig file in the custom folder and tested it. It works.
That's what I have inside:
{% for row in rows %}
<div{{ row.attributes.addClass(row_classes) }}>
{{ row.content }}
</div>
{% endfor %}
The {{ row.content }} gives me every element I need in the default Drupal view markup. I want to costumize the HTML. such as:
<h1 class="headline">
{row.title}
</h1>
<p class="text">
{row.text}
</p>
<img src="{row.image}" />
I didn't find a way to get the values such as the title from the row. I tried things like row.{field_name} or row.content.{field_name} but nothing works yet. Is there anyway to access these fields by machine name?
The easy way to display the contents would be to set the views to content list with a view mode. For e.g: If you want to display a node content type article with display mode teaser then you will get the node--article--teaser.html.twig getting rendered.
Then you can modify the the twig file in required format.
<h1 class="headline">
{{ node.title.value }}
</h1>
<p class="text">
{{ node.body.value }}
</p>
<img src="{{ node.field_image.entity.uri.value }}" />

Twig and Assetic: images names in web directory

To display an image in my Twig template I do
<img src="{{ asset('images/mylogo.png'}}" />
It works fine if I put manually my image 'mylogo.png' in the 'web' directory of my application. But I don't think it is a good way to go. I should use the command 'assets:instal web'
The problem is when I use the command 'assets:instal web' the name of my image is modified (for example the name would be fce32_mylogo_2.png) . . . I think it is assetic who is modifying the name of my image.
But then my Twig template do not find my image when I use to display that image.
Is it normal I put my image 'mylogo.png' manually in my web folder ? I'm not sure about that . . .
Edit: I know I can use this synthax:
{% image 'bundles/myBundle/img/mylogo.png' %}
<img src="{{ asset_url }}" class="img-responsive" alt="">
{% endimage %}
But this does not allow to pass twig variables. I would like to do this:
{% image 'bundles/myBundle/img/'~someVar.logoUrl %}
<img src="{{ asset_url }}" class="img-responsive" alt="">
{% endimage %}
clear your asset dumps and try this:
app/console assets:install web --symlink

Django Registration Redux Customize

I've been trying to get a clear, simple answer to this, but I can't seem to extract anything that makes sense from either the docs or from stack overflow.
I've implemented the standard templates that come with Django Registration Redux, but I can't seem to work out how to change the form styles and the text copy that are the defaults in Redux.
Here's my html:
{% extends 'base.html' %}
{% block title %}
Registration Form - {{ block.super }}
{% endblock title %}
{% block content %}
<div class="content-wrapper">
<div class="content">
<div class="pure-g">
<div class="is-center pure-u-1">
<div class="pure-u-1-2 thin-box">
<h1>Registration form</h1>
<form role="form" action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" class="s-button"/>
</form>
</div>
</div>
</div>
</div>
</div>
{% endblock content %}
As you can see I've styled the button, but none of the elements are available for styling. The redux data must be sitting somewhere, but I've no idea where it is.
Someone on SO has suggested using crispy forms, but I'm using pureCSS rather than bootstrap, and I've already gone to the trouble of setting up a set of form styles that I want to use, so it seems like overkill to install a module with a set of styles that I then have to over-write.
If i understood you correctly you would like to access individual fields. To access them you can get the fields' names here.
So in the template instead of {{ form.as_p }} you could use individual fields like this:
for e.g. {{ form.username }}.
If you also would like to add styling then you need to use filters like:
{{ form.username | add_some_css:"my_new_class" }}. This styling needs to be configured in a separate file for e.g. redux_css.py. To make your template use this file you need to include after {% extends 'base.html' %} the following {% load redux_css %}.
Now you need to create redux_css.py:
from django import template
register = template.Library()
#register.filter
def add_some_css(field, css):
"""
Adds css to fields in Redux templates
"""
return field.as_widget(attrs={"class":css})
All this should make your redux fields (which are text inputs) styled like: class="my_new_class"
Obviously you need to create .css files and "import" them into your .html files.
PS you might need to access fields errors as well, you can do this with {{ form.username.erros }}

How do I generate full URLs for optimised assets using Assetic in Symfony?

I want to include full asset URLs in my page templates, mainly so the Behat failed test page grabs still display properly. I've read this question, which suggests using absolute_url() as of 2.7, however I'm using the {% stylesheets %} or {% image %} tags to filter my assets.
Can anyone advise if there's a better way than doing something like this...
{% image
'#AcemBundle/Resources/public/imgs/logo.jpg'
output='compiled/imgs/logo.jpg' %}
<img src="{{ app.request.getSchemeAndHttpHost() ~ asset_url }}" alt="Logo"/>
{% endimage %}
... ?
Updated
Added full {% image %} tag I'm using.
Use it like this:
<img src="{{ asset('bundles/mybundle/assets/img/logo.png') }}">

One django template not picking CSS, other templates are fine

I have two django templates in my one folder. The template for the url localhost:8000/people picks CSS correctly which is located at /m/css/style.css
The other template for the url localhost:8000/people/some-name in the same folder is trying to retrieve CSS from people/m/css/style.css
Why is this second template not picking CSS like the first one?
My erring second template is like this:
{% extends "base.html" %}
{% block page_title %}{{ entry.name }} | {{ block.super }}{% endblock %}
{% block main %}
<h1>{{ entry.name }}</h1>
{{ entry.body|linebreaks }}
{% endblock main %}
As you can see there's nothing in the template that could cause problem.
It looks to me like your templates are looking for a stylesheet located at ../m/css/style.css. That's why the template in /people works - /people/../m/css/style.css refers to /m/css/style.css. However, /people/some-name/../m/css/style.cssrefers topeople/m/css/style.css`, not the desired address.
Make sure the templates are looking for /m/css/style.css - emphasis on the very first / character.

Resources