to set the format of the date to the local regions requirements requires that the use of hook_form_alter() to set the #date_format. This patch adds a new dropdown that can be used to set the desired date format.

CommentFileSizeAuthor
#55 mr-47-changes-for-in-between-dates-2858610-55.patch3.48 KBgueguerreiro
#44 better_exposed_filters_allow_the_date_format-2858610-23_2.patch5.83 KBOdai Atieh
#44 better_exposed_filters_allow_the_date_format-2858610-44.patch10.82 KBOdai Atieh
#37 interdiff-31-37.txt4.06 KBjordan.jamous
#37 better_exposed_filters_allow_the_date_format-2858610-37.patch10.77 KBjordan.jamous
#35 better_exposed_filters_allow_the_date_format.patch5.77 KBhinal-p
#33 better_exposed_filters_allow_the_date_format.patch5.77 KBhinal-p
#28 better_exposed_filters_allow_the_date_format-2858610-28.patch5.75 KBV.Hilkov
#26 better_exposed_filters_allow_the_date_format-2858610-23.patch5.82 KBDimaS11
#25 better_exposed_filters_allow_the_date_format-2858610-23.patch5.76 KBDimaS11
#24 better_exposed_filters_allow_the_date_format-2858610-23.patch6.2 KBDimaS11
#22 better_exposed_filters_allow_the_date_format-2858610-22.patch5.69 KBHenry Tran
#21 better_exposed_filters_allow_the_date_format-2858610-21.patch9.12 KBHenry Tran
#12 better_exposed_filters_allow_the_date_format-2858610-17.patch5.59 KBmartinadim
#9 allow_the_date_format-2858610-9.patch5.35 KBDanielVeza
#8 interdiff_5-8.txt11.54 KBtame4tex
#8 allow_the_date_format-2858610-8.patch13.59 KBtame4tex
#5 allow_the_date_format-2858610-5.patch5.04 KBcslevy
#4 allow_the_date_format-2858610-4.patch3.06 KBGRO
#2 0001-Issue-2858610-allow-the-date-format-to-be-set-from-t.patch3.34 KBgordon
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

gordon created an issue. See original summary.

gordon’s picture

Assigned: Unassigned » gordon
Status: Active » Needs review
FileSize
3.34 KB

this patch allows you to set the date format via the settings page

mikeker’s picture

Status: Needs review » Needs work

@gordon, Thank you for your work on this issue!

A few things to consider:

  • If we add a BEF option, we have to make a similar change to the schema so it is translatable
  • This patch does not work with "core" date fields such as "authored on"
  • I'm curious if we want to make this a JS-only option and avoid using the #date_format attribute (not sure on this... Need to give it more thought)

I'll try to find time to work on this tomorrow.

GRO’s picture

Patch re-roll.

cslevy’s picture

For me the patch from #4 wasn't working correctly, so I recreated the patch with a couple of changes

cslevy’s picture

Status: Needs work » Needs review
joachim’s picture

Status: Needs review » Needs work
+++ b/src/Plugin/views/exposed_form/BetterExposedFilters.php
@@ -508,6 +508,30 @@ Title Desc|Z -> A</pre> Leave the replacement text blank to remove an option alt
+        $bef_options[$label]['more_options']['bef_date_format'] = array(
+          '#type' => 'select',
+          '#title' => $this->t('Date format'),
+          '#default_value' => isset($this->options['bef'][$label]['more_options']['bef_date_format'])?$this->options['bef'][$label]['more_options']['bef_date_format']:'m/d/Y',
+          '#options' => array(
+            'm/d/Y' => 'm/d/Y',
+            'd-m-Y' => 'd-m-Y',
+            '' => $this->t('Other'),
+          ),
+        );
+        $bef_options[$label]['more_options']['bef_date_format_other'] = array(
+          '#type' => 'textfield',
+          '#title' => $this->t('Custom Date Format'),
+          '#default_value' => !empty($this->options['bef'][$label]['more_options']['bef_date_format_other'])?$this->options['bef'][$label]['more_options']['bef_date_format_other']:'',
+          '#description' => $this->t('Use PHP date() formatting options enter your custom format. Also note that this format needs to be compatible strtotime() to return the correct timestamp'),
+          '#states' => array(
+            'visible' => array(
+              ':input[name="exposed_form_options[bef][' . $label . '][more_options][bef_date_format]"]' => array('value' => ''),
+            ),
+          ),
+        );

This patch works, but rather than offering some hardcoded options and a custom textfield, I think we should be using the site's configured date formats (those at admin/config/regional/date-time).

tame4tex’s picture

Patch #5 did not include the code to apply the date format settings to a standard Drupal date field. I agree with @joachim that we should be utilizing the site's configured date formats but this patch DOES NOT include that functionality.

Still left to be done:

  1. Determine if the `#date_format` attribute is necessary
  2. Utilize the site's configured date formats instead of hard coding options
DanielVeza’s picture

I took a slightly different approach to this, would be interested to hear feedback.

Instead I made a config form at /admin/config/better_exposed_filters/config that lets you set the date at the m-d-Y or d-m-Y format.
Originally I wanted to set this to use the Drupal date formats, but they don't convert well to the jQuery date formats, and the huge convert code in this module isn't a helper function for some reason (Really should be, issue for another day).

Less flexible per view/datepicker I suppose, but I don't think there should be many reasons to have different datepicker formats on one site.

robin.ingelbrecht’s picture

When installing this patch and afterwards enabling a random module, following error is thrown:
Invalid data type in config better_exposed_filters.config

DanielVeza’s picture

Hmm. Had you saved a date format in the admin UI yet?

It might need a default value or a few more empty checks.. I'll review today/tomorrow when I get some time! I'm using this patch on a few sites and it's working well.

martinadim’s picture

Patch #9 thrown error "Invalid data type in config better_exposed_filters.config", when saving data type configurations. So change better_exposed_filters.config.yml.

DanielVeza’s picture

Ah nice thank you for cleaning that up.

bef_date_format: "\"

I'm not even sure how that got into my patch in #9. Hm. I must have accidently done that when using the Drupal console generator.

robin.ingelbrecht’s picture

Status: Needs work » Needs review
imclean’s picture

BEF 8.x-3.x doesn't really follow Drupal 8 coding styles or guidelines so it may be hard to get this in. Targetting 8.x-4.x may be a better option.

See: #3047739: [Meta] Roadmap to Stable 8.4.x Better Exposed Filters Release

danthorne’s picture

#12 (patch 17) working for me

imclean’s picture

Status: Needs review » Needs work

#7:

...rather than offering some hardcoded options and a custom textfield, I think we should be using the site's configured date formats (those at admin/config/regional/date-time).

imclean’s picture

There's also some code commented out. Either remove it or uncomment it and make sure it works.

Etroid’s picture

I agree we should be using the drupal date formats instead of hardcoding php date formats. This also allows for better localization. Active development is now happening on the 8.x-4.x branch. We should aim to get this patch updated for that version.

darvanen’s picture

Version: 8.x-3.x-dev » 8.x-5.x-dev

Bumping to version 5

Henry Tran’s picture

Hi everyone.
I've rerolled #12 to release 5.0-beta1
Cheers

Henry Tran’s picture

Hi everyone.
I've rerolled #12 to release 5.0-beta1.
Cheers

StefanieV’s picture

I used patch #17 on version 3.0.0-alpha6, which worked.
I have now upgraded to version 5.0.0-beta1 and changed the patch to #22 and I get the error:
Notice: Undefined variable: bef_add_js in Drupal\better_exposed_filters\Plugin\views\exposed_form\BetterExposedFilters->exposedFormAlter() (line 834...).

DimaS11’s picture

Fixed: Notice: Undefined variable: bef_add_js in Drupal\better_exposed_filters\Plugin\views\exposed_form\BetterExposedFilters->exposedFormAlter() (line 834...).
Added: more formats + 'dd.mm.yy' and 'mm.dd.yy'.

DimaS11’s picture

DimaS11’s picture

StefanieV’s picture

#26 works for me on 5.0.0-beta1 now. Thanks!

V.Hilkov’s picture

I can also confirm, that #26 is working. I found, that in file BetterExposedFilters.php it adds deprecated core/jquery.ui.datepicker library and better_exposed_filters/datepickers library as well. I don't see why core library should be used, bef datepicker library should have all that is required. I updated patch, only difference is that i removed this deprecated library

svendecabooter made their first commit to this issue’s fork.

svendecabooter’s picture

Status: Needs work » Needs review

I have created a merge request to reroll the last patch for 8.x-5.x and continue work on this.
I started with the patch at #28, with the following improvements:

  • Added custom permission instead of generic "access administration pages", since not all users with that permission should be able to set the format.
  • Added schema & default config file for newly introduced config object
  • Improved path and title for config form
  • Updated some code to be compliant with Drupal coding standards
svendecabooter’s picture

Assigned: gordon » Unassigned
hinal-p’s picture

Status: Needs review » Needs work

The last submitted patch, 33: better_exposed_filters_allow_the_date_format.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

