Dynamic page description content from database using Symfony - symfony

What I hope to do is to change the content of description pages of site dynamically in database, for now the description page is loaded from yml file using sonata.seo.page like this :
//sonata_seo.yml
page:
default: sonata.seo.page.default
separator: ' | '
title: Name_site
metas:
name:
keywords: some keyword....
description: "this description stored in dataabse??"
I called it like this {{ sonata_seo_metadatas() }}
So I can change it anytime in admin dashboard of site, any idea?

Related

How to display the Content type custom fields in twig template?

I am very new to Drupal.
Can anyone tell me how to display the custom fields content type into twig template?
Thank you
I used below code with content and nodes.
{{ content.field_a }}
You'll have to follow these steps :
Step 1) Custom template for a content type
Suppose, you have to create a new template for content type i.e. Article ( machine_name : article ). Just make a copy of node.html.twig and rename with node--article.html.twig
Step 2) Call custom field
In this template, you can display content of your field like {{ content.field_test_field }}
Step 3) Clear Cache
If you're looking to create a full page template for an Article for example, you can create a file called : page--node--article.html.twig
Any custom content type would follow the pattern, for example if the content type is called Machine Product, the template would be: page--node--machine-product.html.twig (1 dash between spaces of the content type name.
For Drupal 8, this is my typical mapping for this filetype :
Blocks:
{{ page.REGIONNAME }}
Title:
{{ node.title.value }}
Taxonomy Terms/Dropdown Selects in Create Content:
{{ node.field_FIELDNAME.0.entity.label }}
General Text Fields:
{{ node.field_FIELDNAME.value }}
Images/Files:
{{ file_url(node.field_FIELDNAME.entity.fileuri')) }}

SeoBundle - title and description are not set

I have configured SeoBundle as stated in docs:
sonata_seo:
page:
title: Default title
metas:
name:
description: Defualt description
keywords: key1, key2
cmf_seo:
title: seo.title
description: seo.description
content_listener:
enabled: false
And placed in base.html.twig:
<head>
{# meta #}
<meta name=viewport content="width=device-width, initial-scale=1">
<meta name="format-detection" content="telephone=no"/>
{{ sonata_seo_title() }}
{{ sonata_seo_metadatas() }}
...
</head>
Now when I refresh page I can see that sonata_seo block title and description are set instead of cmf_seo. In docs there is a warning:
The title and description template is only used when the title is not
set on the content object or when the content object is not available,
otherwise it'll use the default set by the SonataSeoBundle. You should
make sure that the defaults also follow the template.
If only docs would clarify what is "content object" would be great... Anyway I tried removing title and description from sonata_seo block:
sonata_seo:
page: ~
cmf_seo:
title: seo.title
description: seo.description
content_listener:
enabled: false
Now my title becomes "Sonata Project" which is default SonataSeoBundle title.
How do I display cmf_seo title? {{ cmf_seo_title() }} is not working.
what is the value of seo.title and seo.description is it something you expect to be translated? Can you serve a longer stacktrace for the exception please?
In general CmfSeoBundle expects some placeholders (%%content_description%%, %%content_title%%, doubled to make it translatable at the end) to enrich the title/description with dynamic data. That dynamic data can live as SeoContent on the document (content object) you are persisting or as so called extractors which fetch the data from the document (content object). In the first case you have to implement SeoAwareInterface in second case you can find several extractors in the [doucmentation[(https://symfony.com/doc/current/cmf/bundles/seo/extractors.html).
If you wanna simply set static titles, you should stay with the SonataSeoBundle. We do simply enrich a template defined in cmf_seo.title|description with dynamic data from the current content. We do need sonata_seo.title for pages with no content documents (i.e. static pages).

Sonata admin bundle : Accessing the child edit route from an embed admin class

I'm having some headaches trying to , i guess, remake the wheel.
In sonata admin bundle , i'm trying to make a logical navigation through all my admin entities.
Each entities are related in cascade like this : programEntity -> LevelEntity -> ExerciceEntity -> somethingEntity -> etc
From what i've read , sonata admin bundle handle ( hope this will change ) only one embed relation between a parent and a child admin class .
The fact is not very user friendly to be able to edit / list parent's children and go back to dashboard to edit / list parent 's grandchildren
I'm currently trying to make a custom route for editing a parend's child by removing the parent's path from the edit route :
http://localhost/domain/admin/acme/app/parent/4/child/3/edit
i would like to replace this url by direct one like this :
http://localhost/domain/admin/acme/app/child/3/edit
This way i would be able to access grand children from the parent 's selected child.
I tried to override generateObjectUrl and generateUrl from sonata adminClass without a success, i have also in mind to override template for each of my entities but it's not very poo oriented .
any ideas ?
i was able to find a way to resolve this :
1) override base_list_field in your own bundle ( copy the files in vendor\sonata-project\admin-bundle\Sonata\AdminBundle\Resources\views\CRUD\base_list_field.html.twig in your own bundle like acme/bundle/resources/views/CRUD )
2) modify the file to looks like this :
{#
This file is part of the Sonata package.
(c) Thomas Rabaix <thomas.rabaix#sonata-project.org>
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
#}
<td class="sonata-ba-list-field sonata-ba-list-field-{{ field_description.type }}" objectId="{{ admin.id(object) }}">
{% if
field_description.options.identifier is defined
and field_description.options.route is defined
and admin.isGranted(field_description.options.route.name == 'show' ? 'VIEW' : field_description.options.route.name|upper, object)
and admin.hasRoute(field_description.options.route.name)
%}
<script type="text/javascript">
var route = "{{ admin.generateObjectUrl(field_description.options.route.name, object, field_description.options.route.parameters) }}";
var url = route.split(/(app)\//);
var tabUrl = url[2].split ( "/" );
alert ( 'test' ) ;
var new_url = '';
// case where adminclass is a child
if ( tabUrl.length == 5 )
{
new_url = url[0] + url[1] + '/' + tabUrl[2] + '/' + tabUrl[3] + '/' + tabUrl[4] ;
}
// case where adminclass is not a child
if ( tabUrl.length == 3 )
{
new_url = "{{ admin.generateObjectUrl(field_description.options.route.name, object, field_description.options.route.parameters) }}";
}
document.write( {%raw%}"<a href='"{%endraw%} + new_url + {%raw%}"'>{%endraw%}{%- block field %}{{ value }}{% endblock -%}{%raw%}</a>"{%endraw%} );
</script>
{% else %}
{{ block('field') }}
{% endif %}
if the tabUrl ( whitch is a split of the url with '/' ) find 5 results , it means that we are in adminclass embedded , otherwise it's a normal admin class listing .
the code is not very clean and optimized but it works .
3) update your config.yml
sonata_admin:
title: Bonk
title_logo: public/img/logo-admin.png
security:
handler: sonata.admin.security.handler.noop
templates:
# default global templates
layout: SonataAdminBundle::standard_layout.html.twig
ajax: SonataAdminBundle::ajax_layout.html.twig
dashboard: SonataAdminBundle:Core:dashboard.html.twig
# default actions templates, should extend a global templates
list: SonataAdminBundle:CRUD:list.html.twig
show: SonataAdminBundle:CRUD:show.html.twig
edit: SonataAdminBundle:CRUD:edit.html.twig
------>>> base_list_field: AcmeMyBundle:CRUD:base_list_field.html.twig
and
sonata_doctrine_orm_admin:
# default value is null, so doctrine uses the value defined in the configuration
entity_manager: '#doctrine.orm.entity_manager'
templates:
form:
- SonataDoctrineORMAdminBundle:Form:form_admin_fields.html.twig
filter:
- SonataDoctrineORMAdminBundle:Form:filter_admin_fields.html.twig
types:
list:
array: SonataAdminBundle:CRUD:list_array.html.twig
boolean: SonataAdminBundle:CRUD:list_boolean.html.twig
date: SonataAdminBundle:CRUD:list_date.html.twig
time: SonataAdminBundle:CRUD:list_time.html.twig
datetime: SonataAdminBundle:CRUD:list_datetime.html.twig
text: acmeMyBundle:CRUD:base_list_field.html.twig
trans: SonataAdminBundle:CRUD:list_trans.html.twig
string: acmeMyBundle:CRUD:base_list_field.html.twig
smallint: acmeMyBundle:CRUD:base_list_field.html.twig
bigint: acmeMyBundle:CRUD:base_list_field.html.twig
integer: acmeMyBundle:CRUD:base_list_field.html.twig
decimal: acmeMyBundle:CRUD:base_list_field.html.twig
identifier: acmeMyBundle:CRUD:base_list_field.html.twig
hope this will help !

symfony2 multilanguage message error with form validator through yml

I have a yml file for the user form validation (a form like many others):
# src/User/RegBundle/Resources/config/validation.yml
User\RegBundle\Entity\User:
properties:
name:
- NotBlank: ~
- Length:
min: 2
max: 255
minMessage: error_min_message
maxMessage: error_max_message
The form is rendered into the twig like below
//...
{{ form_row(form.name) }}
{{ form_row(form.surname) }}
{{ form_row(form.gender) }}
{{ form_row(form.email) }}
//...
My symfony2 website has a multilanguage structure and there are the messages.mylang.xliff file for the words translation.
All works right.
My question is whether there's a way to insert the "error_min_message" into the messages.mylang.xliff or there are others ways to translate that messages.
Validation strings go to validators.LANG.FORMAT instead of messages.LANG.FORMAT. Don't forget to clear the cache after you add a new translation file.

Limit amount of words displayed from MySQL on a Twig page

I wish to limit the amount of text displayed from a mysql statement. So for example, in my database I have a page where there are 1000 words contained in the content field, I want to be able to just display 200 of those words.
How can this be done using TWIG?
Cheers!
Sounds like your are looking for the "truncate" filter.
In your app/config/config.yml add::
services:
twig.extension.text:
class: Twig_Extensions_Extension_Text
tags:
- { name: twig.extension }
Then you can do in your templates:
{{ var.foo | truncate(200) }}
{{ "Hello good Sir!" | truncate(4) }}

Resources