Keyword

K2 Plugin Development - Dynamic Extra Field

  • BitSirenDev
  • BitSirenDev's Avatar Topic Author
  • Offline
  • New Member
More
10 years 1 week ago #133897 by BitSirenDev
K2 Plugin Development - Dynamic Extra Field was created by BitSirenDev
Hi all,

We are experienced CMS and PHP developers but new to Joomla! and K2; usually working with MODX, Magento and Typo3.

We are working on an existing solution which uses K2. We are trying to implement more dynamic and customized functionality and have investigated K2 Plugins. But with a lack of official documentation (and a lack of fundamental understanding at this point) we are struggling. We will have some questions along the way and here is the first one.

We had a look at the K2 Example Plugin and get the front-end capabilities of it. But we are also looking for more flexibility in the back-end. Currently we are looking to do the following:

Have a dynamically populated Extra Field. There are two main categories of items (Venues + Events). In the events items we want a dynamically populated select object with all the registered venues. We can achieve that with this approach in the Plugin's xml

<field name="title" type="sql" default="10" label="Select an article" query="SELECT id, title FROM #__content" key_field="id" />

Now here is where we are currently struggling:

1. How can we ensure this dynamic field only appears on items with category (and sub-categories) Events, but not on Venues
2. How can we achieve different dynamic population in the front-end. (Obviously the creator of Venue X should only be able to select Venue X and not Venue Z).
3. As an alternative to 2., how can we have one one Extra Field show in the back-end (show all venues) and another in the front-end (only show user's venues).

We are well versed in PHP, we don't need code, but conceptual hints as we are not yet familiar with Joomla! and K2.

Any tips, hints (or examples) are welcome.

Adrian

Please Log in or Create an account to join the conversation.

  • Mohamed Abdelaziz
  • Mohamed Abdelaziz's Avatar
  • Offline
  • Platinum Member
  • Joomla Developer
More
10 years 1 week ago #133901 by Mohamed Abdelaziz
Replied by Mohamed Abdelaziz on topic K2 Plugin Development - Dynamic Extra Field
Hi,

For Q1, you can override the onRenderAdminForm function to restirect displaying the dynamic field to specific categories.

I think all your work will be on this function, either for backend or frontend.

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.

More
10 years 1 week ago #134070 by Abu Reviewer
Replied by Abu Reviewer on topic K2 Plugin Development - Dynamic Extra Field
Hi I also tried to create dynamic extrafield by this following ways

1. Using "onRenderAdminForm" but I do't know how to override it if I echo it not appear as tab it appeared above the Title. If I return then the k2 predefined tab "K2tab7" will enabled but my content will not display any more.

2. Using "xml k2 params" I used params field as remote field every thing as I think but I can't get current itemid. If I can get current Item id in that remote field I'll finish it within 3 days.

Please Log in or Create an account to join the conversation.

  • BitSirenDev
  • BitSirenDev's Avatar Topic Author
  • Offline
  • New Member
More
9 years 11 months ago #134481 by BitSirenDev
Replied by BitSirenDev on topic K2 Plugin Development - Dynamic Extra Field
OK. Got around this project again. Still struggling here. However, this may help Abu Reviewer (the code in example.xml below does work if item category condition does not matter.)

We can render a form item only if item is of cat X. We can dynamically populate a form field. It is the actual combination dynamic field and displaying it only if the item in question is of type X.

So here is where we are:

example.xml
Used by itself, it successfully populates the field with the data we want and it also saves the data to the item correctly.
...
<fields group="item-other">
		<field name="videoURL_venue" type="sql" query="SELECT id, title FROM jos3_k2_items WHERE catid != '2'" multiple="single" key_field="id" value_field="title" class="inputbox" label="fieldlabel" description="fielddesc" required="true" />
	</fields>

example.php
Renders the field only in items with catid 2. However, it does not save the selected option to the DB.
...
function onRenderAdminForm(&$item, $type, $tab='')
{
	$mainframe = &JFactory::getApplication();
    $pluginParams = $this->params;
    
	/* Only Execute Code on Extra Field tab */
	if($type=='item' && $tab=='other') {
		/* Only add field for event type */
		$db = &JFactory::getDBO();
		$query = 'SELECT id, title FROM #__k2_items WHERE `catid` <> 2 AND `trash` = 0 ORDER BY `title`';
		$db->setQuery( $query );
		$venueitems = $db->loadObjectList();
		if($item->catid==2) {
			$form = "<select id='videoURL_venue'>";
			if ($venueitems) {
					foreach ( $venueitems as $venue ) {
						$form .= "<option id='" . $venue->id ."'>" . $venue->title ."</option>";
					}
				}
			
			$form .= "</select>";
			$plugin = new JObject;
			$plugin->set('name', 'Whatever');
			$plugin->set('fields', $form);
			return $plugin;
		}	
	}	
}

I am sure we are approaching this from the wrong way in the example.php. Is there no option to load the plugin from config (XML) when certain conditions are met?

Next Question

Also, the onRenderAdminForm method as we have it only works if the item already exists, i.e. has a catid in the object. How can we achieve the same effect with new items? (Similar to Extra Fields is populated only after category is selected.)

Really looking for some help here.
Adrian

Please Log in or Create an account to join the conversation.

  • Mohamed Abdelaziz
  • Mohamed Abdelaziz's Avatar
  • Offline
  • Platinum Member
  • Joomla Developer
More
9 years 11 months ago #134574 by Mohamed Abdelaziz
Replied by Mohamed Abdelaziz on topic K2 Plugin Development - Dynamic Extra Field

BitSirenDev wrote: Next Question

Also, the onRenderAdminForm method as we have it only works if the item already exists, i.e. has a catid in the object. How can we achieve the same effect with new items? (Similar to Extra Fields is populated only after category is selected.)


This can be done using jQuery/AJAX

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.


Powered by Kunena Forum