1

I'm making a module and almost complete, but I can't seem to get third party settings to save.

Here is my schema.yml:

editor.editor.*.third_party.ckeditor_styles:
  type: mapping
  label: 'CKEditor Styles'
  mapping:
    ckeditor_styles_stylesheets:
      type: sequence
      label: 'Stylesheets'
      sequence:
        type: string
        label: 'Stylesheet'

Form code:

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 */
function ckeditor_styles_form_filter_format_edit_form_alter(&$form, FormStateInterface $form_state) {
  $editor = $form_state->getFormObject()->getEntity();
  $stylesheets = $editor->getThirdPartySetting('ckeditor_styles', 'ckeditor_stylesheets');

  $form['ckeditor_styles'] = [
    '#type' => 'fieldset',
    '#open' => TRUE,
    '#title' => 'CKEditor CSS Styles',
    '#weight' => -5,
  ];

  $form['ckeditor_styles']['ckeditor_stylesheets'] = [
    '#type' => 'textarea',
    '#title' => 'Stylesheets',
    '#description' => 'Enter paths to custom CSS files you want to include when the editor is loaded. Example: themes/custom/mytheme/css/style.css. Seperate multiple files with a comma.',
    '#default_value' => '' // need to default to the values from the third party settings
  ];

  $form['actions']['submit']['#submit'][] = 'ckeditor_styles_form_submit';
}

/**
 * Submit handler to save stylesheets as third party settings for the Editor $editor entity.
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 */
function ckeditor_styles_form_submit($form, FormStateInterface $form_state) {
  $editor = $form_state->getFormObject()->getEntity();
  $stylesheets = $form_state->getValue(['ckeditor_styles', 'ckeditor_stylesheets']);

  if (isset($stylesheets)) {
    $stylesheets = explode(',', $stylesheets);

    foreach ($stylesheets as $stylesheet) {
      $editor->setThirdPartySetting('ckeditor_styles', 'stylesheets', trim($stylesheet));
    }
  }
}

Each iteration overwrites the previous value in the foreach, and no data is saved. Where am I going wrong?

  • If you remove the foreach and just have $editor->setThirdPartySetting('ckeditor_styles', 'stylesheets', 'somestyle.css'; does it work? – No Sssweat Jan 14 '17 at 23:37
  • But the idea is to store multiple values. – Kevin Jan 14 '17 at 23:44
  • Can't you just list them out in your modules.info.yml file just like you would in a theme.info.yml ? – No Sssweat Jan 14 '17 at 23:49
  • The point is to allow someone to configure what stylesheets to load from the admin instead of editing theme info files (non developers) – Kevin Jan 14 '17 at 23:57
  • As this would be familiar to them like it was for the WYSIWYG module in 7. Plus, I wanted to learn a bit with the third party settings api. – Kevin Jan 15 '17 at 0:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.