Robin S

Customise CKEditor toolbar per role

Recommended Posts

Robin S    2,502

Just something I was trying out recently that might be useful to someone...

With the following hook added to /site/ready.php you can adjust the CKEditor toolbar that a particular role gets for a particular field. Use the name of your CKEditor field in the hook condition.

$this->addHookBefore('Field(name=my_ckeditor_field)::getInputfield', function(HookEvent $event) {
    $field = $event->object;
    // Define toolbar for a particular role
    if($this->user->hasRole('editor')) $field->toolbar = 'Format, Bold, Italic, -, NumberedList, BulletedList, Outdent, Indent';
});

Or what I find useful on some sites is adding extra toolbar buttons for superuser only that you don't trust editors to use.

$this->addHookBefore('Field(name=my_ckeditor_field)::getInputfield', function(HookEvent $event) {
    $field = $event->object;
    // Add extra buttons for superuser only
    if($this->user->isSuperuser()) $field->toolbar .= ', Table, TextColor';
});

You could use the same technique to selectively define other CKEditor settings such as 'stylesSet', 'customOptions', 'extraPlugins', etc.

 

  • Like 11
  • Thanks 1

Share this post


Link to post
Share on other sites
tpr    2,218

This is actually so useful  that I need to add to AOS :) (if you don't mind)

Removing toolbar items is easy with your code but AOS may add extra buttons via CKEditor plugins. I found out that listing them in "removePlugins" is enough, so this can be used to remove extra plugins with ease:

$this->addHookBefore('Field(inputfieldClass=InputfieldCKEditor)::getInputfield', function(HookEvent $event) {

  // do not show modified data on Field edit page
  if ($this->wire('process') != 'ProcessPageEdit') {
  	return; 
  }

  $field = $event->object;

  if($this->wire('user')->hasRole('editor')) {
      $field->toolbar = 'Bold, Italic';
      $field->removePlugins = 'div, justify';	// plugins to remove
      $field->formatTags = 'p;h2;h3;h4';	// allowed format tags, separated with semicolon
  }
});

 

  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.