From c3f441260c5a2dcef257e3d754d77f2b3328973a Mon Sep 17 00:00:00 2001 From: Michael Stenta Date: Sat, 4 Feb 2023 10:45:25 -0500 Subject: [PATCH] Hide datetime time field element by default. --- modules/core/ui/theme/css/datetime.css | 3 +++ .../core/ui/theme/farm_ui_theme.libraries.yml | 9 ++++++++ modules/core/ui/theme/farm_ui_theme.module | 3 +++ modules/core/ui/theme/js/datetime.js | 23 +++++++++++++++++++ 4 files changed, 38 insertions(+) create mode 100644 modules/core/ui/theme/css/datetime.css create mode 100644 modules/core/ui/theme/js/datetime.js diff --git a/modules/core/ui/theme/css/datetime.css b/modules/core/ui/theme/css/datetime.css new file mode 100644 index 0000000000..80e5b51fb4 --- /dev/null +++ b/modules/core/ui/theme/css/datetime.css @@ -0,0 +1,3 @@ +a.toggle-time { + font-size: small; +} diff --git a/modules/core/ui/theme/farm_ui_theme.libraries.yml b/modules/core/ui/theme/farm_ui_theme.libraries.yml index b5969032c8..b5e269f62c 100644 --- a/modules/core/ui/theme/farm_ui_theme.libraries.yml +++ b/modules/core/ui/theme/farm_ui_theme.libraries.yml @@ -2,6 +2,15 @@ asset_map_popup: css: theme: css/asset_map_popup.css: { } +datetime: + css: + theme: + css/datetime.css: { } + js: + js/datetime.js: { } + dependencies: + - core/drupal + - core/once flag: css: theme: diff --git a/modules/core/ui/theme/farm_ui_theme.module b/modules/core/ui/theme/farm_ui_theme.module index 84140d2f3a..2c7cc9d6ff 100644 --- a/modules/core/ui/theme/farm_ui_theme.module +++ b/modules/core/ui/theme/farm_ui_theme.module @@ -25,6 +25,9 @@ function farm_ui_theme_theme($existing, $type, $theme, $path) { * Implements hook_element_info_alter(). */ function farm_ui_theme_element_info_alter(array &$info) { + if (isset($info['datetime'])) { + $info['datetime']['#attached']['library'][] = 'farm_ui_theme/datetime'; + } if (isset($info['farm_map'])) { $info['farm_map']['#attached']['library'][] = 'farm_ui_theme/map'; } diff --git a/modules/core/ui/theme/js/datetime.js b/modules/core/ui/theme/js/datetime.js new file mode 100644 index 0000000000..87aa6cfaba --- /dev/null +++ b/modules/core/ui/theme/js/datetime.js @@ -0,0 +1,23 @@ +(function (Drupal, once) { + Drupal.behaviors.farm_ui_datetime = { + attach: function (context, settings) { + var wrappers = once('toggle-time', '.form-datetime-wrapper', context); + wrappers.forEach(this.hideTime); + }, + hideTime: function (wrapper) { + var link = document.createElement('a'); + link.href = 'javascript: void(0)'; + link.innerHTML = 'Show time'; + link.classList.add('toggle-time'); + link.addEventListener('click', Drupal.behaviors.farm_ui_datetime.showTime); + wrapper.querySelector('.form-type--date').appendChild(link); + wrapper.querySelector('input.form-time').style.display = 'none'; + }, + showTime: function (event) { + var wrapper = event.target.closest('.form-datetime-wrapper'); + wrapper.querySelector('input.form-time').style.display = 'block'; + var link = wrapper.querySelector('a.toggle-time'); + link.parentNode.removeChild(link); + }, + }; +}(Drupal, once));