Source button

Forums: 

Hello, I'm using h5p drupal7 module and I was trying to add extra plugin sourcedialog to the CKEditor toolbar using the guide provided here, but wasn't successful. In fact plugin was loading, but was never assigning to the actual toolbar. Maybe you can give me any clue why isn't that working for me? The source code is:

function mymodule_h5p_semantics_alter(&$semantics, $library_name = NULL) {
  // Check if this is the multichoice question type.
  if ($library_name !== 'H5P.CoursePresentation') {
    return; // Nope, do not continue.
  }

  foreach ($semantics as $field) {
    // Go through list fields
    while ($field->type === 'list') {
      $field = $field->field;
    }

    // Go through group fields
    if ($field->type === 'group') {
      mymodule_h5p_semantics_alter($field->fields, $library_name);
      continue;
    }

    // Check to see if we have the correct type and widget
    if ($field->type === 'text' && isset($field->widget) && $field->widget === 'html') {
      // Found a field. Add support for table tags.
      if (!isset($field->tags)) {
        $field->tags = array();
      }
      $field->tags = array_merge($field->tags, array(
        'sourcedialog',
      ));
    }
  }
}

function mymodule_form_h5p_content_node_form_alter(&$form, &$form_state) {
  $module_path = drupal_get_path('module', 'mymodule');
  $cache_buster = '?' . variable_get('css_js_query_string', '');
  $form['#attached']['js'][] = array(
    'data' => array(
      'h5peditor' => array(
        'testPath' => base_path() . $module_path . '/',
        'assets' => array(
          'js' => array(
            '/' . $module_path . '/extraplugins.js' . $cache_buster
          )
        )
      ),
    ),
    'type' => 'setting',
  );
}
var H5PEditor = H5PEditor || {};
H5PEditor.HtmlAddons = H5PEditor.HtmlAddons || {};
H5PEditor.HtmlAddons.sourcedialog = H5PEditor.HtmlAddons.sourcedialog || {};
H5PEditor.HtmlAddons.sourcedialog.sourcedialog = function (config, tags) {
    config.extraPlugins = (config.extraPlugins ? ',' : '') + 'sourcedialog';
    config.toolbar.push({
        name: "sourcedialog",
        items: ['Source']
    });
console.log(config);
    tags.push('sourcedialog');
};
  
(function ($) {
    $(document).ready(function () {
        if (window.Drupal === undefined) {
            window.Drupal = window.top.Drupal;
        }
        if (window.CKEDITOR !== undefined) {
            // Add plugin
            ['dialog', 'sourcedialog'].forEach(function (p) {
                CKEDITOR.plugins.addExternal(p, Drupal.settings.h5peditor.testPath + p + '/');
            });
        }
    });
})(H5P.jQuery);
thomasmars's picture

Course presentation does not actually have any html widgets in it, it only uses other libraries with html widget, so if you want for instance the text library to display your sourcedialog you have to enable changing of semantics for that library instead of doing it for H5P.CoursePresentation.

So you could for instance change your library name check from H5P.CoursePresentation to H5P.AdvancedText, like this:

// Check if this is the AdvancedText library.
if ($library_name !== 'H5P.AdvancedText') {
  return; // Nope, do not continue.
}

And similarly for any other libraries where you'd want to add sourcedialog.

I finally got it, thanks a lot.