2

I'm trying to implement a popup when trying to implement a popup to alert users to a required field, but running into an error with even loading the CMS after trying to create the code:

Could not instantiate event handler. Type: DVD.Utility.DVDItemSavingProcessor. Method: DialogProcessor (method: Sitecore.Events.Event.GetConfigSubscribers()).

Here are my pieces of code:

Config:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <events>
      <event name="item:saving">
        <handler type="DVD.Utility.DVDItemSavingProcessor, DVD" method="DialogProcessor"/>
     </event>
   </events>
  </sitecore>
 </configuration>

Processor:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sitecore.Pipelines;
using Sitecore.Pipelines.HttpRequest;
using Sitecore.Data.Items;
using Sitecore.ContentSearch.SearchTypes;
using Sitecore.Shell.Framework.Commands;
using Sitecore.Web.UI.Sheer;

namespace DVD.Utility
{
    public class DVDItemSavingProcessor 
    {

        private void StartDialog()
        {
            // Start the dialog and pass in an item ID as an argument
            ClientPipelineArgs cpa = new ClientPipelineArgs();
            cpa.Parameters.Add("id", Sitecore.Context.Item.ID.ToString());

            // Kick off the processor in the client pipeline
            Sitecore.Context.ClientPage.Start(this, "DialogProcessor", cpa);
        }
        protected void DialogProcessor(ClientPipelineArgs args)
        {
            var id = args.Parameters["id"];

            if (!args.IsPostBack)
            {
                Item item = Sitecore.Context.Item;
                if (item.TemplateName == "Image" && string.IsNullOrEmpty(item.Fields["Alt"].ToString()))
                {
                    SheerResponse.Alert("Please fill in the Alt Tag field");
                }

                // Suspend the pipeline to wait for a postback and resume from another processor
                args.WaitForPostBack(true);
            }

        }
    }
}
2

The way to implement the event handler for the item:saving is to add the parameters sender and arguments. I've updated the StartDialog method as shown below.

public void StartDialog(object sender, EventArgs args)
{
    var savedItem = Event.ExtractParameter(args, 0) as Item;

    if (savedItem == null || savedItem.Database.Name.ToLower() != "master")
    {
        return;
    }

    // Start the dialog and pass in an item ID as an argument
    ClientPipelineArgs cpa = new ClientPipelineArgs();
    cpa.Parameters.Add("id", savedItem.ID.ToString());

    // Kick off the processor in the client pipeline
    Sitecore.Context.ClientPage.Start(this, "DialogProcessor", cpa);
}

And your config will look like this

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <events>
      <event name="item:saving">
        <handler type="DVD.Utility.DVDItemSavingProcessor, DVD" method="StartDialog"/>
     </event>
   </events>
  </sitecore>
 </configuration>

Best Approach - Using OOTB Sitecore

Instead of creating a custom dialog, you can make use of the Sitecore Validators. Based on the code you've provided, it shows that you want to show a popup if the alt field is blank.

You can actually do this without any code change using OOTB Sitecore feature. However, Alt Text validation is set to Error and this will still allow the media item to be saved. You'll need to update the Alt Required validator item by addingResult=FatalError. See screenshot below:

enter image description here

Item path: /sitecore/system/Settings/Validation Rules/Field Rules/System/Alt Required.

Create Your Own Validator

You can also create your own validator. Below is a snippet of a custom validator

using System;
using Sitecore.Data;
using Sitecore.Data.Validators;

namespace Sitecore.Experimental
{
    [Serializable]
    public class AltValidator : StandardValidator
    {
        protected override ValidatorResult Evaluate()
        {
            var database = Database.GetDatabase("master");

            if (this.ItemUri != null)
            {
                var currentItem = database.GetItem(this.ItemUri.ItemID, this.ItemUri.Language);

                if (currentItem.TemplateName == "Image" && string.IsNullOrEmpty(currentItem.Fields["Alt"].ToString()))
                {
                    this.Text = this.GetText("Alternate text is missing in the Image field.", this.GetFieldDisplayName());
                    return this.GetFailedResult(ValidatorResult.FatalError);
                }
            }

            return ValidatorResult.Valid;
        }

        protected override ValidatorResult GetMaxValidatorResult()
        {
            return this.GetFailedResult(ValidatorResult.FatalError);
        }

        public override string Name => "AltFieldValidator";
    }
}

Once you have the code in place, you just need to create a validation rule at the path /sitecore/system/Settings/Validation Rules. You can create your own direction if needed.

Then you'll need to populate the different fields. Please see screenshot below:

enter image description here

Then you can assign the new validation item to the fields from the template.

Point to note

The error message that will be displayed is a default Sitecore one. If you want to override the default message to a custom one, you can check the answer for question Sitecore Field Validator Change to Custom Message

  • +1 for OOTB Sitecore – Dan Sinclair Apr 1 at 0:15
  • I tried the OOTB option, but it seems like the Text=Parameter doesn't change the message? Neither does the Text field in the Text section. I'm wondering if these fields items do anything. Am I clear that if I want to customize the popup text I will need to override the save validator as in the linked post? Or would the initial item:saving processor do the trick? – Levi Wallach Apr 1 at 18:04
  • Those are used in the right side bar and also near the fields. You’ll see a red highlight near the fields and when hovering on it, it shows the message. In the answer, at the Point to note, I’ve pasted a link on where to change the default message – Hishaam Namooya Apr 1 at 18:06
  • The Point to note option you linked to I've been trying to get to work but running into an error, so will post about that separately, thanks. – Levi Wallach Apr 2 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.