Restrict IFrame from Rich Text Editor field
Tested on Sitecore 10.3 and 10.4.
In this blog post, I cover steps to raise a validation error in a rich text field that has an Iframe tag since IFrame might be a bad practice to use in certain organizations. The process involves the following steps:
1. Create a validation rule
2. Ensure that the validation rule fires on CE save/publish button click
3. Strip-off the iframe tags since RTE will still have the entry
The approach here is to raise a validation error on the concerned field. So, here is the c# code:
First, create an RTE IFrame Validation Rule:
//////
/////
Config patch:
#####
#####
Next, Validate Before save button click:
//////
/////
Associated config:
#####
#####
Strip IFrame tags in RTE config:
/////
/////
Associated C# code:
//****
//****
Clone the Github repo. Since it is a .net framework 4.8 project, open the project with VS 2022 Community edition, build the dll and deploy the dll to bin folder while config files must be deployed to app_config\include\zzz folder.
VS 2022 sln structure for reference:
Create validation rule as follows in Sitecore or install the Sitecore package:
/sitecore/system/Settings/Validation Rules/Field Rules/RTEIframeValidationRule
Also, for the above item, set type to the assembly/namespace/class name. For the current code setup it is - SitecoreValidation.Validators.RTEIframeValidationRule, SitecoreValidationIn template validation rules for the concerned field, set the RTEIframeValidationRule rule in all the fields -
Workflow, Validator Bar, Quick Action Bar, Validate Button
Assign rule to template (via PS script):
Pre-requisite: the Sitecore validation rule
SPE installed in the instance
SPE script that bulk applies the rule for RTE in a given template path:
PS Script end-result:
Hi, how about a case if I type html with iframe tag like this: '%3Ciframe%3Etest%3C%2Fiframe%3E' equivalent of encoded iframe tag ? Not sure that regex will pass.
ReplyDeleteshould cover that scenario too now....
DeleteAt the same time, iframe tag can be self-closed tag in some browsers and still working like this: [iframe src="http://web.simmons.edu/~grovesd/comm244/notes/week2/links" /]
ReplyDelete