ldriver
I'm new here

How to get the current entity being edited via script?

Jump to solution

I have a page that has a dataset, on table template in the output channel I have added $CMS_VALUE(editorId())$ to make the section editable.  Also on the <img> tag I have added $CMS_VALUE(editorId(editorName:"st_image"))$ so the image itself is editable in ContentCreator.

I would like to know how to get the current entity being edited in ContentCreator in EasyEdit mode via script.  If I view the UID of the dataset it's something like PressRelease#694.  And it has a list of FS_REFERENCE objects.  But having added the editorId, I'm only editing one of those FS_REFERENCE entities.  Is there a way to get the current entity being edited?

0 Kudos
1 Solution

Accepted Solutions

Hi Leland,

if I understand it correctly, you want to change the image when the user clicks on the "external gallery" button in the form and has chosen something, right?

So you should not work on the persisted data but on the form field containing the image itself.

First, you can provide input components in the form as parameters to the button by using the #field.* syntax:

<FS_BUTTON 

  name="st_custom_button" 

  hidden="no" 

  icon="fs:new" 

  onClick="class:class.name.goes.here" 

  style="firstspirit"

  useLanguages="no"> 

  <LANGINFOS> 

    <LANGINFO lang="*" label="External Gallery"/> 

  </LANGINFOS> 

  <PARAMS>

     <PARAM name="imageGalleryField">#field.tt_picture</PARAM>

  </PARAMS>

</FS_BUTTON> 

Beeing "tt_picture" the name of the input component you want to access. Of course you could also add more parameters.

In the Executable, you can then access the field and its value using

FormField<TargetReference> targetReferenceField = (FormField<TargetReference>) params.get("imageGalleryField");

TargetReference reference = targetReferenceField.get();

Media media = reference.getMedia();

The important thing is that you really get the FormField here, so you could even set it to another value later using targetReferenceField.set(...)

Hope this helps 😉

Michael

View solution in original post

0 Kudos
4 Replies
ldriver
I'm new here

Also, a few more details, I'm calling a script from FS_BUTTON, so the class implements Executable, is it possible to get the current entity being edited with BaseContext:

Execute method:

public Object execute(Map<String, Object> params, Writer writer, Writer writer1) {

    context = (BaseContext) params.get(PARAM_CONTEXT);

    return null;

}

0 Kudos

Here are more details on the scenario.

I want to know how to reference the object I'm editing via Easy Edit in a Java class. I have a list of images in an FS_LIST object.  And each image has the code snippet to make it editable in ContentCreator, so as an example we want the editor to be able to change each image:

image-easy-edit.png

And when you click edit, the image edit opens, in which I have a custom FS_BUTTON, used to choose an image from an external gallery (if they want to):

<FS_BUTTON

  name="st_custom_button"

  hidden="no"

  icon="fs:new"

  onClick="class:class.name.goes.here"

  style="firstspirit"

  useLanguages="no">

  <LANGINFOS>

    <LANGINFO lang="*" label="External Gallery"/>

  </LANGINFOS>

</FS_BUTTON>

edit-image.png

I have Java code written which communicates from the external gallery and the executable class. Basically I can do things like Logging.logInfo("Message here", this.getClass()) when I interact with the external gallery, I can pass and read parameters (using top.WE_API.Common.execute to communicate with the class from JavaScript).

The thing I am missing is how can I get that image entity that I want to edit (in the first screen shot) so I can populate it.  Here is a very rudimentary snippet of something I have used in Beanshell to create a section, where you have the context, and from that you can create a Section:

final StoreAgent storeAgent = context.requireSpecialist(StoreAgent.TYPE);

final Store templateStore  = storeAgent.getStore(Store.Type.TEMPLATESTORE);

SectionTemplate mainContentTemplate = templateStore.getStoreElement("st_main_content", IDProvider.UidType.TEMPLATESTORE);

Body mainContentArea = page.getBodyByName("main_content");

Section mainContentSection = mainContentArea.createSection("Main Content", mainContentTemplate);

What I need is when the user clicks a button on the external gallery, I call top.WE_API.Common.execute to call my Executable class, I want to update the image section.  Similar to that Beanshell example above, at the point I will writing code I only have BaseContext:

public Object execute(Map<String, Object> params, Writer writer, Writer writer1) {

    context = (BaseContext) params.get(PARAM_CONTEXT);

    return null;

}

So is it possible using a class that implements Executable to get the entity being edited in ContentCreator (first screens shot above)?

0 Kudos

Hi Leland,

if I understand it correctly, you want to change the image when the user clicks on the "external gallery" button in the form and has chosen something, right?

So you should not work on the persisted data but on the form field containing the image itself.

First, you can provide input components in the form as parameters to the button by using the #field.* syntax:

<FS_BUTTON 

  name="st_custom_button" 

  hidden="no" 

  icon="fs:new" 

  onClick="class:class.name.goes.here" 

  style="firstspirit"

  useLanguages="no"> 

  <LANGINFOS> 

    <LANGINFO lang="*" label="External Gallery"/> 

  </LANGINFOS> 

  <PARAMS>

     <PARAM name="imageGalleryField">#field.tt_picture</PARAM>

  </PARAMS>

</FS_BUTTON> 

Beeing "tt_picture" the name of the input component you want to access. Of course you could also add more parameters.

In the Executable, you can then access the field and its value using

FormField<TargetReference> targetReferenceField = (FormField<TargetReference>) params.get("imageGalleryField");

TargetReference reference = targetReferenceField.get();

Media media = reference.getMedia();

The important thing is that you really get the FormField here, so you could even set it to another value later using targetReferenceField.set(...)

Hope this helps 😉

Michael

0 Kudos

Thank you for the help, this helped me get passed my sticking point.

0 Kudos