Drupal 7 Webform多实体默认值预选的自定义模块实现问询
Hey Annie, great job getting the session storage and VBO redirect working so far—this is a solid start for your first complex module! Let's break down how to finish the job: pre-filling your Webform's category checkboxes with the entity IDs stored in the user's session.
Step 1: Hook into Webform's Default Value System
We'll use Drupal's hook system to intercept the Webform element and inject our session-stored IDs as default values. The best hook for this is hook_webform_element_default_value_alter, which lets us modify default values right before the form renders.
Add this code to your custom module's .module file:
/** * Implements hook_webform_element_default_value_alter(). */ function yourmodule_webform_element_default_value_alter(&$default_value, $context) { // Target your specific Webform and checkbox element $webform = $context['webform']; $element = $context['element']; // Replace these with your actual Webform ID and checkbox element key $target_webform_id = 'your_webform_machine_name'; $target_element_key = 'category_checkboxes'; if ($webform->id() === $target_webform_id && $element['#webform_key'] === $target_element_key) { // Retrieve the entity IDs from the user's session $session = \Drupal::request()->getSession(); $entity_ids = $session->get('vbo_selected_entity_ids', []); if (!empty($entity_ids) && is_array($entity_ids)) { // Validate the IDs to ensure they're valid and accessible $valid_entity_ids = []; $entity_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term'); // Swap with your entity type if needed foreach ($entity_ids as $id) { $entity = $entity_storage->load($id); // Only keep IDs for entities the user can view if ($entity && $entity->access('view')) { $valid_entity_ids[] = $id; } } // Set the valid IDs as the default checked values $default_value = $valid_entity_ids; // Optional: Clear the session data so refresh doesn't re-populate $session->remove('vbo_selected_entity_ids'); } } }
Step 2: Key Configuration Checks
- Webform Element Setup: Make sure your checkbox element in Webform has the "Multiple" option enabled (it should be by default for checkboxes). This allows the element to accept an array of values as defaults.
- Session Key Match: Double-check that the session key
vbo_selected_entity_idsmatches the key you used when storing IDs from VBO. If you used a different key, update it here. - Entity Type Adjustment: If your entities aren't taxonomy terms (e.g., nodes), replace
taxonomy_termwith your actual entity type machine name (likenode).
Step 3: Debugging Tips
If things aren't working as expected, add some debug logs to verify the flow:
// Inside the hook function, after retrieving $entity_ids \Drupal::logger('yourmodule')->debug('Session entity IDs: @ids', ['@ids' => print_r($entity_ids, true)]); \Drupal::logger('yourmodule')->debug('Valid entity IDs to pre-check: @valid', ['@valid' => print_r($valid_entity_ids, true)]);
You can view these logs in Drupal's Reports > Recent log messages.
Final Notes
- Security: Validating the entity IDs and checking user access is crucial to prevent malicious users from injecting invalid or restricted entity IDs into the session.
- Session Cleanup: Clearing the session data after setting the default values ensures that if the user refreshes the Webform, the checkboxes won't re-populate unless they run the VBO action again.
内容的提问来源于stack exchange,提问作者Annie G

