php Drupal中的条件表单字段
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Drupal中的条件表单字段相关的知识,希望对你有一定的参考价值。
[function `hook_form_alter` (aka, the function in which you mess with states)](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Form%21form.api.php/function/hook_form_alter/8.6.x)
[Build a Module: Adding usability with the #states attribute](https://buildamodule.com/video/drupal-7-development-core-concepts-how-to-build-and-manipulate-forms-with-the-form-api-modifying-forms-with-hook-form-alter#viewing)
[Form API Reference - #states](https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7.x#states)
[function `drupal_process_states` (Includes a list of all possible states!)](https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_process_states/7.x)
[Lullabot: Form API #states](https://www.lullabot.com/articles/form-api-states)
[Drupal 7 Form API: Using #states with multiple conditionals (AND, OR and XOR)](https://www.metaltoad.com/blog/drupal-7-form-api-using-states-multiple-conditionals-and-or-and-xor)
[Conditional Fields in Paragraphs Using the Javascript States API for Drupal 8](https://agaric.coop/blog/conditional-fields-paragraphs-using-javascript-states-api-drupal-8)
Note that the states condition uses a jQuery selector
// This is stuff from essential access.
// Note that it's altering the pre-existing user registration form which was mostly created in the GUI.
// However, some stuff (such as conditional states and instructional text) can't be done in the GUI.
/**
* Set up conditional PDPT fields on the user_register_form.
*/
function essentialaccess_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
if($form_id == 'user_register_form') {
$form['field_eligibility_requirements']['#states'] = [
'visible' => [
':input[name="field_registering_for[pdpt]"]' => ['checked' => TRUE],
],
'enabled' => [
':input[name="field_registering_for[pdpt]"]' => ['checked' => TRUE],
],
'invisible' => [
':input[name="field_registering_for[pdpt]"]' => ['checked' => FALSE],
],
'disabled' => [
':input[name="field_registering_for[pdpt]"]' => ['checked' => FALSE],
],
];
$form['field_eligibility_requirements']['widget'] += [
'#field_prefix' => t("All participating clinic sites and LHJs must:"),
];
$form['field_eligibility_requirements'] += [
'explanation' => [
'#type' => "item",
'#markup' => "<div id='eligibility-requirements-not-met'>" . t("Please note that if your site does not meet these eligibility requirements then you cannot register it.") . "</div>",
'#states' => [
'invisible' => [
':input[name="field_eligibility_requirements[california]"]' => ['checked' => TRUE],
':input[name="field_eligibility_requirements[at_risk]"]' => ['checked' => TRUE],
':input[name="field_eligibility_requirements[340b]"]' => ['checked' => TRUE],
':input[name="field_eligibility_requirements[uninsured]"]' => ['checked' => TRUE],
':input[name="field_eligibility_requirements[treatment]"]' => ['checked' => TRUE],
],
'disabled' => [
':input[name="field_eligibility_requirements[california]"]' => ['checked' => TRUE],
':input[name="field_eligibility_requirements[at_risk]"]' => ['checked' => TRUE],
':input[name="field_eligibility_requirements[340b]"]' => ['checked' => TRUE],
':input[name="field_eligibility_requirements[uninsured]"]' => ['checked' => TRUE],
':input[name="field_eligibility_requirements[treatment]"]' => ['checked' => TRUE],
],
'visible' => [
[
[':input[name="field_eligibility_requirements[california]"]' => ['checked' => FALSE]],
'or',
[':input[name="field_eligibility_requirements[at_risk]"]' => ['checked' => FALSE]],
'or',
[':input[name="field_eligibility_requirements[340b]"]' => ['checked' => FALSE]],
'or',
[':input[name="field_eligibility_requirements[uninsured]"]' => ['checked' => FALSE]],
'or',
[':input[name="field_eligibility_requirements[treatment]"]' => ['checked' => FALSE]],
],
],
'enabled' => [
[
[':input[name="field_eligibility_requirements[california]"]' => ['checked' => FALSE]],
'or',
[':input[name="field_eligibility_requirements[at_risk]"]' => ['checked' => FALSE]],
'or',
[':input[name="field_eligibility_requirements[340b]"]' => ['checked' => FALSE]],
'or',
[':input[name="field_eligibility_requirements[uninsured]"]' => ['checked' => FALSE]],
'or',
[':input[name="field_eligibility_requirements[treatment]"]' => ['checked' => FALSE]],
],
],
],
]
];
var_dump($form['field_program_information']['widget'][0]['subform']);
if()
$form['field_program_information']['widget'][0]['subform']['field_point_person_name']['widget'][0]['#required'] =
// $form['field_program_information']['widget'][0]['subform']['field_point_person_name']['widget'][0]['#states'] = [
// 'disabled' => [
// ':input[name="field_registering_for[pdpt]"]' => ['checked' => TRUE],
// ],
// ];
// $form['field_program_information']['widget'][0]['subform']['field_additional_contact_informa']['#states'] = [
// 'disabled' => [
// ':input[name="field_registering_for[pdpt]"]' => ['checked' => TRUE],
// ],
// ];
$form['field_program_information']['#states'] = [
'visible' => [
':input[name="field_eligibility_requirements[california]"]' => ['checked' => TRUE],
':input[name="field_eligibility_requirements[at_risk]"]' => ['checked' => TRUE],
':input[name="field_eligibility_requirements[340b]"]' => ['checked' => TRUE],
':input[name="field_eligibility_requirements[uninsured]"]' => ['checked' => TRUE],
':input[name="field_eligibility_requirements[treatment]"]' => ['checked' => TRUE],
],
'enabled' => [
':input[name="field_eligibility_requirements[california]"]' => ['checked' => TRUE],
':input[name="field_eligibility_requirements[at_risk]"]' => ['checked' => TRUE],
':input[name="field_eligibility_requirements[340b]"]' => ['checked' => TRUE],
':input[name="field_eligibility_requirements[uninsured]"]' => ['checked' => TRUE],
':input[name="field_eligibility_requirements[treatment]"]' => ['checked' => TRUE],
],
'invisible' => [
[
[':input[name="field_eligibility_requirements[california]"]' => ['checked' => FALSE]],
'or',
[':input[name="field_eligibility_requirements[at_risk]"]' => ['checked' => FALSE]],
'or',
[':input[name="field_eligibility_requirements[340b]"]' => ['checked' => FALSE]],
'or',
[':input[name="field_eligibility_requirements[uninsured]"]' => ['checked' => FALSE]],
'or',
[':input[name="field_eligibility_requirements[treatment]"]' => ['checked' => FALSE]],
],
],
'disabled' => [
[
[':input[name="field_eligibility_requirements[california]"]' => ['checked' => FALSE]],
'or',
[':input[name="field_eligibility_requirements[at_risk]"]' => ['checked' => FALSE]],
'or',
[':input[name="field_eligibility_requirements[340b]"]' => ['checked' => FALSE]],
'or',
[':input[name="field_eligibility_requirements[uninsured]"]' => ['checked' => FALSE]],
'or',
[':input[name="field_eligibility_requirements[treatment]"]' => ['checked' => FALSE]],
],
],
];
// var_dump($form);
}
}
以上是关于php Drupal中的条件表单字段的主要内容,如果未能解决你的问题,请参考以下文章
Drupal Commerce Kickstart2 中的其他字段
尝试根据日期条件隐藏 Drupal 视图输出中的 CC 字段