Incorporating CKEditor 5 with Nette Application
- Cupara
- Member | 90
I'm trying to incorporate CKEditor 5 with my application. As far as I know, I have to do this with my forms to get the editor to display:
<div class="form" style="width: 100%;">
{form editForm}
<input type="hidden" name="id" value="{$post->id}">
<div class="input-group mb-3">
<label for="author" class="form-label">Author</label>
<input type="text" class="form-control" id="author" name="author" value="{$post->author}" required>
</div>
<div class="input-group mb-3">
<label for="title" class="form-label">Title</label>
<input type="text" class="form-control" id="title" name="title" value="{$post->title}" required>
</div>
<div class="input-group mb-3">
<label for="slugify" class="form-label">Slug</label>
<input type="text" class="form-control" id="slugify" name="slug" value="{$post->slug}" required>
</div>
<div class="input-group mb-3">
<label for="content" class="form-label">Content</label>
<textarea class="form-control" id="content" name="content" rows="10" value="{$post->content}" required></textarea>
</div>
<div class="input-group mb-3">
<label for="updated_at" class="form-label">Updated</label>
<type="date" class="form-control" id="updated_at" name="updated_at" required>
</div>
<button type="submit" class="btn btn-success">Submit</button>
{/form}
</div>
Is there a way to incorporate it in my presenters so I can use Bootstrap forms? I found Nette-CKEditor on Github but that seems more like I would have to start over from scratch which I don't want to do since I'm very close to being finished with my site.
Also is there a way to make my slugify field autofill from the title field?
- Infanticide0
- Member | 107
https://doc.nette.org/…ms/rendering#…
Why would you want to write all input properties again? You already defined
them in createComponentEditForm method.
Using {input name} or <input n:name=“name”> in Latte you get whole
input/label HTML generated with all attributes defined (name, type, value,
rules, class).
You just add custom class=“form-control ckeditor” or data-editor for JS initializing CKEditor.
latte:
<label n:name="content" />
<input n:name="content" data-editor>
render:
<label for="content" class="form-label">Content</label>
<textarea class="form-control" data-editor id="content" name="content" rows="10" NO VALUE ATTRIBUTE HERE required>
value content
</textarea>
<script>
ClassicEditor
.create( document.querySelector( '[data-editor]' ) )
.catch( error => {
console.error( error );
} );
</script>
- Infanticide0
- Member | 107
@Cupara
Use webalize before saving form data to the database in onSuccess callback. There is not only one right way to do it, depends on use case.
//$title = "My page title!" (form input value)
$title = $values->title;
$slug = Strings::webalize($title);
//sql insert
Then you can change your router to have page URLs like
domain.com/132-my-page-title
When using slugs, you should canonicalize https://doc.nette.org/…n/presenters#…
request in your PagePresenter
Last edited by Infanticide0 (2024-01-14 00:32)
- Cupara
- Member | 90
@Infanticide0 so I'm trying to incorporate canonicalize in my files but I'm not sure what I should be doing.
It's reporting that
'Cannot read an undeclared property App\Presenters\NewsPresenter::$facade.'
I have this in my NewsPresenter.php
public function actionShow(int $id, string $slug = null): void
{
$realSlug = $this->facade->getSlugForId($id);
$this->canonicalize('News:show', [$id, $realSlug]);
}
public function renderShow(int $id): void
{
$new = $this->template->new = $this->database
->table('news')
->get($id);
if (!$new) {
$this->error('Article not found');
}
$this->template->new = $new;
$this->template->comments = $this->database->table('comments')->where('news_id', $id)->order('created_at');
}
Last edited by Cupara (2024-01-16 05:52)
- mskocik
- Member | 62
Your route definition in router should use only slug then. Like this:
// RouterFactory.php
$router->addRoute('/news/<slug .*>', 'News:show');
// NewsPresenter.php
public function actionShow(?string slug = null): {
$new = $this->template->new = $this->facade->getNewsBySlug($slug);
// etc.
}
Is it clear?
Last edited by mskocik (2024-01-16 13:01)