Monday, December 26, 2016

Edit Templates From Page | Alchemy Plugin

SDL Developers India Community organized a professionals meet this year in New Delhi and I was fortunate to join the event. I met some really nice guys from the community there and had a good time. I also spoke on Alchemy along with Robert Curtelle and after finishing the session, this guy Tejas came to me with this idea of the plugin.

He said, as a Tridion developer when he found a rendering bug on a webpage (published from SDL WEB), he used to do the following:
1. Navigate to the page in Tridion.
2. See which templates were used.
3. Memorize the Name or TcmId of the template.
4. Navigate to the template to open it.
5. Check the TBBs used in the template to identify/fix the issue.

He asked me it would be really helpful, if we could build a plugin which gives links/buttons to Edit the templates directly from the page. With this we could eliminate the steps 2, 3, 4 from the process above. I really liked the Idea and almost after a year I manged to build the plugin (Yeah, I know I am lazy :) )

What it does?:

Well, it is one of the simplest plugins you would have. It simply places edit buttons in form of the templates used on the page. User can simply click the buttons to Open/Edit the template.
Page Template Edit Button

Let's build it once again:

I've already shared the code for the plugin on GitHub. You can have a look at the code in detail after downloading it but let's discuss it briefly here.
In technical terms, we want to build a "ExtensionGroup" GUI Extension using Alchemy, which has group of resource files, to be targeted to the Page View ("Tridion.Web.UI.Editors.CME.Views.Page").
So after creating a project using the A4T template in Visual Studio, I created two classes:

1. EditTemplatesFromPageResourceGroup (Extending from ResourceGroup base class)

2. EditTemplatesFromPageExtensionGroup (Extending from ExtensionGroup base class)


As the comment inside the code says, this code simply adds "EditTemplatesFromPageResourceGroup" Resource Group as an Extension to extend the resource "Tridion.Web.UI.Editors.CME.Views.Page". That means all the resource files referenced in the class "EditTemplatesFromPageResourceGroup" will be available on the Page.


This class represents a Resource Group, we want to add for the extension.
From line 17 to 19 we are adding JS and CSS files needed.
Line 21 adds a Web API Proxy in case we want to interact with CMS using Web API Controller, Though we are not using it in this version but added it for future versions.
Line 23 adds JQuery as a dependency. With this we can have JQuery  available in our JS files. If you look at the JS code, you will see we are creating JQuery object something like

$JQ = Alchemy.library("jQuery");

Okay, lets talk about the resource files used in the Resource Group . It has the following resource files:

1. PageTemplateEditLink.js
  This JS file is responsible to render the Page Template Edit Button. If you look at the code, it simply
  1. Create a HTML Element for the button
  2. Insert it beside the Page Template dropdown
  3. Add a "click" event handler function to the button, which runs the following JS code
var pageTemplateId = page.getPageTemplateId();
var s = new Tridion.Cme.Selection();
$commands.executeCommand("Open", s);

The code gets the current Page Template from the Page object and fires a "Open" command on it using Anguilla framework.

2. ComponentTemplateEditLink.js
This JS file is responsible to render the Component Template Edit Button/s. What is does:
  1. Create a HTML Element for the button
  2. Insert it beside the Component Template dropdown in Component Presentations Tab
  3. Add a "click" event handler method to the button as below
// Get the selected Component template from the dropdown anguilla object
var ctId = ddl.getValue();
// Execute the open command
var s = new Tridion.Cme.Selection();
$commands.executeCommand("Open", s);

The code gets the current Component Template from the dropdown object and fires a "Open" command on it using Anguilla framework.

3. EditTemplatesFromPage.css
This file contains all the css styles used for the Edit Buttons.

After updating a4t.xml file with the name and description, now we are all set to build our plugin and after a successful build we will get the magic A4T file, which can be installed to Tridion SDL Web.

Again, I have shared the plugin code on GitHub you can download it if you want. In case you have any questions, feel free to contact or write comments. 

1 comment:

  1. Awesome! Useful, when you are in hurry to fix code issue on page template.