1

On Drupal 8, the select list provided by the Date module looks like this:

example select list

The site I am building in Japanese, and in Japanese, it's common to add a character to indicate what the number means, like this:

2018 2 5 15 00 -> 2018年 2月 5日 15時 00分

How can I add these labels? I attempted to do it through the string translation UI screen, but since the year, day, hour, and minutes are just numbers, there is no way to "translate" them. The month shows up correctly because it is translated from Jan/Feb/etc.

I am thinking the solution involves hook_form_alter() but I don't know how to modify the way the values are displayed in the select widget without actually changing the values input and causing the form to fail.

1 Answer 1

2

You can't use hook_form_alter() or hook_field_widget_form_alter() in this case. Try with hook_element_info_alter()

function MY_MODULE_element_info_alter(array &$types) {
  if (isset($types['datelist'])) {
     $types['datelist']['#process'][] = '_mymodule_add_suffix_value';
  }
}

function _mymodule_add_suffix_value(&$element) {
  // process logic option $element['day']
  // process $element['month']
  // process $element['hour']
  return $element;
}
2
  • Unfortunately, this code gives me a WSOD: TypeError: Argument 1 passed to Drupal\bootstrap\Plugin\ProcessManager::process() must be of the type array, null given in Drupal\bootstrap\Plugin\ProcessManager::process() (line 44 of themes/contrib/bootstrap/src/Plugin/ProcessManager.php). Commented Feb 2, 2018 at 17:21
  • @PatrickKenny Just return $element at custom process function.
    – MrD
    Commented Feb 5, 2018 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.