My issue, while quite specific, could be happening to a lot of people. The problem I'm having is I have a custom view mode which I'm using to render various node types on a page. These pages are all cached by varnish. The problem is that as soon as a webform node is present on the page the Cache-Control headers get set to no-cache as drupal thinks there's a form on the page now (even though there's not).

In webform_node_view there's a statement which returns out of the hook if the view mode is equal to 'teaser'. What if I want the same behaviour to happen for my custom view modes? Currently that's impossible.

I propose a fix to add another admin setting which defaults to teaser only, but allows you to choose more view modes to act like teaser view modes for the webform. This means that the original functionality would remain in tact, and would also allow people to choose any others they want to behave this way.

This patch adds this to the admin screen and adds the check to webform_node_view. Please provide feedback if there's a better way to do this, I'm happy to develop the functionality a bit more.

Committed to 7.x-4.x and 8.x.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

acbramley’s picture

Added variable_del call to uninstall hook

acbramley’s picture

Status: Active » Needs review
quicksketch’s picture

Status: Needs review » Needs work

Hi there, over in #1713106: Remove "Show complete form in teaser" and all "teaser" hard-coded options (use view modes instead), we removed all options around dealing with teasers, so I wouldn't want to add another option that re-introduces the concept.

I see where you're coming from here, but the approach is strange. It sounds like the best thing to do would be to check if Webform is going to be used in the current view mode, and bail out in all other situations. Basically instead of asking the user for this information, we simply read the view modes and check if Webform is needed to begin with before doing any work in hook_node_view(). I agree that this would be a nice performance improvement in any case; I hadn't realized that Drupal would attempt to call hook_node_view() on modules that weren't going to be displaying anything.

quicksketch’s picture

Title: Rendering webform as anything except teaser includes full webform_node_view hook, causes Cache-Control to be set to no-cache. » Skip hook_node_view() if Webform is not rendering a form for a view mode

Retitling on this proposal. It sounds like it would solve your problem without any additional options.

acbramley’s picture

@quicksketch, it certainly will call hook_node_view()!

I'll spend a bit of time today trying your suggestion, although I'm unsure how $view_mode could tell you whether it's rendering a webform or not. Cheers

acbramley’s picture

$node and $view_mode do not hold any information that I can see in hook_node_view that would allow us to check whether the node_view is going to be rendering a form. Further more I don't know how we could possibly check this? View modes can be custom, they are defined in hook_theme with any variables they like. In hook_preprocess_node they could take the webform variable, do whatever they want with it and spit it out in a template. If you can give me any more information on how you envisioned this could work please let me know.

quicksketch’s picture

$node and $view_mode do not hold any information that I can see in hook_node_view that would allow us to check whether the node_view is going to be rendering a form.

Yeah you're right, it's not an easy thing to check.

Further more I don't know how we could possibly check this?

I think the needed information is in entity_get_info(), which means we could retreive it via the child-function field_info_bundles(). That contains the field-info array which will tell us the configuration of the view mode (I think). Basically we'll be reading out the configuration of the view mode just the same as Field UI does. We can tell from that whether the Webform "field" has been enabled (which is exposed to Field UI via hook_field_extra_fields().

I personally haven't tried this myself yet, but I it should be possible in some way. If Field UI gets at that configuration somehow, we should be able to also.

acbramley’s picture

Ah yes that does sound like a valid way to go about it, at first glance it doesn't seem like hook_field_extra_fields knows about view modes though which instantly tells me that all view modes will get this field. I'll have a quick look into it now.

acbramley’s picture

Issue summary: View changes
FileSize
2.29 KB

I still haven't come up with a solution for this and with the recent security upgrades I needed to reroll this patch

DanChadwick’s picture

