How to Add New Snippets to CKEditor

Permalink 1 user found helpful
Does anyone know how to add new snippets to the CKEditor?

Currently C5v8.1 has two: Page Name and User Name

jfhencken
 
MrKDilkington replied on at Permalink Reply
MrKDilkington
Hi jfhencken,

I just had a quick look and was able to create new snippets, but only by adding files to the core. I am not sure how you add them through a package.

Example: add snippet through core manipulation
- create a file called UserLastOnlineSnippet.php
concrete\src\Editor\UserLastOnlineSnippet.php
- open UserLastOnlineSnippet.php and paste the following code
<?php
namespace Concrete\Core\Editor;
use User;
class UserLastOnlineSnippet extends Snippet
{
    public function replace()
    {
        $u = new User();
        $ui = \UserInfo::getByID($u->getUserID());
        return \Core::make('helper/date')->formatDateTime($ui->getLastOnline());
    }
}

- add the database entry for the snippet
INSERT INTO SystemContentEditorSnippets (scsHandle, scsName, scsIsActive) VALUES ('user_last_online', 'User Last Online', '1');

The snippet class and PHP file name must end in "Snippet":
- SomethingSnippet
- SomethingSomethingSnippet
https://github.com/concrete5/concrete5/blob/develop/concrete/src/Edi...

The database entry scsHandle must be in snake case minus the word "Snippet":
- something
- something_something

There looks to be a static method for adding the scsHandle and scsName:
https://github.com/concrete5/concrete5/blob/develop/concrete/src/Edi...

This will create a snippet called User Last Online and should get the time the user was last online. Warning, this is code is just an example, do not modify the core.

If anyone knows how to package this, I would be very interested.
mnakalay replied on at Permalink Reply
mnakalay
Hello both,

Actually I just submitted an issue on GitHub because when you try to do it from a package it fails miserably and I don't understand why.

This is the pagehttps://github.com/concrete5/concrete5/issues/5268...

It just seems to be looking for the snippet class in the wrong place but maybe I'm doing something wrong
jfhencken replied on at Permalink Reply
jfhencken
Thank you MrKDilkington for your insight and suggestions.

Thank you mnakalay for the info on your project to add snippets. Looks like Andrew gave you an answer that could make this work for you. Are you planning on offering an Add-On?
mnakalay replied on at Permalink Reply
mnakalay
Yes maybe. So far I was only testing. Managed to make it work too. I think MrK is planning a little write up about it.

I have a few ideas where it could be useful.

Do you have any idea you'd like to share?
MrKDilkington replied on at Permalink Reply
MrKDilkington
@jfhencken

It turns out there is an existing editor snippets add-on in the marketplace.
Basic Snippets Pack
https://www.concrete5.org/marketplace/addons/basic-snippets-pack...

There is mention of Redactor in the add-on description, but it is not limited to use in Redactor - it works with CKEditor.
mnakalay replied on at Permalink Reply
mnakalay
Don't we love it when we spend time reinventing the wheel ⊙▂⊙

Well I guess we learned something.

MrK, did you notice he uses the content importer to add the snippet data to the db. I wonder if the Snippet class with the add() function exists in 5.7?
MrKDilkington replied on at Permalink Reply
MrKDilkington
@mnakalay

The Snippet class looks the same in 5.7 and v8, I think using the content importer might be a personal preference.
MrKDilkington replied on at Permalink Reply
MrKDilkington
I put together a tutorial on the topic.
How to create rich text editor snippets
https://documentation.concrete5.org/tutorials/how-to-create-rich-tex...