Presentation Details dialog throws null reference exception – Sitecore 8

Reason

When opening the Presentation Details dialog for an item (e.g. the Standard Values for a template) a null reference exception is thrown.

[ArgumentNullException: Value cannot be null. Parameter name: value]
   Sitecore.Diagnostics.Assert.ArgumentNotNull(Object argument, String argumentName) +105
   Sitecore.Web.WebUtil.SafeEncode(String value) +84
   Sitecore.Shell.Applications.Layouts.DeviceEditor.DeviceEditorForm.RenderRenderings(DeviceDefinition deviceDefinition, Int32 selectedIndex, Int32 index) +1682
   Sitecore.Shell.Applications.Layouts.DeviceEditor.DeviceEditorForm.Refresh() +622
   Sitecore.Shell.Applications.Layouts.DeviceEditor.DeviceEditorForm.OnLoad(EventArgs e) +703

The issue arises when the placeholder XML-attribute (“ph”) is missing from one or more of the elements in the “Renderings”-field.
I honestly can’t say when or why some of the “ph”-attributes went missing, as the solution in question is 2+ years old and started out as Sitecore 6.6 Update-2. All I know is that earlier versions of Sitecore handled the circumstance gracefully.
If you know what causes this issue, please leave a comment 🙂

This article is based on a Sitecore 8 Update-2 (rev. 150223).

Code

To fix the problem, follow these steps:

  1. Open the “View” tab and check “Standard fields” and “Raw values”.
    Required view settings.
  2. Copy the contents of the “Renderings”-field into a text editor, e.g. Visual Studio.
    Renderings field raw values.
  3. Search for any “r”-elements which do not contain a “ph”-attribute.
    Lines missing the 'ph' attribute
  4. Add an empty “ph”-attribute to these elements.
    'ph' attribute fixed
  5. Copy and paste the modified XML back into the “Renderings”-field.
  6. Save the item.

Example

A video of how to reproduce and solve this issue can be found here.