function webform_node_node_prepare_form in Webform 6.x
Same name and namespace in other branches
- 8.5 modules/webform_node/webform_node.module \webform_node_node_prepare_form()
Implements hook_node_prepare_form().
Prepopulate a node's webform field target id.
See also
\Drupal\webform_node\Controller\WebformNodeReferencesListController::render
File
- modules/
webform_node/ webform_node.module, line 121 - Provides a webform content type which allows webforms to be integrated into a website as nodes.
Code
function webform_node_node_prepare_form(NodeInterface $node, $operation, FormStateInterface $form_state) {
// Only prepopulate new nodes.
if (!$node
->isNew()) {
return;
}
/** @var \Drupal\webform\WebformEntityReferenceManagerInterface $entity_reference_manager */
$entity_reference_manager = \Drupal::service('webform.entity_reference_manager');
// Make the node has a webform (entity reference) field.
$field_name = $entity_reference_manager
->getFieldName($node);
if (!$field_name) {
return;
}
// Populate the node's title, webform field id and default data.
$webform_id = \Drupal::request()->query
->get('webform_id');
if ($webform_id && ($webform = Webform::load($webform_id))) {
$node->{$field_name}->target_id = $webform_id;
$node->title->value = \Drupal::request()->query
->get('webform_title') ?: $webform
->label();
if ($webform_default_data = \Drupal::request()->query
->get('webform_default_data')) {
$node->{$field_name}->default_data = is_array($webform_default_data) ? Yaml::encode($webform_default_data) : $webform_default_data;
}
}
}