hinal-p’s picture

I have made changes on patch #28 to make it compatible with the current bef module.

hinal-p’s picture

Status: Needs work » Needs review
jordan.jamous’s picture

Thanks guys for the efforts.

Continuing the work of @DanielVeza and rerolling #31 with the following enhancement:

  • Adding phpToJqueryDateFormat() converter function to reuse Drupal date formats
Alina Basarabeanu’s picture

Patch #37 works for me.
Drupal version 9.4
BEF version 5.2.0

junaidpv’s picture

Does patch from #37 really work? jQuery UI Picker always gives the format "m/d/Y" regardless of whatever format I choose in configuration setting.
Drupal: 9.3.13
BEF: 5.2

scuba_fly’s picture

#37 worked for me.

I needed to go to admin/config/regional/date-time and add a new format for the datepicker
After that I could select it in admin/config/system/better_exposed_filters
Now the datepicker in BEF shows the selected date when I click in the datepicker.
Drupal: 9.4.3
BEF: 5.2

rutiolma’s picture

It works for me but not with all date format patterns.
I noticed that patterns with slashes or spaces/commas, like "d/m/Y" or "l, d F Y" will not work. The only way I could make it work with patch #37 is by using a pattern like "d-m-Y" (plus following the tips provided by scuba_fly on comment 40)

hpower0809’s picture

#37 doesn't seem to work for me either. I added a new date format in the config but it keeps reverting to "m/d/Y"

Drupal: 9.4.8
BEF: 5.2

FiNeX’s picture

Hi, thanks for the patch. It solves the bug.

Odai Atieh’s picture

A warning appeared as below, so I've updated the latest patch to check if the filter is set or not before checking if datepicker is selected.

Warning: Undefined array key "filter" in Drupal\better_exposed_filters\Plugin\views\exposed_form\BetterExposedFilters->exposedFormAlter() (line 797 of modules/contrib/better_exposed_filters/src/Plugin/views/exposed_form/BetterExposedFilters.php).
Odai Atieh’s picture

Please ignore better_exposed_filters_allow_the_date_format-2858610-23_2.patch

Hotfingers’s picture

Hi, I tried #44, but it didn't work.
I had to use #37, but I also had to add a new date format, as @scuba_fly said in #40.
The new format must be set like "19-05-23" as "19/05/23" won't work.
Remember to clear cache.

Drupal: 9.5.5
BEF: 5.2

Alina Basarabeanu’s picture

Using Drupal Version 9.5.8, better_exposed_filters version 6.0.3, jquery_ui_datepicker 1.4.0 and patch #44 the date filter is working only with the 'd-m-Y' date format.

DieterHolvoet made their first commit to this issue’s fork.

DieterHolvoet’s picture

I started a fresh MR, not based on the previous patches, because I don't really agree with the approach there. Some differences:

  • A lot simpler, less code.
  • No global module config is added. The date format is stored in the filter widget config instead.
  • Every date format is now compatible. Before, only formats that were parseable by the strtotime function were supported.

Let's stop posting patches now in order to prevent duplicate work and potential confusion.

DieterHolvoet’s picture

Title: allow the date format to be set from the bef interface » Allow the date format to be set from the BEF interface
Status: Needs work » Needs review
borisson_’s picture

Status: Needs review » Needs work

I really like the latest MR, it is super clear. I made some small remarks in the MR.

DieterHolvoet’s picture

Status: Needs work » Needs review
borisson_’s picture

Status: Needs review » Reviewed & tested by the community

Thanks @DieterHolvoet!

gueguerreiro’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
3.48 KB

Merge request 47 worked perfectly for me, except until I needed to use an in-between date filter, which instead provides multi-date fields (min and max).

The exposedFormAlter() Seems to have special cases for the min/max fields already, but the same does not seem to exist for the exposedFormSubmit() and getElement() functions.

I made some changes to make it work on a project of mine. I'm not sure how to fork from @DieterHolvoet's existing branch, so I'm attaching a patch with the changes (Relative to the MR 47). It can be seen as a diff.

If you could review it and include it on your MR it would be greatly appreciated @DieterHolvoet!

DieterHolvoet’s picture

I'm not sure how to fork from @DieterHolvoet's existing branch

There should be a button at the top of the page saying Get push access. Afterwards click Show commands and you'll get the Git commands necessary to check out the branch locally.

gueguerreiro’s picture

Thank you @DieterHolvoet, I pushed my changes directly to the branch

smustgrave’s picture

Status: Needs review » Needs work

Will need a post update hook for the schema change.