Drupal 8:如何通过输入年龄(年、月)计算患者近似DoB?
解决Drupal中年龄转近似出生日期的问题
嗨,作为Drupal新手碰到这个需求完全能理解——毕竟很多场景下用户只能提供年龄而非准确生日,我来给你几个实用的实现方案,一步步搞定:
方案一:自定义模块 + 前后端结合(推荐)
这个方案灵活性最高,既能保证用户体验,又能确保数据准确性。
1. 创建基础自定义模块
先新建一个简单的自定义模块(比如命名为patient_age_converter):
- 新建
patient_age_converter.info.yml文件:
name: Patient Age to DOB Converter type: module description: Converts patient age (years/months) to approximate date of birth. core_version_requirement: ^9 || ^10 package: Custom
- 新建
patient_age_converter.module文件,用来写核心逻辑。
2. 修改患者表单,添加年龄输入字段
用hook_form_alter修改Patients内容类型的表单,添加年、月两个输入框,同时处理原DoB字段的显示:
/** * Implements hook_form_alter(). */ function patient_age_converter_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { // 匹配Patients内容类型的添加/编辑表单,替换成你的内容类型机器名 if ($form_id == 'node_patient_form' || $form_id == 'node_patient_edit_form') { // 添加年龄输入字段 $form['patient_age'] = [ '#type' => 'fieldset', '#title' => t('Patient Age'), '#weight' => 10, '#attributes' => ['class' => ['patient-age-fieldset']], ]; $form['patient_age']['years'] = [ '#type' => 'number', '#title' => t('Years'), '#min' => 0, '#max' => 120, '#size' => 3, ]; $form['patient_age']['months'] = [ '#type' => 'number', '#title' => t('Months'), '#min' => 0, '#max' => 11, '#size' => 2, ]; // 禁用原DoB字段,避免用户手动输入冲突 $form['field_dob']['#disabled'] = TRUE; // 如果不需要显示DoB字段,可以设置#access为FALSE隐藏它 // $form['field_dob']['#access'] = FALSE; // 添加提交前的计算逻辑(后端验证) $form['#submit'][] = 'patient_age_converter_calculate_dob_submit'; } }
3. 后端计算并赋值DoB
在提交函数里用PHP计算近似出生日期,确保数据准确:
/** * Submit handler to calculate approximate DOB from age. */ function patient_age_converter_calculate_dob_submit(&$form, \Drupal\Core\Form\FormStateInterface $form_state) { $years = $form_state->getValue(['patient_age', 'years']) ?: 0; $months = $form_state->getValue(['patient_age', 'months']) ?: 0; if ($years || $months) { $today = new DateTime(); // 减去对应的年和月 $today->sub(new DateInterval("P{$years}Y{$months}M")); // 格式化为Drupal日期字段需要的格式(通常是Y-m-d) $form_state->setValue('field_dob', ['value' => $today->format('Y-m-d')]); } }
4. 前端实时计算(提升用户体验)
添加JavaScript实现实时计算,用户输入年龄时自动填充DoB字段:
新建patient_age_converter.js文件,放在模块的js目录下,然后在.info.yml里引入:
# 在info.yml里添加 scripts: - js/patient_age_converter.js
JS代码:
(function ($) { Drupal.behaviors.patientAgeConverter = { attach: function (context, settings) { // 获取年龄输入框和DoB字段(替换成你的字段ID) const yearsInput = $('#edit-patient-age-years', context); const monthsInput = $('#edit-patient-age-months', context); const dobField = $('#edit-field-dob-0-value', context); // 计算出生日期的函数 function updateDob() { const years = parseInt(yearsInput.val()) || 0; const months = parseInt(monthsInput.val()) || 0; if (years || months) { const today = new Date(); today.setFullYear(today.getFullYear() - years); today.setMonth(today.getMonth() - months); // 格式化为YYYY-MM-DD const formattedDate = today.toISOString().split('T')[0]; dobField.val(formattedDate); } else { dobField.val(''); } } // 监听输入变化 yearsInput.on('input change', updateDob); monthsInput.on('input change', updateDob); } }; })(jQuery);
方案二:使用现有模块(快速实现)
如果不想写自定义代码,可以试试Age Field模块(注意确认版本兼容你的Drupal版本)。这个模块可以添加年龄字段,并且支持将年龄转换为出生日期字段值,配置起来比较简单。不过自定义模块的灵活性更高,适合需要定制逻辑的场景。
注意事项
- 测试边缘情况:比如输入0年11个月、100年0个月等,确保计算逻辑正常。
- 日期格式:Drupal的日期字段格式可能因配置不同而变化,确保PHP和JS输出的格式和字段配置一致。
- 权限:确保只有有权限编辑患者内容的用户能看到这些字段。
内容的提问来源于stack exchange,提问作者Arnab Roychoudhury
相关产品推荐
相关产品推荐

