- Posts: 20
COMMUNITY FORUM
K2 Category Plugin developments
- Dario Pintarić
- Topic Author
- Offline
- Junior Member
Less
More
6 years 8 months ago - 6 years 8 months ago #167250
by Dario Pintarić
K2 Category Plugin developments was created by Dario Pintarić
Hi all...
I'm facing a weird problem in K2 category.... I've developed a K2 plugin that basicly let's you select any articles that you wan't to show on the category list (via slightly modified k2modalselector field type).
Now the problem is.... when i select 1 or more articles - the plugin params get saved.....
Imagine we just selected an article with ID 1 and 2... all well, my params get saved....
Now after it's saved once, if i remove those two articles - so i have no articles selected - they magicaly reappear......
The delete itself works ok, but the problem is when there are no items selected - but were selected and saved before - K2 will totally ignore plugin database colum since there were no plugin inputs on the admin form....
Let me remind yout that when you select an item with k2modalselector - it will add hidden input for every article.... but if no articles were selected - there are no inputs....
I've "fixed" this by editing /administrator/components/com_k2/models/category.php and changing the line
into
because "plugins" array key is not set inside POST, and therefore (probably) doesn't get updated in the table....
What i did is a K2 core hack, and i don't wanna do this....
So my question is... Am i doing something wrong or is it a K2 bug ?
How do i reset plugin value to be "none" / "empty" or entirely remove plugin params from the plugins database column ?
I'm facing a weird problem in K2 category.... I've developed a K2 plugin that basicly let's you select any articles that you wan't to show on the category list (via slightly modified k2modalselector field type).
Now the problem is.... when i select 1 or more articles - the plugin params get saved.....
Imagine we just selected an article with ID 1 and 2... all well, my params get saved....
Now after it's saved once, if i remove those two articles - so i have no articles selected - they magicaly reappear......
The delete itself works ok, but the problem is when there are no items selected - but were selected and saved before - K2 will totally ignore plugin database colum since there were no plugin inputs on the admin form....
Let me remind yout that when you select an item with k2modalselector - it will add hidden input for every article.... but if no articles were selected - there are no inputs....
I've "fixed" this by editing /administrator/components/com_k2/models/category.php and changing the line
if (!$row->bind(JRequest::get('post')))
into
$post = JRequest::get('post');
if(!isset($post['plugins']))
$post['plugins'] = array();
if (!$row->bind($post))
because "plugins" array key is not set inside POST, and therefore (probably) doesn't get updated in the table....
What i did is a K2 core hack, and i don't wanna do this....
So my question is... Am i doing something wrong or is it a K2 bug ?
How do i reset plugin value to be "none" / "empty" or entirely remove plugin params from the plugins database column ?
Last edit: 6 years 8 months ago by Dario Pintarić.
Please Log in or Create an account to join the conversation.
- Dario Pintarić
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 20
6 years 8 months ago #167290
by Dario Pintarić
Replied by Dario Pintarić on topic K2 Category Plugin developments
Never mind.... i got it.....
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
- Offline
- Platinum Member
Less
More
- Posts: 15920
6 years 8 months ago #167292
by Krikor Boghossian
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by Krikor Boghossian on topic K2 Category Plugin developments
What was the issue?
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Dario Pintarić
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 20
6 years 8 months ago - 6 years 8 months ago #167311
by Dario Pintarić
Replied by Dario Pintarić on topic K2 Category Plugin developments
Well, since K2 will only "react" to plugins if an input value is actually present and passed in POST, I've inserted a hidden input with the same name and an empty value - just not array....
S i did
before the K2modalselector ordered list - which inserts hidden inputs - but as an array...
And then when you delete all the articles/tems, an empty value of the same variable name will be passed, and if you selected anything - the first set variable name gets "overridden" with the array of values after it (containing the same name)...
Works like a charm - and no K2 core hacks :)
S i did
<input name="jform[someinputname]" value="" />
before the K2modalselector ordered list - which inserts hidden inputs - but as an array...
<input name="jform[someinputname][]" value="1"/>
<input name="jform[someinputname][]" value="2"/>
And then when you delete all the articles/tems, an empty value of the same variable name will be passed, and if you selected anything - the first set variable name gets "overridden" with the array of values after it (containing the same name)...
Works like a charm - and no K2 core hacks :)
Last edit: 6 years 8 months ago by Dario Pintarić. Reason: Grammar nazzi :)
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
- Offline
- Platinum Member
Less
More
- Posts: 15920
6 years 7 months ago #167332
by Krikor Boghossian
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by Krikor Boghossian on topic K2 Category Plugin developments
Kudos Dario!!!
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Dario Pintarić
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 20
6 years 1 week ago - 6 years 1 week ago #170232
by Dario Pintarić
Replied by Dario Pintarić on topic K2 Category Plugin developments
I think you have to use the name "items", not "setItems" for the field name, because the field name is hardcoded in the script that adds items to the list....
I am 99% sure of this...
I am 99% sure of this...
Last edit: 6 years 1 week ago by Dario Pintarić.
Please Log in or Create an account to join the conversation.
- JoomlaWorks
- Offline
- Admin
Less
More
- Posts: 6218
6 years 1 week ago #170260
by JoomlaWorks
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by JoomlaWorks on topic K2 Category Plugin developments
The element/field works just fine. There is no limitation to the "name" value of course.
It's what happens when you "save" the item edit form. Or when you read the plugin data.
In Joomla 3.8.x+ I noticed that fetching the plugin data in the item form only works when you don't specify the 3rd parameter here:
Can you show the main php code of the plugin please?
It's what happens when you "save" the item edit form. Or when you read the plugin data.
In Joomla 3.8.x+ I noticed that fetching the plugin data in the item form only works when you don't specify the 3rd parameter here:
$plugins = new K2Parameter($item->plugins, '', ''); // Setting $this->pluginName as 3rd parameter does not work in J3.8.x)
Can you show the main php code of the plugin please?
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- JoomlaWorks
- Offline
- Admin
Less
More
- Posts: 6218
6 years 1 week ago #170331
by JoomlaWorks
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by JoomlaWorks on topic K2 Category Plugin developments
The K2 Plugin API does replacements in all field names before they are rendered in the form. So a field name called "items" is converted to "plugins[items]". But in the case of the new modal selector, the fields are added using JS and thus they have not been converted yet. Until I resolve this, do the following. Copy the file "/administrator/components/com_k2/elements/k2modalselector.php" inside your plugin structure and edit line 53 from:
to:
This will make that field render properly for K2 plugins.
I will make sure this is fixed in K2 v2.10 or 2.11.
$fieldName = $name.'[]';
to:
$fieldName = 'plugins[' . $name . '][]';
This will make that field render properly for K2 plugins.
I will make sure this is fixed in K2 v2.10 or 2.11.
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- JoomlaWorks
- Offline
- Admin
Less
More
- Posts: 6218
6 years 1 week ago - 6 years 1 week ago #170332
by JoomlaWorks
As a sidenote, if you rename <field type="text" name="itemsID" size="20" default="" label="Item IDs" description="IDs de los ítems de K2 a incluir, separados con coma" /> to <field type="text" name="itemsID[]" size="20" default="" label="Item IDs" description="IDs de los ítems de K2 a incluir, separados con coma" /> (notice the name value now uses [] at the end), it will also work. This was probably an assumption at the time for elements that expect to return arrays.
UPDATE: The above won't work as the new k2modalselector element is a special element and the K2Plugin API can't "augment" it. But I'll make sure to fix this like I said in the coming updates. For the time being, simply copy the element as I mentioned previously and change that single line of code.
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by JoomlaWorks on topic K2 Category Plugin developments
UPDATE: The above won't work as the new k2modalselector element is a special element and the K2Plugin API can't "augment" it. But I'll make sure to fix this like I said in the coming updates. For the time being, simply copy the element as I mentioned previously and change that single line of code.
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Last edit: 6 years 1 week ago by JoomlaWorks.
Please Log in or Create an account to join the conversation.
- JoomlaWorks
- Offline
- Admin
Less
More
- Posts: 6218
6 years 1 week ago #170334
by JoomlaWorks
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by JoomlaWorks on topic K2 Category Plugin developments
I have just updated K2 v2.9.1 (dev) with a fix. Now using k2modalselector directly from the K2 /elements folder will work as expected (no need to move the file over to your plugin to edit).
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.