Jump to content

Customise CKEditor toolbar per role


Robin S
 Share

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 18
  • Thanks 1
Link to comment
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 6
  • Thanks 1
Link to comment
Share on other sites

  • 2 years later...
On 9/14/2017 at 5:16 AM, Robin S said:

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.

 

Hi. I'm new with processwire. How can I use embed using CKEditor? Is there a way? I don't know why using the youtube/vimeo module didn't work. 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...