I am making application with symfony2 and sonata-admin bundle.
public function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('id')
->add('fromDate');
it shows time like this
September 1, 2013 02:00
so I changed
- ->add('fromDate');
+ ->add('fromDate',null,array('format' => 'yyyy-MM-dd HH:mm:ss'))
but it still show the same.
please give me how to use format for time display?
Try using
->add('fromDate','datetime',array('date_format' => 'yyyy-MM-dd HH:mm:ss'))
By the way, the relevant code is here. I think your format option gets overwritten by the system, so it's important to use date_format instead. For an application-wide solution, have a look at this question too.
If you use SonataIntlBundle then date formats are:
$list->add('createdAt', 'date', array(
'pattern' => 'yyyy-MM-dd',
))
described in https://docs.sonata-project.org/projects/SonataIntlBundle/en/3.x/reference/datetime/#php-usage
and its formats here: https://www.php.net/manual/en/class.intldateformatter.php
Nothing from the above had worked for me, so I ventured to inspect the page source, which pointed me towards template used:
<!-- START
fieldName: date
template: SonataAdminBundle:CRUD:list_date.html.twig
compiled template: SonataAdminBundle:CRUD:list_date.html.twig
-->
<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="1">11.03.17</td>
<!-- END - fieldName: date -->
And after inspecting the template I've found the correct option filed name:
{%- elseif field_description.options.format is defined -%}
So I made my Admin code like this
->add('date', null, ['format' => 'd/m/Y'])
and it worked.
Note that format is the regular date() format, and one have to use just a single placeholder mark, like y for year, as yyyy produces 2017201720172017.
I know it's an old thread, but I hope it will help someone...
Try to add this, like carzogliore said (but quotes are missed from datetime):
->add('fromDate', 'datetime', array('format' => 'yyyy-MM-dd HH:mm:ss'))
You have to indicate that "DateTime" is the type you watn to use and then use the format key, for instance:
->add('fromDate',datetime,array('format' => 'yyyy-MM-dd HH:mm:ss'))
Internally, Sonata will use list_datetime.html.twig as you are refering "Datetime" as type and that contains format options. Below you can see the list_datetime.html.twig sonata base implementation:
{% extends admin.getTemplate('base_list_field') %}
{% block field %}
{%- if value is empty -%}
{%- elseif field_description.options.format is defined -%}
{{ value|date(field_description.options.format) }}
{%- else -%}
{{ value|date }}
{%- endif -%}
{% endblock %}
my 'problem' was different.
Because I have the sonata intl bundle active, it uses it to format the datetime list.
->add('startAt', 'datetime', array(
'dateType' => IntlDateFormatter::LONG,
'timeType' => IntlDateFormatter::SHORT
))
In the modern versions of Sonata I have managed to make it work like this:
$listMapper->add('dateadd', 'datetime', array('format' => 'y-m-d H:m:s'));
Related
I have a custom block which returns an array with a text-field.
How can I replace the string %current_year% in the #text property of the array with the current year in my twig template?
'0' => array(4)
'#type' => string(14) "processed_text"
'#text' => string UTF-8(119) "The current year is %current_year%."
I'm assuming your array/object is passed to the template as the variable myObject.
You can render the string in the #text property while replacing the placeholder as follows:
{{ attribute(myObject, '#text') | replace({"%current_year%": ("now"|date("Y"))}) }}
... or use an intermediate variable ...
{% set currentYear = "now"|date("Y") %}
{{ attribute(myObject, '#text') | replace({"%current_year%": currentYear}) }}
I have a show page and I want to add a custom value.
I have tried doing what I did in other actions which is to add an array to the
third parameter with the data key like so:
protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper
->add('name')
->add('dateEnd')
->add('example', null,
array('data' => 'example value')
)
;
}
In the configureListFields action, this works. I have injected custom values with the data attribute.
But still I am not able to access key example in the show.html.twig file.
It gives me this error
Variable "example" does not exist.
What should I do to access this custom variable in the twig file ?
Try
{{ elements.elements.example.options.data }}
in your twig template
I used this solution. In the configureShowFields() method of an Admin class:
$showMapper
->with('Tab Name')
->add(
'any_name',
null,
[
'template' => 'Admin/Custom/any_name_show_template.html.twig',
'customData' => $this->someRepository->getSomeEntityBy($field),
'anotherCustomData' => $this->someService->getSomeDataBy($value),
]
)
;
In the custom template, you can access custom data by field_description.options.<customFieldName>, so for provided example data accessors would be {{ field_description.options.customData }} and {{ field_description.options.anotherCustomData }}
For the shorter field name in the Twig template, you can do like this:
{% set customData = field_description.options.customData %}
and access the custom data like {{ customData }}
Hope this helps and saves time.
I want to add a class to the <a>-Tag of a Field that consists of a URL-link and a link text (it's a field of type "Link") and the name of the field is content.field_c_button_link
So with twig in my HTML-File I want to have something like this:
{{ content.field_c_button_link.0.addClass('button blue') }}
How can I add a class properly?
Why not piece the anchor tag together manually? That way you have complete control over everything. Something like this in your template
{{content.field_url.0['#title']}}
Ok, this is horrible but it's the only way I found to get this to work:
If you look at the default drupal build array for your link you should see that content.field_c_button_link.0 is an array(4)
'#type' => string(4) "link"
'#title' => string(15) "Big Blue Button"
'#options' => array(0)
'#url' => object Drupal\Core\Url(11)
So, to set classes directly on the <a> tag we have to load '#options' (which is presently empty) with the right setup of subarrays
'#options' => array(1)
'attributes' => array(1)
'class' => array(2)
string(6) "button"
string(4) "blue"
The only way I could find to do this within twig was to use a series of temps and merging them with the original array because twig wouldn't parse anything else I tried:
{% set temp = {'attributes': {'class': ['button','blue']}} %}
{% set temp2 = content.field_c_button_link.0 %}
{% set temp2 = temp2|merge({'#options': temp}) %}
{% set temp3 = content.field_c_button_link|without('0') %}
{% set temp3 = temp3|merge({'0': temp2}) %}
{% set content = content|merge({'field_c_button_link': temp3}) %}
Note the |without which is a Drupal/twig filter. I had to use it to remove the empty '0' element to avoid having the link print twice.
Please tell me there is an easier way.
My problem concerns just applying Twig filter inside a form without getting a 500 server error. Here is the code, which is in the view 'Room/new.html.twig':
{{ form_label(form.name) }}
or
{{ form_label(form.name, 'room.name') }}
Both will output: 'nom de salle' ('room name' in French). That is because, in my translation file 'messages.fr.yml', I have:
room.name: "nom de salle"
Also, I activated the translation in my form type 'RoomType.php' with this code:
->add('name', 'text', array(
'label' => 'room.name'
))
I tried every possible combination I thought of with |capitalize and got either a 500 server error or no change at all. Some examples:
{{ form_label(form.name)|capitalize }}
{{ form_label(form.name, 'room.name'|capitalize) }}
and so many more...
Does anyone know how to do it, it doesn't seem to be in the Symfony doc. Of course, I thought about duplicating the translation to have one in lower case and one starting with a capital letter but that would defeat the object. Plus there are other Twig filters I would like to use with this form_label() function.
Help greatly appreciated, thank you.
I just found the solution. It's so easy that it is embarrassing. I thought I didn't need the |trans as it was already translated but without it, 'room.name' = 'nom de salle' would transform to 'Room.name' instead of 'Nom de salle'. So the correct code is just:
{{ form_label(form.name, 'room.name'|trans|capitalize) }}
I am displaying a date time widget in a web page I am developing. By default time part only show hours and minutes. I need to show seconds too.
I have found that the date time wtdget has a variable called with_seconds, but I need to pass true from the form.
This is what I have but it did not work:
{{ form_widget(edit_form.fechhoramarcada, { 'attr': {'class': 'input-text no-border'}, 'with_seconds': true }) }}
Any help will be appreciated,
Thanks
Jaime
You can change default options when you creating them.
$builder->add('fechhoramarcada', 'datetime', array(
'with_seconds' => true,
));