Developer forum

Forum » Development » Exclude paragraphs

Exclude paragraphs


Reply
At runtime I need a way to exclude specific paragraphs from being rendered on a page.

Is there a notification I can use?

Regards,
Thomas



Replies

 
Reply
 
Hi Thomas,

There is a notification for paragraphs you can use.

Here is an example (not tested)...

[Subscribe(Dynamicweb.Notifications.Standard.Paragraph.OnBeforeRender)]
    public class ParagraphBeforeRender : NotificationSubscriber
    {
        public override void OnNotify(string notification, NotificationArgs args)
        {
            var parameters = args as Dynamicweb.Notifications.Standard.Paragraph.ParagraphNotificationArgs;
            if (parameters == null) return;
 
            var paragraph = parameters.Target;
            paragraph.ShowParagraph = false; // add your own logic here
        }
    } 

You can also control what paragraphs are displayed by setting user permissions on those paragraphs, but that is probably not what you need.

BR.
Morten
 
Reply
Hi Morten,

Tanks for your reply, but  unfortunately the code does not work

var parameters = args as Dynamicweb.Notifications.Standard.Paragraph.ParagraphNotificationArgs;

Always return null

I have tried cast to  Dynamicweb.Notifications.Standard.Paragraph.OnBeforeRenderArgs instead and this work.

var onBeforeRenderParameters = args as Dynamicweb.Notifications.Standard.Paragraph.OnBeforeRenderArgs;

But setting
onBeforeRenderParameters.Paragraph.Paragraph.ParagraphShowParagraph = false;

Don't change anything :-(

/Thomas
 
Nicolai Høeg Pedersen
Reply
Hi Thomas

The notification is raised just before each paragraph is rendered. Setting ShowParagraph to false does nothing at this stage.

But - you also have a template object as a parameter on the notification args. Set the html property of the template object to string.empty and nothing will be rendered.
 
Reply
Hi Nicolai,

This works, no output is generated from the paragraph then setting html to string.empty.

But then paragraph/module is still being "executed", and I would prefer not to use unnecessary resources on this.

A notification before the WriteParagraph method, would be a nice feature.
 
Nicolai Høeg Pedersen
Reply
Hi Thomas

You can avoid the module from being executed by setting paragraphmodulesystemname to string.empty as well. That will have an impact on the module because module output is not generated untill the render of the paragraph. It is only active and publication period you cannot set on this notification subscriber because they are already checked for when getting the data.

Will look into further notifications.

 

You must be logged in to post in the forum