Jump to content
Robin S

Customise CKEditor toolbar per role

Recommended Posts

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 14
  • Thanks 1

Share this post


Link to post
Share on other sites

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 3
  • Thanks 1

Share this post


Link to post
Share on other sites
2 hours ago, tpr said:

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

Sure, be my guest. :)

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...