Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sitecore change field tooltip dynamically

Is there a way to change the tooltip of an item dynamically in the content editor? It doesn't necessarily have to be the toolip - I'm just trying to output a text that is based on the item and field right next to the field, to show what the default value of the field would be. So far, in the pipeline processor, none of the field properties can be set - they are all readonly. Any idea how maybe I can just tag onto it, or something of that nature?

like image 706
M.R. Avatar asked Dec 26 '22 09:12

M.R.


1 Answers

Yes, it can be done, but it does require a small amount of code reflection to modify the appearance of individual fields in the Content Editor, since there is currently no Sitecore pipeline available for the Content Editor at a field level.

  1. Make a class MyEditorFormatter that inherits from Sitecore.Shell.Applications.ContentEditor.EditorFormatter.
  2. Using a tool like Reflector or DotPeek, copy the implementation of two methods from the original EditorFormatter into the new class:
    public virtual void RenderField(System.Web.UI.Control parent, Editor.Field field, bool readOnly)
    {...}
    public void RenderLabel(System.Web.UI.Control parent, Editor.Field field, Item fieldType, bool readOnly)
    {...}
    Note: RenderLabel is the method that writes the field-level tooltip, but since it is not virtual, the only way to override its functionality is to override RenderField, which calls it.
  3. Change the signature of RenderField from virtual to override. This will cause a call to args.EditorFormatter.RenderField to run the new code.
  4. Insert the desired tooltip logic in RenderLabel:
    if (itemField.Description.Length > 0)
    {
      str4 = " title=\"" + itemField.Description + " (custom text)\"";
    }
    Note: You can replace (custom text) with your new logic. Also note that you may wish to remove the check on the Description.Length, as this will prevent your new tooltip from appearing if the Description is not populated.
  5. Create a pipeline processor to replace Sitecore's EditorFormatter with yours:

    using Sitecore.Shell.Applications.ContentEditor.Pipelines.RenderContentEditor;
    namespace CustomizedEditor
    {
      public class ChangeToMyEditorFormatter : RenderStandardContentEditor
      {
        public void Process(RenderContentEditorArgs args)
        {
            args.EditorFormatter = new MyEditorFormatter();
            args.EditorFormatter.Arguments = args;
        }
      }
    }
    
    Populating EditorFormatter.Arguments is necessary to prevent a null object exception.
  6. Add your pipeline processor to the start of the RenderContentEditor pipeline:

    
    <renderContentEditor>
    <processor type="CustomizedEditor.ChangeToMyEditorFormatter, CustomizedEditor" />
    <processor type="Sitecore.Shell.Applications.ContentEditor.Pipelines.RenderContentEditor.RenderSkinedContentEditor, Sitecore.Client" />
    <processor type="Sitecore.Shell.Applications.ContentEditor.Pipelines.RenderContentEditor.RenderStandardContentEditor, Sitecore.Client" />
    </renderContentEditor>

Your custom tooltip will now appear:
Customized tooltip

Update: Mike Reynolds has written a very nice article showing how this approach can be used to add a "Where is this field defined" function to the Content Editor.

like image 129
Dan Solovay Avatar answered Dec 28 '22 23:12

Dan Solovay