Thursday 8 March 2012

Manipulating (embedded!) fields from a Custom URL resource


As of SDL Tridion 2011, we have a new API for manipulating fields within Web resources referenced by Custom URLs. You can find some basic examples on live documentation.

I got a question by a customer on how exactly to deal with embedded fields. I played around a little bit with it and I thought… let‘s share the result!  :) It is actually pretty simple, we deal with embedded fields in the same way as we do for the rest of fields.

My use case

Custom URL associated to field Paragraphs:
·         Embedded schema Field, that contains two inner fields
o   Heading
o   Text
·         Multivalue

When we click on the Paragraphs field description, we will set default values to both heading and text as an example. This is the expected result:





My Web Resource

I think the code speaks by itself

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head/>
  <script type="text/javascript">
  //<![CDATA[
  var opener = window.dialogArguments;

  function load()
  {
    if (opener)
    {
       var fields = opener.getFields();
          
       if (fields && fields.length > 0)
       {
          var firstValueEmbField = fields[0];
          var headingField = firstValueEmbField.getField("heading",false);
          headingField.setValues(["Inserting heading text"]);

          var textField = firstValueEmbField.getField("text",false);
          textField.setValues(["Inserting paragraph text "]);
       }
       window.returnValue = 0;            
       //Leaving the window open, showing whatever is displayed inside 
       //the body tag
       //window.close();
      }
  }
  //]]>
  </script>
<title>Tridion</title>

<body style="margin:0px;overflow:no" scroll="no" onload="load()" >
    My test on Custom Url
</body>

</html>

Don’t forget to use the developer tools that come with the browser!

If you use Firefox, open Firebug and work on the Console tab. You can paste the code you are testing and get syntax completion like shown in the screenshot below.



Aaaand remember you can even debug the javascript if you work on the Script tab, with break points and all.

2 comments:

  1. Monica, good start, just would wish a bit more details:

    1. Where do you put that markup for the popup window? Is is some .html page within Tridion web?

    2. What if i want to apply custom value selector for each value of an multivalue field? As i understand you're assuming, that there's just one value, ain't it so?

    ReplyDelete
    Replies
    1. Hey Taras,

      Yes, the post does not go into basic details, only wants to solve the embedded fields challenge. To your questions:
      1) That is correct :) The html page is located somewhere under web, and you add that location to the Custom URL property of your field
      2) I am not really assuming there is just one value, but the goal was to show how to access the value. Of course, you could have a loop to access every value, and then iterate on fields[x]. Remember you can add values yourself too

      Cheers
      Mónica

      Delete