Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating editor interface for single field resets editor interfaces and help text for other fields #155

Closed
a-lehdet opened this issue May 2, 2018 · 13 comments
Labels

Comments

@a-lehdet
Copy link

a-lehdet commented May 2, 2018

Hi,

I used the following code to change the editor interface of a single field in a content type. It appears that in the process some of the editor interfaces settings of other fields in the same content type have been changed/reset and the help texts have vanished.

client = Contentful::Management::Client.new(<TOKEN>)
space = client.spaces.find(<SPACE-ID>)
editor_interface = client.editor_interfaces.default(space_id, "content_type_id")

editor_interface.controls = [
  {
    fieldId: "fieldName",
    widgetId: "dropdown",
    settings: {
      helpText: "Here is some help text."
    }
  }
]

editor_interface.update(controls: editor_interface.controls)
@dlitvakb
Copy link
Contributor

dlitvakb commented May 2, 2018

Hey @a-lehdet,

Sorry that happened, but this because the CMA API only allows PUT requests and not PATCH requests, so you need to specify the whole payload in order to update it without losing information.

What I recommend you to do is to update it by doing something like:

client = Contentful::Management::Client.new(<TOKEN>)
space = client.spaces.find(<SPACE-ID>)
editor_interface = client.editor_interfaces(space_id, environment_id, content_type_id).default

control = editor_interface.controls.detect { |c| c['fieldId'] == 'fieldName' }
control['settings'].update('helpText' => 'some help text') unless control.nil?

editor_interface.save

This will avoid losing data by removing existing configuration from the editor interface.

Hope this helps,

Cheers

@a-lehdet
Copy link
Author

a-lehdet commented May 3, 2018

Thanks for the response and help @dlitvakb,

Now that you have explained it, it makes total sense given the structure of the payload. However, as this is a bit of "you just need to know it" issue, it might be worthwhile explaining this in the documentation more in-depth as it was not particularly intuitive nor obvious...not to me, at least. 😄

@a-lehdet
Copy link
Author

a-lehdet commented Jun 4, 2018

Hi @dlitvakb,

As the example you provided no longer works with the current version, I could really use some help as the documentation is still scarce regarding editor interfaces and help texts. Basically what I'm trying to archive is loop through all the fields of a content type and set widgetId and helpText for each field.

If you could provide a code example how this can be done, I would appreciate it very much. :)

@dlitvakb
Copy link
Contributor

dlitvakb commented Jun 4, 2018

Could you explain further what's not working? As there's no new breaking published version since that comment has been made.

Cheers

@a-lehdet
Copy link
Author

a-lehdet commented Jun 4, 2018

@dlitvakb I'm getting the following error.

Traceback (most recent call last):
1: from temp.rb:5:in `<main>'
/path/to/ruby/gems/contentful-management-2.1.1/lib/contentful/management/client.rb:229:in `editor_interfaces': wrong number of arguments (given 0, expected 3) (ArgumentError)

In addition, I'm a bit puzzled that the example connects to space rather than environment. I noticed that editor interfaces of a particular content type can be accessed like this...

environment.content_types.find("content_type_id").editor_interface.default

...but I wasn't able to figure out how to set values for widgetId and helpText of each field.

@dlitvakb
Copy link
Contributor

dlitvakb commented Jun 4, 2018

Can you provide me the snippet that throws that error?

Cheers

@a-lehdet
Copy link
Author

a-lehdet commented Jun 4, 2018

Here's the code. The purpose of the editor_interface.controls.detect loop is a bit unclear to me, but the content type in question person has a fieldId and fieldName that are called name and Name.

require "contentful/management"

client = Contentful::Management::Client.new("<TOKEN>")
space = client.spaces.find("<SPACE-ID>")
editor_interface = client.editor_interfaces.default(space.id, "person")

control = editor_interface.controls.detect do |c|
  c['name'] == 'Name'
end

control['settings'].update('helpText' => 'some help text') unless control.nil?

editor_interface.save

@a-lehdet
Copy link
Author

a-lehdet commented Jun 4, 2018

@dlitvakb

So far what I've got this, basically just shooting in the dark...

person = environment.content_types.find("person")
editor_interface = person.editor_interface.default

editor_interface.properties[:controls].each do |field|
  
  # Works, outputs the correct field value
  puts field["settings"]["helpText"] if defined? field["settings"]["helpText"]

  # Does nothing, but doesn't give any errors either
  field["settings"].update("helpText" => "testing") if defined? field["settings"]["helpText"]

end

Trying to save using editor_interface.save gives this return unknown method error. Using person.save instead doesn't return errors, but doesn't update the help text field either.

@dlitvakb
Copy link
Contributor

dlitvakb commented Jun 4, 2018

Hey @a-lehdet,

I'll dig deeper into this as the snippet above (which I now updated to support environments) should work and will provide you an answer by around tomorrow this time.

Cheers

@dlitvakb
Copy link
Contributor

dlitvakb commented Jun 5, 2018

Hey @a-lehdet,

This is indeed something that's currently wrong in the SDK, though it's not because it's a bug, it's just because it's implemented a bit differently.

I'm going to add a #save method to the SDK for editor interfaces to avoid creating a breaking change by rewriting #update.

I will create a PR and get it reviewed as soon as possible,

Cheers

@a-lehdet
Copy link
Author

a-lehdet commented Jun 5, 2018

Hi @dlitvakb,

Thanks for investigating the issue. At the moment I've decided to proceed with the approach described in the first message of this thread (i.e. set the widgets and help texts in a single hash), but latter approach would definitely be more elegant, so adding the save method to editor interface is a welcome addition.

@dlitvakb
Copy link
Contributor

dlitvakb commented Jun 5, 2018

This is the snippet with how the updated SDK works:

require 'contentful/management'

client = Contentful::Management::Client.new(ENV['CF_TEST_CMA_TOKEN'])
space = client.spaces.find('facgnwwgj5fe')
environment = space.environments.find('master')
content_type = environment.content_types.find('cat')
editor_interface = content_type.editor_interface.default
control = editor_interface.controls.detect { |c| c['fieldId'] == 'name' }

if control['settings'].nil?
  control['settings'] = {'helpText' => 'some help text'}
else
  control['settings'].update('helpText' => 'some help text')
end

editor_interface.save

Creating the PR right now,

Cheers

@dlitvakb
Copy link
Contributor

dlitvakb commented Jun 5, 2018

This has just been released as version 2.2.0

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants