Jump to content

Frontend form with tinymce or ckeditor module


pwFoo
 Share

Recommended Posts

Hi :)

I try to build a frontend form  with from template fields.

No problem to generate the form and detect the field type like InputfieldTinyMCE.

But how could I load the InputfieldTinyMCE module to use it in the frontend?

Tried to load the module with module get and print the scripts and styles inside the html head.

But I think editor have to be initialized? Any module method to call?

Link to comment
Share on other sites

TinyMCE module loaded

$modules->get('InputfieldTinyMCE');

But form fields are unstyled and without TinyMCE.

scripts and styles also loaded

<head>
    <script type="text/javascript" src="/pwProfile/wire/modules/Jquery/JqueryCore/JqueryCore.js?v=183"></script>
    <script type="text/javascript" src="/pwProfile/wire/modules/Jquery/JqueryUI/JqueryUI.js?v=193"></script>

    <script type="text/javascript" src="/pwProfile/wire/modules/Inputfield/InputfieldTinyMCE/InputfieldTinyMCE.js?v=358"></script>
    <script type="text/javascript" src="/pwProfile/wire/modules/Inputfield/InputfieldTinyMCE/tinymce-3.5.8/tiny_mce.js"></script>

    <link type="text/css" href="/pwProfile/wire/modules/Inputfield/InputfieldTinyMCE/InputfieldTinyMCE.css?v=358" rel="stylesheet" </head>

TinyMCE should be initialized at InputfieldTinyMCE.js? But it's loaded before InputfieldTinyMCE/tinymce-3.5.8/tiny_mce.js?

Changed it for testing but no editor loaded... 

Link to comment
Share on other sites

I can't see what is missing, but have you tried this approach to ensure that your call to load the module also loads all required js and and css files:

foreach($config->scripts->unique() as $file) echo "\n\t<script type='text/javascript' src='$file'></script>";
foreach($config->styles->unique() as $file) echo "\n\t<link type='text/css' href='$file' rel='stylesheet' />";
Link to comment
Share on other sites

Debugged and also changed to ckeditor for more tests...

Both have a problem with the config variable

ReferenceError: config is not defined

ckeditor generates...

<textarea id="Inputfield_body" class="FieldtypeTextarea InputfieldMaxWidth" name="body" rows="5">Test</textarea><script>CKEDITOR.replace('body', config.InputfieldCKEditor_body);</script>

Comparable error with tinymce.

So were should config.Inputfield<EDITOR> ... set?

Link to comment
Share on other sites

Fixed...

Needed code

        <script type="text/javascript">
                <?php
   
                $jsConfig = $config->js();
                $jsConfig['debug'] = $config->debug;                                                          
                $jsConfig['urls'] = array(     
                        'root' => $config->urls->root,
                        'admin' => $config->urls->admin,
                        'modules' => $config->urls->modules,                    
                        'core' => $config->urls->core,                                
                        'files' => $config->urls->files,                     
                        'templates' => $config->urls->templates,                                                   
                        'adminTemplates' => $config->urls->adminTemplates,             
                        );                                                      
                ?>
                                            
                var config = <?php echo json_encode($jsConfig); ?>;                                           
        </script>
Link to comment
Share on other sites

Finished first version of my helper module.

Generate a frontend form based on page template fields.

Additional load inputfield modules (like ckeditor or tinymce) and prefill form fields with page field values (to build a edit form) via a hook (default loaded at the moment).

Sure some bug fixes and improvements needed, but first version renders a form with page field values prefilled and ckeditor loaded.

Two unfinished modules will be rewritten and based on template2form(? don't know how to call it *g*) module...

Link to comment
Share on other sites

Strange...

First thought I have to load the modules (InputfieldTinyMCE, CKEditor) because editor wasn't loaded with via form api created form...

But now it works without a 

$modules->get('CKEditor');

or

$modules->get('InputfieldTinyMCE');

So... take the form api care about loading such modules or not?! Or is it some kind of browser / PW cache?

Link to comment
Share on other sites

  • 3 weeks later...

I'll try to get CKEditor in a front end form, did some research here but still got the feeling that I'm missing an important step.

That's what I do so far:

1. Referencing the module (I'm working with 2.4.18, so its available)

$form = $modules->get('InputfieldCKEditor');
$form->render(); // not sure about this. But its an aptly named public method of the module 
;) Though I never found example code incorperating render();
 

2.

Put that into the site's head in order to get the necessary JS and CSS loaded:

foreach($config->scripts->unique() as $file) echo "\n\t<script type='text/javascript' src='$file'></script>";
foreach($config->styles->unique() as $file) echo "\n\t<link type='text/css' href='$file' rel='stylesheet' />";

3. Within the template I would like to have CKE, the following code

<label for="body">Beschreibung:</label>
<textarea id="Inputfield_body" class="FieldtypeTextarea InputfieldMaxWidth " name="body" rows="5" name="body">
...
</textarea>
<script>CKEDITOR.replace('body', config.InputfieldCKEditor_body);</script>

Neither CKEditors JS nor CSS gets loaded and therefore it doesn't work (JS object CKEDITOR is undefined) so I get the feeling that I initalize the whole thing in a wrong way. Is there any documentation on this I have missed? Same thing goes for a jQueryUI datepicker later - I got the feeling that haven't yet grasped the basic principle.

Thanks! :)

Link to comment
Share on other sites

After some testing that worked for me ;)

// needed by ckeditor!
$modules->get('JqueryCore');

// load ckeditor
$modules->get('InputfieldCKEditor');

// here you have to build a form with InputfieldForm module and form fields!
// Examples: 

You need js config in the html head section before module styles and scripts!

