-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSearchInitialization.cs
More file actions
160 lines (140 loc) · 6.3 KB
/
SearchInitialization.cs
File metadata and controls
160 lines (140 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright © 2026 Jeroen Stemerdink.
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
using Microsoft.Extensions.DependencyInjection;
namespace EPi.Libraries.BlockSearch
{
using EPiServer;
using EPiServer.Core;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.Logging;
using Microsoft.Extensions.Logging;
/// <summary>
/// Class SearchInitialization.
/// </summary>
[InitializableModule]
[ModuleDependency(typeof(FrameworkInitialization))]
public class SearchInitialization : IInitializableModule
{
/// <summary>
/// Gets or sets the logger
/// </summary>
/// <value>The logger.</value>
protected ILogger<SearchInitialization> Logger;
/// <summary>
/// Gets or sets the content events.
/// </summary>
/// <value>The content events.</value>
protected IContentEvents ContentEvents { get; set; }
/// <summary>
/// Gets or sets the helper.
/// </summary>
/// <value>The helper.</value>
protected Helper Helper { get; set; }
/// <summary>
/// Initializes this instance.
/// </summary>
/// <param name="context">The context.</param>
/// <remarks>
/// Gets called as part of the EPiServer Framework initialization sequence. Note that it will be called
/// only once per AppDomain, unless the method throws an exception. If an exception is thrown, the initialization
/// method will be called repeatedly for each request reaching the site until the method succeeds.
/// </remarks>
public void Initialize(InitializationEngine context)
{
if (context == null)
{
return;
}
this.ContentEvents = context.Services.GetRequiredService<IContentEvents>();
this.Logger = context.Services.GetRequiredService<ILogger<SearchInitialization>>();
this.Helper = new Helper(serviceProvider: context.Services);
this.ContentEvents.PublishedContent += this.OnPublishedContent;
this.ContentEvents.PublishingContent += this.OnPublishingContent;
this.Logger.LogInformation("[Blocksearch] Initialized");
}
/// <summary>
/// Handles the <see cref="E:PublishedContent" /> event.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="contentEventArgs">The <see cref="ContentEventArgs" /> instance containing the event data.</param>
public void OnPublishedContent(object sender, ContentEventArgs contentEventArgs)
{
if (contentEventArgs == null)
{
return;
}
// Check if the content that is published is indeed a block.
// If it's not, don't do anything.
if (contentEventArgs.Content is not BlockData)
{
return;
}
this.Helper.UpdateParents(contentLink: contentEventArgs.ContentLink);
}
/// <summary>
/// Handles the <see cref="E:PublishingContent" /> event.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="contentEventArgs">The <see cref="ContentEventArgs"/> instance containing the event data.</param>
public void OnPublishingContent(object sender, ContentEventArgs contentEventArgs)
{
if (contentEventArgs == null)
{
return;
}
// Check if the content that is published is a page. If it's not, don't do anything.
if (contentEventArgs.Content is not PageData pageData)
{
return;
}
if (pageData.IsReadOnly)
{
pageData = pageData.CreateWritableClone();
contentEventArgs.Content = pageData;
}
this.Helper.UpdateAdditionalSearchContent(parent: pageData);
}
/// <summary>
/// Resets the module into an uninitialized state.
/// </summary>
/// <param name="context">The context.</param>
/// <remarks>
/// <para>
/// This method is usually not called when running under a web application since the web app may be shut down very
/// abruptly, but your module should still implement it properly since it will make integration and unit testing
/// much simpler.
/// </para>
/// <para>
/// Any work done by
/// <see
/// cref="M:EPiServer.Framework.IInitializableModule.Initialize(EPiServer.Framework.Initialization.InitializationEngine)" />
/// as well as any code executing on
/// <see cref="E:EPiServer.Framework.Initialization.InitializationEngine.InitComplete" /> should be reversed.
/// </para>
/// </remarks>
public void Uninitialize(InitializationEngine context)
{
this.ContentEvents.PublishedContent -= this.OnPublishedContent;
this.ContentEvents.PublishingContent -= this.OnPublishingContent;
this.Logger.LogInformation("[Blocksearch] Uninitialized");
}
}
}