I'm following your instructions to add C++. I have built hightlight.js to include C++, (I believe it's called cpp in the .js file). But I'm absolutely perplexed by "create module and attach hook". Can you elaborate for the new drupal user?

Thanks,

M

CommentFileSizeAuthor
#3 codesnippet.png52.95 KBkevinquillen
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

monty_t_hall created an issue. See original summary.

monty_t_hall’s picture

Title: How to at C++ - am I reading your instructions correct? » How to add C++ - am I reading your instructions correct?
kevinquillen’s picture

FileSize
52.95 KB

At the moment, in order to create the option for C++, you will need to create a simple module that will implement hook_form_FORM_ID_alter().

Below is an example of what your module might look like. Instead of mymodule, your module name would be something like codesnippet_extras. Let's assume that is the case.

In your MYMODULE.module file, you would put this:


use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_FORM_ID_alter().
 * Add extra languages for CodeSnippet 
 * @param $form
 * @param FormStateInterface $form_state
 * @param $form_id
 */
function codesnippet_extras_form_filter_format_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (isset($form['editor']['settings']['subform']['plugins']['codesnippet'])) {
    $form['editor']['settings']['subform']['plugins']['codesnippet']['highlight_languages']['#options']['cpp'] = 'C++';
    asort($form['editor']['settings']['subform']['plugins']['codesnippet']['highlight_languages']['#options']);
  }
}

You don't necessarily need the asort() line, but I like the languages to be alphabetical (thats just me). This alter only fires when viewing the text filter format form and if codesnippet is present in the form array.

Attached is a pic of the result when the new module is enabled.

Sorry if it seems like a bit much for a newcomer, but I wanted to avoid going the kitchen sink route with HightlightJS - it would mean an additional like 220kb JS file being loaded per page just to cover all the bases of syntax highlighting.

  • kevinquillen committed 0997269 on 8.x-1.x
    Issue #2725579 by kevinquillen: How to add C++ - am I reading your...
kevinquillen’s picture

Status: Active » Fixed

I've updated the documentation to reflect this working example as well as the project page.

kevinquillen’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.