// ckeditor needs some js config BEFORE you load the module styles and scripts
        <script type="text/javascript">
                <?php
   
                $jsConfig = $config->js();
                $jsConfig['debug'] = $config->debug;                                                          
                $jsConfig['urls'] = array(     
                        'root' => $config->urls->root,
                        'admin' => $config->urls->admin,
                        'modules' => $config->urls->modules,                    
                        'core' => $config->urls->core,                                
                        'files' => $config->urls->files,                     
                        'templates' => $config->urls->templates,                                                   
                        'adminTemplates' => $config->urls->adminTemplates,             
                        );                                                      
                ?>
                                            
                var config = <?php echo json_encode($jsConfig); ?>;                                           
        </script>

After that, you can output the modules styles and scripts

foreach($config->scripts->unique() as $file) echo "\n\t<script type='text/javascript' src='$file'></script>";
foreach($config->styles->unique() as $file) echo "\n\t<link type='text/css' href='$file' rel='stylesheet' />";
  • Like 1
Link to comment
Share on other sites

Thanks! Added the jQueryCore dependency (should have thought of this...) and the $jsConfig to the head. Still, there's no rendering of the modules styles and scripts, although I included...

foreach($config->scripts->unique() as $file) echo "\n\t<script type='text/javascript' src='$file'></script>";
foreach($config->styles->unique() as $file) echo "\n\t<link type='text/css' href='$file' rel='stylesheet' />";

... right after the $jsConfig and before I load module CKEditor.

Link to comment
Share on other sites

You have to load the ckeditor before you output scripts and styles ;) Load the module adds the css and js to $config->scripts and $config->styles.

foreach($config->scripts->unique() as $file) echo "\n\t<script type='text/javascript' src='$file'></script>";
foreach($config->styles->unique() as $file) echo "\n\t<link type='text/css' href='$file' rel='stylesheet' />";
  • Like 1
Link to comment
Share on other sites

You have to load the ckeditor before you output scripts and styles ;) Load the module adds the css and js to $config->scripts and $config->styles.

foreach($config->scripts->unique() as $file) echo "\n\t<script type='text/javascript' src='$file'></script>";
foreach($config->styles->unique() as $file) echo "\n\t<link type='text/css' href='$file' rel='stylesheet' />";

Hm, tried that (1. jsConfig 2. loading of jQueryCore + InputfieldCKEditor 3. loading of scripts, styles) - no change.  :huh:

Link to comment
Share on other sites

I have build an module with frontend form, but will rewrite it (again) to make it more flexible...

https://processwire.com/talk/topic/7508-template2form-module-helper/

But at the source code you should see how it works (inside the module).

You also need to build a form!

@Martijn Geerts

Thank's, was just quick search the forum and paste there ;)

  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...

Just for the record: Finally got it working with the following order:

  1. jQuery Core module init
  2. CKeditor module init
  3. jsConfig inline JS
  4. All other necessary styles and scripts via iterating through $config->styles and $config->scripts
  5. Creation of form via api
Link to comment
Share on other sites

  • 2 weeks later...

I followed this thread carefully but still cant get CKEditor to work on the frontend.

I'm building my form through the API.

My setup:

<script>
<?php
$jsConfig = $config->js();
$jsConfig['urls'] = array(
    'root' => $config->urls->root,
    'admin' => $config->urls->admin,
    'modules' => $config->urls->modules,
    'core' => $config->urls->core,
    'files' => $config->urls->files,
    'templates' => $config->urls->templates,
    'adminTemplates' => $config->urls->adminTemplates,
    'adminTemplates' => $config->urls->adminTemplates,
);
?>
var config = <?php echo json_encode($jsConfig); ?>;
</script>

<?php

$modules->get('JqueryCore');
$modules->get('CKEditor');
//$modules->get('InputfieldCKEditor');

//$config->styles->prepend($config->urls->adminTemplates . "styles/main.css?v=2");
$config->styles->append($config->urls->adminTemplates . "styles/inputfields.css");
$config->styles->append($config->urls->adminTemplates . "styles/ui.css?v=2");

$config->scripts->append($config->urls->JqueryUI . "JqueryUI.js");
$config->scripts->prepend($config->urls->JqueryCore . "JqueryCore.js");
$config->scripts->append($config->urls->adminTemplates . "scripts/inputfields.js");

So I think I'm loading everything necessary and in the correct order.

Even when I switch $modules->get('InputfieldCKEditor'); for $modules->get('CKEditor');, the editor doesn't show.

I get console errors:

Uncaught TypeError: Cannot read property 'editors' of undefined (index):111
Uncaught TypeError: Cannot read property 'plugins' of undefined InputfieldCKEditor.js?v=128:16

line 111 is

<textarea id="Inputfield_ad_content" class="FieldtypeTextarea InputfieldMaxWidth" name="ad_content" rows="5"></textarea>

All scripts are loaded and visible in the resources tab of chrome debug console.

post-1920-0-56557400-1412678549_thumb.pn

Now I'm lost and any pointers to a solution would be great.

Link to comment
Share on other sites

  • 1 month later...

Get working ckeditor (regular mode) with Processwire 2.5 in the frontend (jsConfig and JqueryCore is required!). 

To get the ckeditor loaded in inline mode it's important to not load JqueryCore! It won't work... 

After figured that out I noticed that the value isn't set correct .You get the default value every time!

["body"]=> string(8) ":IGNORE:" 

So the behavior of the ckeditor module is strange...

By the way... shouldn't be added JqueryCore as dependency to InputfieldCKEditor module? Because it won't work without. But JqueryCore will also kill ckeditor in inline mode...  :huh:

Can't test it with TinyMCE because each try to install it redirects to the ckeditor installation page. So it seems a bug in PW 2.5.

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...