Status: Needs work » Closed (won't fix)

Webform 7.x-3.x is only receiving critical bug fixes, so I don't expect you'll need to keep re-patching. It does not appear that webform 7.x-4.x has the same code in webform_node_view. If this same issue exists in 7.x-4.x, please feel to re-open this issue.

torotil’s picture

Version: 7.x-3.18 » 7.x-3.x-dev
Status: Closed (won't fix) » Needs work
DanChadwick’s picture

Category: Bug report » Task

Let's use "Task" for 7.x-3.x issues that were closed and are being reopened for possible fixing. torotil has taken the lead on 7.x-3.x issues, but as a project webform has no commitment to fix anything other than critical bugs in this branch. Task indicates an issue that torotil (and others) have decided to work on.

We are grateful for torotil's efforts.

torotil’s picture

Version: 7.x-3.x-dev » 7.x-4.x-dev

I've taken a look at webform-7.x-4.x to check how it solves this issue. Turns out it doesn't. It behaves exactly like webform-7.x-3.x in that regard. While (nearly) all hard-coded behavior for teasers has been removed, the webform is still rendered into $node->content['webform'] regardless of the $view_mode. This is an performance issue as soon as there are many teasers of webform-enabled nodes on one page.

DanChadwick’s picture

Category: Task » Feature request

Optimizations sound like features, rather than bugs.

DanChadwick’s picture

Category: Feature request » Task

Never mind. More consistent as a task.

DanChadwick’s picture

Category: Task » Bug report
Status: Needs work » Active

This patch does not apply to the 7.x-3.x branch, since teasers are handled differently. The original -4.x branch issue for removing the webform-specific teaser option and replacing it with view modes:
#1713106: Remove "Show complete form in teaser" and all "teaser" hard-coded options (use view modes instead)

That patch has a flaw in that it relies on view mode to determine whether to display the form, but then never checks the display before generating the form.

This will represent a change in behavior because any view modes in which the extra field "webform" isn't set to display in the content type's display settings will no longer have the form. A change note will be needed.

This leave only one special test for teaser. When the webform IS set to display in the teaser view mode, the form will direct to the webform's node page. I'm not wild about weird special handling, but I think it is pretty harmless. If you have a list of webforms on the front page, it is probably best to display complete them one at a time. If you don't like this, create another view mode identical to teaser, but with a different name.

A side note: The entire webform is loaded when teasers are displayed. The view mode is not available when the webform is loaded. The only solution to this would be some lazy-loading, which would be complicated to implement.

I know that torotil has been involved in this issue and is primarily interested in the 7.x-3.x branch. Out policy is not to introduce new features to that branch, so I don't see how this can be implemented in the 3.x branch without doing just that. I would suggest that anyone interested in this feature consider updating to the 4.x branch, or given the lack of -3.x branch development, patch webform themselves.

DanChadwick’s picture

DanChadwick’s picture

Version: 7.x-4.x-dev » 8.x-4.x-dev
Issue summary: View changes
Status: Active » Fixed
FileSize
1.41 KB

  • DanChadwick committed b345c3b on 7.x-4.x
    Issue #1919872 by DanChadwick: Skip hook_node_view() if webform is not...
  • DanChadwick committed 06438c3 on 8.x-4.x
    Issue #1919872 by DanChadwick: Skip hook_node_view() if webform is not...
DanChadwick’s picture

Version: 8.x-4.x-dev » 7.x-4.x-dev
torotil’s picture

Backport to 7.x-3.x.

  • torotil committed 2749c82 on 7.x-3.x
    Issue #1919872 by torotil, DanChadwick: Skip hook_node_view() if webform...
torotil’s picture

Status: Fixed » Needs work

I think we have created another issue / backwards-incompatible change wrt to how webform blocks work: The webform block is empty now if the webform is not shown on the full view_mode of the webform-node itself.

So this poses the question: Should webform-blocks respond to view-modes? … and if yes, how?

Either way I guess this needs a follow-up patch.

EDIT: I think webform-blocks shouldn't react to the view-mode settings as they are something different.

torotil’s picture

Status: Needs work » Needs review
FileSize
1.15 KB

Here is a patch that makes blocks ignore view-modes.

DanChadwick’s picture

Status: Needs review » Fixed
FileSize
1.95 KB

@torotil -- Good catch, although one might argue they got what they asked for. Still I agree it should be fixed. I chose a slightly different implementation. It also affects the views support.

Committed to 7.x-4.x and 8.x.

  • DanChadwick committed 0939754 on 7.x-4.x
    Issue #1919872 by torotil, DanChadwick: Don't skip hook_node_view() for...
  • DanChadwick committed 390203a on 8.x-4.x
    Issue #1919872 by torotil, DanChadwick: Don't skip hook_node_view() for...
torotil’s picture

Cherry-picked into 7.x-3.x.

  • torotil committed 8f4a212 on 7.x-3.x authored by DanChadwick
    Issue #1919872 by torotil, DanChadwick: Don't skip hook_node_view() for...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.