2
votes

I need to take the Component and Component Template tcm ids from the Component Presentation tab of any Tridion Page.

Scenario:

  • On the exisiting page in Tridion.
  • Go to "Component Presentation" tab.
  • When I click the "Insert" button, I want to iterate the Component and Component Template tcm id and display that in JavaScript alert box.

I am trying to find the exact javascript file/function to accomplish this, I couldn't find the exact one.

I am using SDL Tridion 2011 SP1.

Any help/suggestion would be highly appreciated.

EDIT

I am having this script which will fire when we wil click the Insert button in the "Insert component presentation" window.

Tridion.Cme.Views.InsertCpDialog.prototype.onInsertClick = 
                    function InsertCpDialog$onInsertClick()
{
  var p = this.properties;
  var c = p.controls;
  var templateId = c.templateSelect.getValue();
  var components = c.list.getSelection().getItems();

  p.componentPresentationsAdded += components.length;

  var compId = components[components.length - 1];
  var component = $models.getItem(compId);
  if (component)
  {
    var userSettings = Tridion.UI.UserSettings.getInstance();
    if (userSettings && userSettings.isLoaded())
    {
      Tridion.UI.UserSettings.setLastSelectedLocation(
          component.getPublicationId(), $const.ItemType.COMPONENT, p.contextUri);
    }
  }

  this.fireEvent("insert", {
    components: components,
    template: templateId
  });
};

So here I will get the tcm id of selected Component and template "compId" & "templateId", which is to be inserted. Now my question is: how can I check whether this component and template is already present on the Component Presentation Tab?

If somehow I will be able to get the id of all component and template already present on the page (from my "Insert" button) then I can compare them. But I am not getting any function which will give me those id. Here I got stuck.

EDIT

I am trying to go inside the page with these codes but i am not able to get the correct function which is getting fired on click of Insert, or which will return me the list of component and template id listed there.

Extensions.Test.prototype.isEnabled = function Test$isEnabled(selection) {
  try
  {
    console.log($controls);
    var masterTabControl = $controls.getControl($("#MasterTabControl"), 
                                            "Tridion.Controls.TabControl");

    console.log(masterTabControl);
    alert("Mastercontrol - ComponentpresentationsTab");
    console.log(masterTabControl.getPage("ComponentPresentationsTab"));
    console.log("list of component presentations");
    console.log(masterTabControl.getPage("ComponentPresentationsTab").
                             getListComponentPresentations().getItems());
    console.log("list of get xml");
    console.log(masterTabControl.getPage ("ComponentPresentationsTab").
                             getListComponentPresentations().getItems().getXml);
  }
  (catch exc)
  {
  }
  return true;
}

EDIT I got all the CP Id listed under CP TAB with this code. Thanks to @Frank van Puffelen

var p = this.properties;
var tgp = this.properties;
var c = p.controls;

var pageId = selection.getItem(0);
var masterTabControl = $controls.getControl($("#MasterTabControl"), 
                                            "Tridion.Controls.TabControl");
var compPresTab = masterTabControl.getPage("ComponentPresentationsTab");
var comPresList = p.compPresTab.getListComponentPresentations(); 
for (var i = 0; i <= myStringArray.length; i++) 
{     
  console.log(comPresList.getItems()[i].getComponentId());
  console.log(comPresList.getItems()[i].getComponentTemplateId());     
}
2
How about you show us the code that you already wrote and where you are stuck? - Frank van Puffelen
possible duplicate of [how to write a c program to add a button ](stackoverflow.com/questions/11446313/…) - Frank van Puffelen
If you don't provide more information about what you have already done, you run a good chance that this user account will get locked out of asking questions similar to what happened to @user1518281. Stack Overflow is not meant to turn your list of requirements into a working piece of code that you can copy/paste into a solution. We can help you do your work, but only if you show us what you've already done. - Frank van Puffelen
Did you write this code? It looks you copied standard InsertCpDialog.onInsertClick method. Do you understand what this code does? If not: did you set a breakpoint in it and step through it? You'll see that it gets access to a controls array that contains all the controls in the view (in this case the InsertCpDialog popup). This logic is applicable in any view, so you can just follow the trail. - Frank van Puffelen
No i didn't tell like i write this code.I got this while debugging the site and searching for the function which is getting triggered on click of "Insert"(on the InsertCpDialog Popup) button. So i am trying to do the same for the Insert button which is Present under CP Tab on page. So i am trying to dig inside - SDLBeginner

2 Answers

3
votes

Once you have the Component Presentations tab of the Page dialog, you can get the list of Component Presentations like this

comPresList = compPresTab.getListComponentPresentations();

You can inspect the list in XML format like this:

comPresList.getXml()

Or, as you seemingly are trying in your code snippet, you can get the items themselves using:

comPresList.getItems()

This will give you a list of Tridion.ContentManager.ComponentPresentation objects, each of which you can call getComponentId and getComponentTemplateId on.

comPresList.getItems()[0].getComponentId()
comPresList.getItems()[0].getComponentTemplateId()

I got this information by taking the snippet that John Winter provided you earlier, pasting it into a Chrome JavaScript console and then doing a few text searches on the source tree (on disc) for key phrases such as "insert" (to find all handlers for the insert event) and ComponentPresentation = (to find the ComponentPresentation class).

2
votes

I answered this one here: restrict user to insert same component and template

It seems the question has been changed since so the answer now doesn't seem related, but i'm sure this will help you figure out what you need to do.