Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
webform_node_view() permits the 'form' view mode to force display:
// View mode of 'form' is exempted to allow blocks and views to force display.
However, if the 'form' view mode is passed to webform_node_view(), it may result in an undefined index error. $node->content['webform']['visible'] assumes $extra_fields['webform']['visible'] is set, which isn't necessarily true if the 'form' view mode is used.
// Add the output to the node.
$node->content['webform'] = array(
'#theme' => 'webform_view',
'#node' => $node,
'#page' => $page,
'#form' => $form,
'#enabled' => $enabled,
'#visible' => $extra_fields['webform']['visible'], // When 'form' view mode is used, $extra_fields['webform']['visible'] isn't set.
'#weight' => 10,
);
Proposed resolution
The proposed solution is to set $extra_fields['webform']['visible'] to TRUE if the 'form' view mode is being used:
// If the webform is not set to display in this view mode, return early.
// View mode of 'form' is exempted to allow blocks and views to force display.
$extra_fields = field_extra_fields_get_display('node', $node->type, $view_mode);
if ($view_mode != 'form' && empty($extra_fields['webform']['visible'])) {
return;
}
+ elseif ($view_mode == 'form') {
+ $extra_fields['webform']['visible'] = TRUE;
+ }
Remaining tasks
None
User interface changes
None
API changes
None
Data model changes
None
Comments
Comment #2
Chris Burge CreditAttribution: Chris Burge commentedComment #3
Liam MorlandProblem introduced in #1919872: Skip hook_node_view() if Webform is not rendering a form for a view mode.
Could you add a test?
Comment #4
Liam MorlandUnder what conditions does the undefined index error appear? At the moment, when such an error happens,
#visible
will be set to NULL. Setting$extra_fields['webform']['visible']
to TRUE will change this. What is the impact of that change? What if we set$extra_fields['webform']['visible']
to NULL?Comment #5
Liam MorlandPlease provide steps to reproduce the problem and indicate the expected behavior and the actual behavior.
Comment #6
Chris Burge CreditAttribution: Chris Burge commented@Liam Morland - Thanks for looking into this issue. I believe I was programmatically rendering a webform inside a custom CTools content type. That said, I no longer have an association with the project for which this patch was developed.