Managing Wygwam’s settings via code

Posted on in expressionengine

Managing Wygwam’s settings via code

A return to Wygwam

Since I created EECK, I’ve not used Wygwam. I created EECK more for the integration with CK Finder with a standalone file field than anything else. A free wysiwyg was just a bonus.

With the release of Assets, there’s now a file manager that is a good rival for CK Finder. It’s not perfect (I don’t like the interface), but the meta data and multi file upload is great. I received requests almost from day one to integrate EECK with Assets, but it quickly became apparent it was easier to take another look at Wygwam.

Three factors made me decide to switch back to Wygwam. The first was Assets integration, the second was deferred loading of the editor. That was a new feature since I last checked in, but it’s a very useful addition. The clincher was the wygwam_config extension hook.

Annoyances remain

The thing that always annoyed me about Wygwam was the graphical configuration. I’m sure all you designers love it, but I find it horribly cumbersome to browse through to the settings page and then individually drag icons around. It takes AGES.

All I want to do is quickly edit a PHP or Javascript text file, and fortunately the extension hook allows that.

Extension to the rescue

Making the extension that allows you to configure using PHP is simple. First off, download a template package from the brilliant http://pkg.io/ as follows:

  • Give your extension a name and fill out your author details
  • Select ‘Extension’ from ‘Package Content’s
  • Set ‘Has Control Panel Settings’ to No
  • Set Extension description to something useful (‘Wygwam configuration’ perhaps)
  • Pick the first Extension hook from the list, you’ll overtype it soon.
  • In the resulting method mapping box, type ‘placeholder’

Place the downloaded package in your third_party folder and then open up the extension.

First off we need to change some placeholders to actual names. Locate the function activate_extension and change the name of the placeholder and the extension you picked (in my case channel_entries_query_result) to wygwam_config. The function will now look like this:

public function activate_extension() { // Setup custom settings in this array. $this->settings = array(); $data = array( 'class' => __CLASS__, 'method' => 'wygwam_config', 'hook' => 'wygwam_config', 'settings' => serialize($this->settings), 'version' => $this->version, 'enabled' => 'y' ); $this->EE->db->insert('extensions', $data); }

Next locate the function placeholder and replace it with this

/** * wygwam_config * * Adjust the Wygwam config without messing around with icon draggers * * @param array $config * @param array $settings */ public function wygwam_config($config,$settings) { // your code here }

The next step is to decide what you want to adjust about Wygwam. For me it’s the toolbar and a couple of other bits. This is my start point:

$config['height'] = 250; $config['startupOutlineBlocks'] = 'y'; $config['toolbar'] = array( array( 'Maximize', 'Source','ShowBlocks','-', 'Templates', '-','PasteText','PasteFromWord', 'SpellChecker', '-', 'Undo', 'Redo', '-', 'ShowBlocks', '-', 'Bold', 'Italic', '-', 'Subscript','Superscript' ), array ( 'NumberedList', 'BulletedList','-','SpecialChar' ), '/', array( 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'Outdent', 'Indent','HorizontalRule', 'Blockquote' ), array( 'Link', 'Anchor','Unlink', 'Image' ), array( 'Format', 'Styles' ) ); return $config;

You might need a bit of trial and error to get the line breaks and gaps as you want them. You can also add in anything from the CK Editor config here.

You are also free to use any PHP logic to adjust the configuration. I like to add a custom configuration to Wygwam and only use my extension to adjust the toolbar for instances that are set to use it. I do this by adding the following at the beginning of the function:

if($settings['config'] != 3) { return $config; }

The $settings array contains all sorts of useful information. $settings[‘config’] is the unique ID of the Wygwam cofiguration. As it comes with two out of the box, my custom one is ID 3. The above code simply returns the supplied config unchanged if we’re not using ID 3.

Now you may have noticed that all this probably took longer than configuring Wygwam – but if you make a lot of EE sites then that time adds up – and this extension can be re-used.

There is also much more that can be done that is not possible without the extension. You could for example return a different toolbar for Super Admins like so:

if($this->EE->session->userdata[‘group_id’] == 1) { $config[‘toolbar’] = array( ) ; // layout 1 } else { $config[‘toolbar’] = array( ) ; // layout 2 }

If sharing channel fields across channels, you could change the configuration based on channel ID. You could even change things for an individual member or based on the current entry status. Anything you like really.

Comments