I have a date field (field_date) and on the right of this field I have to display the day of the week
-
1To do this you must create a custom field formatter and get the weekday using this function, api.drupal.org/api/drupal/….– user72672Sep 9, 2019 at 9:18
-
Are you trying to display the day on the admin interface or on the front-end interface?– misterdidiSep 9, 2019 at 9:22
-
Interesting, especially for the personalization of the field. I read the func in drupal.api, but I didn't understand where to put it. the front-end interface– GaetanoSep 10, 2019 at 11:24
2 Answers
You can change how dates from date fields are displayed by changing it on the Manage display
(/admin/structure/types/manage/[my_CT]/display) page for that Content type.
Custom date format for individual field
The quick and dirty way of doing it is by choosing Custom
for Formatter and entering the code for the date, for example:
Y-m-d\TH:i:s, l
will give you "2019-09-09T17:30:19, Monday".
You can lookup the various date and time codes in the PHP manual: https://www.php.net/manual/en/function.date.php
Overriding individual fields displays with custom date formats isn't the best way to go because you can't reuse it throughout the site, for other fields, or translate into other languages (different countries have different conventions about how to write dates).
Reusable Custom date format
The best way is to first add the new Date format to your site. This way you will be able to use anywhere where you can format Date output.
Go to Configuration > Regional and language > Date and time formats
and Add a new format.
Name it so it makes sense to your case, enter the PHP date code you want into the Format string
, select Language and add the format.
Now visit your Content type's Manage display
page and change the Formatter
of the Date field you want to affect to Default
, and the Date format
to the one you have just created.
If your question is for showing the day on the content create/edit form, and not displaying after saving that form (which I answered in the previous answer), you'll have to change the widget for the date field.
You can do that through some custom code, but I believe the Bootstrap Datepicker module can do that too (I haven't tried it).
That module has the Date format
option where you can enter for example
mm/dd/yyyy DD
to get
09/10/2019 Tuesday
in the Date input field after selecting a date from the calendar popup.
-
Yes thank you, that's exactly what I have to do. I saw the datepicker mosule, but I would like to create a customized code, because then I would have to visualize the form and other information too and at this point instead of installing more modules, the code will be better analyzed. I read something about how to edit custom fields and I also read that "allowed options" could also help, but I saw a partial example. Could you please give me a complete example of both the custom fields and the "allowed options" interface? Thanks– GaetanoSep 14, 2019 at 14:28
-
What do you mean by "allowed options"? If you plan on creating a custom field in a custom module maybe you should open a new question and ask for the code solution specifically. You can probably find questions and answers here that can help, for example: drupal.stackexchange.com/questions/64928/…– prkosSep 16, 2019 at 15:28
-
sorry I was referring to the "allowed values" described in this article. But I would need a complete example to better understand how to interact with drupal thanks– GaetanoSep 18, 2019 at 14:34