Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anguilla - Updating a field's value from a popup?

Tags:

tridion

I have a modal popup that appears whenever an editor tries to save a component with some values (a date field in the past in this case).

In this popup I show the editor a few options (very similar to the default "Open Shared Item" dialog) and an OK/Cancel button combo. On Cancel I fire the "cancel" event and the editor goes back to the editing screen, all good here. On "OK" I want to change the value of the field to match whatever the editor selected, then save.

I tried to use an approach with FieldBuilder and the sample Boris mentioned on this other topic but I can't get to the field from my popup dialog.

Any suggestions on how I can go and modify the xml of the item (could be also a page) from a modal popup?

EDIT: Code used in getControlForFieldName

function getControlForFieldName(name) {
    var fieldBuilder = $display.getView().properties.controls.fieldBuilder;
    var fieldsContainer = fieldBuilder.properties.input;
    var fieldsNode = fieldsContainer.getElement();
    var fieldContainer = $dom.getFirstElementChild(fieldsNode);
    while (fieldContainer) {
        var labelNode = $dom.getFirstElementChild(fieldContainer);
        var fieldNode = $dom.getNextElementSibling(labelNode);
        var control = fieldNode.control;
        if (control.getFieldName() == name) {
            return control;
        }
        fieldContainer = $dom.getNextElementSibling(fieldContainer);
    }
}

EDIT #2

After Frank's advice, and some help from Jaime & Frank offline, I got it to work as follows:

  1. The popup is called from a Command Extension (Save & Close in my case)
  2. The command.js specifies an event handler that gets called on "submit" (== OK was pressed)
$evt.addEventHandler(p.dialogPopup, "submit", 
    this.getDelegate(this._onPopupSubmit));

In my popup I am passing the selected item (it's a keyword ID) to the event handler:

this.fireEvent("submit", { id: select.options[select.selectedIndex].value });

and now back in the event handler _onPopupSubmit(e) I just read e.data.id, load this keyword, get properties like ID & Title, and update the metadata of the item using item.setMetadata("new metadata with updated values").

Simple :)

like image 911
Nuno Linhares Avatar asked Jun 26 '12 13:06

Nuno Linhares


1 Answers

Your code runs in a popup, so any references you make to global variables will be taken from the popup window.

So when you get the fieldBuilder:

var fieldBuilder = $display.getView().properties.controls.fieldBuilder;

$display is a reference to a global variable. So this actually looks for the FieldBuilder in the popup window (which doesn't have one).

To get the FieldBuilder of the Component window, you can get it from the opener:

var fieldBuilder = opener.$display.getView().properties.controls.fieldBuilder;

You might want to consider actually passing the updated value to either a callback function or with a (custom) event though, since that makes your popup less dependent on opener. trick.

like image 133
Frank van Puffelen Avatar answered Oct 15 '22 16:10

Frank van Puffelen