Manipulating field values from a Custom URL resource
You can add a line of JavaScript to your Web resource to ensure that when a content author clicks the description of a field, some arguments are passed to the Custom URL.
To get the arguments, add the following in your Web page's <head>:
<script type="text/javascript" language="javascript" src="/WebUI/Core/Controls/Popup/PopupInit.js"></script>
container- This contains the field itself. The class of this item implements Tridion.FieldBuilder.FieldsContainer.
getFields- This is a method that retrieves an array of fields grouped under the clicked label (this array is zero-based and will contain one more than item is the field is a multivalue field). The class of this item implements Tridion.FieldBuilder.FieldHandlerBase.
controller-
A
$displayobject that gives access to the item being edited through thecontroller.getItem()method, along with some other properties. The class of this item implements Tridion.DisplayController.
Using this, you can perform a number of tasks on the field:
- Setting or modifying the field value
-
To set or change the value of a field, use a method called
setValues. For example, if your field is not a multivalue field, you can set its single value to "Tridion" as follows:<!-- Set the (first) value of the field. This assumes a field of type String --> <script type="text/javascript"> var fields = window.dialogArguments.getFields(); if (fields && fields.length > 0) { fields[0].setValues(["Tridion"]); } </script> - Adding a new value to a multivalue field
-
Add a new value to a multivalue field as follows:
<!-- Add a new value and set its value to 19. This assumes a field of type Number --> <script type="text/javascript"> var fields = window.dialogArguments.getFields(); if (fields && fields.length > 0) { newField = window.dialogArguments.container.insertField(fields[fields.length - 1]); newField.setValues([19]); } </script> - Removing a value from a multivalue field
-
To remove one of the values in a multivalue field, do the following:
<!-- Remove the value at position 3. --> <script type="text/javascript"> var fields = window.dialogArguments.getFields(); if (fields && fields.length >= 3) { newField = window.dialogArguments.container.deleteField(fields[2]); } </script> - Moving a value to another position in the list of values
-
To do this, use the
moveFieldmethod.moveFieldhas the following parameters:- the field you want to move
- the field that is in the position you want to move that field before or after
- a Boolean indicating whether you want to movie the field after (
true) or before (false) that position.
For example:
<!-- Move the value at position 3 to the position directly after the 4th field. --> <script type="text/javascript"> var fields = window.dialogArguments.getFields(); if (fields && fields.length >= 4) { window.dialogArguments.container.moveField(fields[2], fields[3], true); } </script>