- Posts: 7
COMMUNITY FORUM
- Forum
- K2 Community Forum
- English K2 Community
- how to insert additional data in the database when creating an item
how to insert additional data in the database when creating an item
- doberk
- Topic Author
- Offline
- New Member
I was taking a look the the k2 files, but I don't know what file I have to override? Could you help me?
Thanks in advance.
Please Log in or Create an account to join the conversation.
- Sambroza
- Offline
- New Member
- Posts: 6
Find here an example plugin: github.com/getk2/k2-example-plugin/blob/master/example.php
You can hook your additional actions on the
onAfterK2Save
function onAfterK2Save($row, $isNew)
{
// Do something here
}
Please Log in or Create an account to join the conversation.
- doberk
- Topic Author
- Offline
- New Member
- Posts: 7
Please Log in or Create an account to join the conversation.
- doberk
- Topic Author
- Offline
- New Member
- Posts: 7
Sambroza wrote: You can do this with a K2 plugin.
Find here an example plugin: github.com/getk2/k2-example-plugin/blob/master/example.php
You can hook your additional actions on themethod.onAfterK2Save
function onAfterK2Save($row, $isNew) { // Do something here }
Hello, I was trying to create a plugin, but it doesn't work for me. This is my plugin code:
file blogNotifyPlugin.php:
<?php
// no direct access
defined('_JEXEC') or die ;
// Load the K2 Plugin API
JLoader::register('K2Plugin', JPATH_ADMINISTRATOR.'/components/com_k2/lib/k2plugin.php');
// Initiate class to hold plugin events
class blogNotifyPlugin extends K2Plugin {
public $pluginName = 'blogNotifyPlugin';
public $pluginNameHumanReadable = 'Blog Notify Plugin';
public function __construct(&$subject, $params){
parent::__construct($subject, $params);
}
public function onAfterK2Save(&$row, $isNew){
echo '<script>alert("onAfterK2Save event");</script>';
die();
return true;
}
}
and file blogNotifyPlugin.xml:
<?xml version="1.0" encoding="utf-8"?>
<extension version="2.5" type="plugin" group="k2" method="upgrade">
<name>Blog Notify Plugin</name>
<author>Doberk</author>
<creationDate>18/09/2018</creationDate>
<copyright></copyright>
<authorEmail></authorEmail>
<authorUrl></authorUrl>
<version>1.0</version>
<description>test plugin</description>
<files>
<filename plugin="blogNotifyPlugin">blogNotifyPlugin.php</filename>
</files>
</extension>
The plugin was installed from the backend using joomla installer, and installation worked good. The files was uploaded to /plugins/k2/blogNotifyPlugin folder. I have published the plugin with public access after installation. It should show a popup after creating new items, but nothing happens. Where am I wrong? I create new items from backend.
Thanks.
Please Log in or Create an account to join the conversation.
- Mohamed Abdelaziz
- Offline
- Platinum Member
- Joomla Developer
Multiple Extra Fields Groups for K2
AutoMeta for K2
Chained Fields for K2
More K2 Extensions In My Extensions Store
Please Log in or Create an account to join the conversation.
- JoomlaWorks
- Offline
- Admin
- Posts: 6218
If you want to show a message after saving, then use the Joomla message API (docs.joomla.org/Display_error_messages_and_notices) for that.
That event is great when building a K2 plugin with its own DB table(s) and you want e.g. to grab the input from the K2 item form and process/save data in the database.
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- doberk
- Topic Author
- Offline
- New Member
- Posts: 7
I have change the code according your recomendations, now the method should insert a row in a table, but still does't work, no new rows are inserted after creating items. I have checked k2 settings and I have ensured k2 plugins are enabled. I keep looking for the problem, this is the updated code of my plugin:
<?php
// no direct access
defined('_JEXEC') or die ;
// Load the K2 Plugin API
JLoader::register('K2Plugin', JPATH_ADMINISTRATOR.'/components/com_k2/lib/k2plugin.php');
// Initiate class to hold plugin events
class plgblogNotifyPlugin extends K2Plugin {
public $pluginName = 'blogNotifyPlugin';
public $pluginNameHumanReadable = 'Blog Notify Plugin';
public function __construct(&$subject, $params){
parent::__construct($subject, $params);
}
public function onAfterK2Save(&$row, $isNew){
$query = "insert into jos_f1ld_suscripciones_blog values (null,'18/09/2018','[email protected]','0','12345')";
$db->setQuery($query);
$result = $db->execute();
}
}
That query works good in phpmyadmin, I have checked it.
Please Log in or Create an account to join the conversation.
- doberk
- Topic Author
- Offline
- New Member
- Posts: 7
public function onAfterK2Save(&$row, $isNew){
$query = "insert into `jos_f1ld_suscripciones_blog` values (null,'18/09/2018','[email protected]','0','12345')";
$db = JFactory::getDbo();
$db->setQuery($query);
$result = $db->execute();
}
Many thanks for help :)
Please Log in or Create an account to join the conversation.
- JoomlaWorks
- Offline
- Admin
- Posts: 6218
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- doberk
- Topic Author
- Offline
- New Member
- Posts: 7
Mohamed Abdelaziz wrote: Change the class name to 'plgblogNotifyPlugin'
This is an important matter, but is inexact. Following the example plugin , the class name must be PlgK2 + plugin_name. In my case, my plugin name is blogNotifyPlugin and it didn't work until the class name was PlgK2blogNotifyPlugin. I comment it in case anybody with the same problem find this thread in the future.
R.
Please Log in or Create an account to join the conversation.
- Forum
- K2 Community Forum
- English K2 Community
- how to insert additional data in the database when creating an item