Eddy Kavanagh
Eddy Kavanagh

Reputation: 254

How to add Orchard shape to start of body?

I'm just getting started with Orchard and writing a module for Google Tag Manager which means I need to add a JavaScript snippet at the start of the body tag for every page in our site.

In my FilterProvider OnResultExecuting I've got

var context = _workContextAccessor.GetContext();
var tags = context.Layout.Body;

var snippet = Shape.Snippet();
snippet.ContainerId = containerId;

tags.Add(snippet);

where Shape is the DefaultShapeFactory. I know that Shape.Add has a second argument for position, but I've tried ":before", ".5", "1" with no success. It always gets rendered at the end of the body, using the ThemeMachine theme with no modifications.

If I modify the theme and add a new zone to either the Document or Layout view at the start of the body and then add my shape to that it obviously appears there which is my solution for now, but this means that we'll have to ensure that any theme we build has that zone in the correct place.

How do I add shape in code to the Body and control its position?

Upvotes: 1

Views: 339

Answers (1)

Alexander  Petryakov
Alexander Petryakov

Reputation: 730

This code works:

context.Layout.Body.Items.Insert(0, snippet)

Your code does not work because of

  1. Body shape is created by the CoreShapes class and then Specific Shape is added to it with default (null) position.
  2. Sorting shapes inside body take place by the FlatPositionComparer class and null position be converted to "before" position.
  3. So Specific Shape has the highest priority and is the first place in the list. And nothing could move it.

Upvotes: 1

Related Questions