Created
April 4, 2016 06:40
-
-
Save renekorss/946ac4ac9e5c123d862294000a2f2192 to your computer and use it in GitHub Desktop.
Custom Joomla! form field to generate extensions (components, modules, plugins, libraries) dropdown list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Custom Joomla! form field to generate extensions dropdown list | |
* | |
* @author Rene Korss <[email protected]> | |
* @copyright 2016 All rights reserved. | |
* @license MIT | |
* | |
* Usage examples: | |
* | |
* <field | |
* name="componentpicker" | |
* type="extensions" | |
* label="Choose component" | |
* > | |
* <option value="">Please choose component</option> | |
* </field> | |
* | |
* <field | |
* name="modulepicker" | |
* type="extensions" | |
* extensiontype="module" | |
* clientid="1" | |
* label="Choose module" | |
* > | |
* <option value="">Please choose module</option> | |
* </field> | |
* | |
* <field | |
* name="pluginpicker" | |
* type="extensions" | |
* extensiontype="plugin" | |
* clientid="1" | |
* label="Choose plugin" | |
* > | |
* <option value="">Please choose plugin</option> | |
* </field> | |
* | |
*/ | |
defined('JPATH_PLATFORM') or die; | |
JFormHelper::loadFieldClass('list'); | |
/** | |
* Form Field class for the Joomla Framework. | |
*/ | |
class JFormFieldExtensions extends JFormFieldList | |
{ | |
/** | |
* The field type. | |
* | |
* @var string | |
*/ | |
protected $type = 'Extensions'; | |
/** | |
* The extension type to seaarch for. | |
* | |
* @var string | |
*/ | |
protected $extensionType = 'component'; | |
/** | |
* The path to folder for plugins. | |
* | |
* @var string | |
*/ | |
protected $folder; | |
/** | |
* The client if to search extensions. | |
* | |
* 1 - backend, 0 - frontend | |
* | |
* @var int | |
*/ | |
protected $clientId; | |
/** | |
* Method to get certain otherwise inaccessible properties from the form field object. | |
* | |
* @param string $name The property name for which to the the value. | |
* | |
* @return mixed The property value or null. | |
*/ | |
public function __get($name) | |
{ | |
switch ($name) | |
{ | |
case 'folder': | |
case 'clientId': | |
return $this->{$name}; | |
case 'extensiontype': | |
return $this->extensionType; | |
} | |
return parent::__get($name); | |
} | |
/** | |
* Method to set certain otherwise inaccessible properties of the form field object. | |
* | |
* @param string $name The property name for which to the the value. | |
* @param mixed $value The value of the property. | |
* | |
* @return void | |
*/ | |
public function __set($name, $value) | |
{ | |
switch ($name) | |
{ | |
case 'folder': | |
$this->folder = (string) $value; | |
break; | |
case 'extensiontype': | |
$this->extensionType = (string) $value; | |
break; | |
case 'clientId': | |
$this->clientId = (int) $value; | |
break; | |
default: | |
parent::__set($name, $value); | |
} | |
} | |
/** | |
* Method to attach a JForm object to the field. | |
* | |
* @param SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object. | |
* @param mixed $value The form field value to validate. | |
* @param string $group The field name group control value. This acts as as an array container for the field. | |
* For example if the field has name="foo" and the group value is set to "bar" then the | |
* full field name would end up being "bar[foo]". | |
* | |
* @return boolean True on success. | |
* | |
* @see JFormField::setup() | |
*/ | |
public function setup(SimpleXMLElement $element, $value, $group = null) | |
{ | |
$return = parent::setup($element, $value, $group); | |
if ($return) | |
{ | |
$this->folder = (string) $this->element['folder']; | |
$this->extensionType = $this->element['extensiontype'] ? (string) $this->element['extensiontype'] : 'component'; | |
$this->clientId = $this->element['clientid'] ? (int) $this->element['clientid'] : 1; | |
// Force client id to be 0 for plugins and libraries | |
if ($this->extensiontype === 'plugin' || $this->extensiontype == 'library') | |
{ | |
$this->clientId = 0; | |
} | |
} | |
return $return; | |
} | |
/** | |
* Method to get a list of options for a list input. | |
* | |
* @return array An array of JHtml options. | |
*/ | |
protected function getOptions() | |
{ | |
$folder = $this->folder; | |
$type = $this->extensionType; | |
$clientId = $this->clientId; | |
$options = array(); | |
if (!empty($type)) | |
{ | |
// Get list of plugins | |
$db = JFactory::getDbo(); | |
$query = $db->getQuery(true) | |
->select('element AS value, name AS text, folder') | |
->from('#__extensions') | |
->where('enabled = 1') | |
->order('ordering, name'); | |
if (strlen($type) > 0) | |
{ | |
$query->where('type = ' . $db->quote($type)); | |
} | |
if ($type === 'plugin' && !empty($folder)) | |
{ | |
$query->where('folder = ' . $db->quote($folder)); | |
} | |
if (in_array($clientId, array(0, 1))) | |
{ | |
$query->where('client_id = ' . $db->quote($clientId)); | |
} | |
$db->setQuery($query); | |
$options = $db->loadObjectList(); | |
$lang = JFactory::getLanguage(); | |
foreach ($options as $i => $item) | |
{ | |
if ($type === 'plugin') | |
{ | |
$extension = 'plg_' . $item->folder . '_' . $item->value; | |
} | |
else | |
{ | |
$extension = $item->text; | |
} | |
if ($type === 'plugin') | |
{ | |
$source = JPATH_PLUGINS . '/' . $item->folder . '/' . $item->value; | |
$lang->load($extension . '.sys', JPATH_ADMINISTRATOR, null, false, true) || $lang->load($extension, $source, null, false, true); | |
} | |
else | |
{ | |
$lang->load($extension . '.sys', $clientId === 1 ? JPATH_ADMINISTRATOR : JPATH_SITE, null, false, true) || $lang->load($extension . '.sys', JPATH_ADMINISTRATOR, null, false, true); | |
} | |
$options[$i]->text = JText::_($item->text); | |
} | |
} | |
else | |
{ | |
JLog::add(JText::_('No extensions found.'), JLog::WARNING, 'jerror'); | |
} | |
// Merge any additional options in the XML definition. | |
$options = array_merge(parent::getOptions(), (array) $options); | |
return $options; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment