Skip to content

Commit

Permalink
[com_fields] Add base list plugin class which activates the list plug…
Browse files Browse the repository at this point in the history
…in (joomla#13546)

* Add base list plugin class

* Update fieldslistplugin.php
  • Loading branch information
laoneo authored and rdeutz committed Jan 16, 2017
1 parent 0bc385d commit e8043e8
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 72 deletions.
82 changes: 82 additions & 0 deletions administrator/components/com_fields/libraries/fieldslistplugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* @package Joomla.Administrator
* @subpackage com_fields
*
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;

JLoader::import('components.com_fields.libraries.fieldsplugin', JPATH_ADMINISTRATOR);

/**
* Base plugin for all list based plugins
*
* @since __DEPLOY_VERSION__
*/
class FieldsListPlugin extends FieldsPlugin
{
/**
* Transforms the field into an XML element and appends it as child on the given parent. This
* is the default implementation of a field. Form fields which do support to be transformed into
* an XML Element mut implemet the JFormDomfieldinterface.
*
* @param stdClass $field The field.
* @param DOMElement $parent The field node parent.
* @param JForm $form The form.
*
* @return DOMElement
*
* @since __DEPLOY_VERSION__
*/
public function onCustomFieldsPrepareDom($field, DOMElement $parent, JForm $form)
{
$fieldNode = parent::onCustomFieldsPrepareDom($field, $parent, $form);

if (!$fieldNode)
{
return $fieldNode;
}

foreach ($this->getOptionsFromField($field) as $value => $name)
{
$option = new DOMElement('option', $value);
$option->nodeValue = JText::_($name);

$element = $fieldNode->appendChild($option);
$element->setAttribute('value', $value);
}

return $fieldNode;
}

/**
* Returns an array of key values to put in a list from the given field.
*
* @param stdClass $field The field.
*
* @return array
*
* @since __DEPLOY_VERSION__
*/
public function getOptionsFromField($field)
{
$data = array();

// Fetch the options from the plugin
foreach ($this->params->get('options', array()) as $option)
{
$op = (object) $option;
$data[$op->value] = $op->name;
}

// Fetch the options from the field
foreach ($field->fieldparams->get('options', array()) as $option)
{
$data[$option->value] = $option->name;
}

return $data;
}
}
4 changes: 2 additions & 2 deletions plugins/fields/checkboxes/checkboxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

defined('_JEXEC') or die;

JLoader::import('fields.list.list', JPATH_PLUGINS);
JLoader::import('components.com_fields.libraries.fieldslistplugin', JPATH_ADMINISTRATOR);

/**
* Fields Checkboxes Plugin
*
* @since __DEPLOY_VERSION__
*/
class PlgFieldsCheckboxes extends PlgFieldsList
class PlgFieldsCheckboxes extends FieldsListPlugin
{
}
4 changes: 2 additions & 2 deletions plugins/fields/imagelist/imagelist.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

defined('_JEXEC') or die;

JLoader::import('fields.list.list', JPATH_PLUGINS);
JLoader::import('components.com_fields.libraries.fieldslistplugin', JPATH_ADMINISTRATOR);

/**
* Fields Imagelist Plugin
*
* @since __DEPLOY_VERSION__
*/
class PlgFieldsImagelist extends PlgFieldsList
class PlgFieldsImagelist extends FieldsListPlugin
{
/**
* Transforms the field into an XML element and appends it as child on the given parent. This
Expand Down
66 changes: 2 additions & 64 deletions plugins/fields/list/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,75 +9,13 @@

defined('_JEXEC') or die;

JLoader::import('components.com_fields.libraries.fieldsplugin', JPATH_ADMINISTRATOR);
JLoader::import('components.com_fields.libraries.fieldslistplugin', JPATH_ADMINISTRATOR);

/**
* Fields list Plugin
*
* @since __DEPLOY_VERSION__
*/
class PlgFieldsList extends FieldsPlugin
class PlgFieldsList extends FieldsListPlugin
{
/**
* Transforms the field into an XML element and appends it as child on the given parent. This
* is the default implementation of a field. Form fields which do support to be transformed into
* an XML Element mut implemet the JFormDomfieldinterface.
*
* @param stdClass $field The field.
* @param DOMElement $parent The field node parent.
* @param JForm $form The form.
*
* @return DOMElement
*
* @since 3.7.0
*/
public function onCustomFieldsPrepareDom($field, DOMElement $parent, JForm $form)
{
$fieldNode = parent::onCustomFieldsPrepareDom($field, $parent, $form);

if (!$fieldNode)
{
return $fieldNode;
}

foreach ($this->getOptionsFromField($field) as $value => $name)
{
$option = new DOMElement('option', $value);
$option->nodeValue = JText::_($name);

$element = $fieldNode->appendChild($option);
$element->setAttribute('value', $value);
}

return $fieldNode;
}

/**
* Returns an array of key values to put in a list from the given field.
*
* @param stdClass $field The field.
*
* @return array
*
* @since 3.7.0
*/
public function getOptionsFromField($field)
{
$data = array();

// Fetch the options from the plugin
foreach ($this->params->get('options', array()) as $option)
{
$op = (object) $option;
$data[$op->value] = $op->name;
}

// Fetch the options from the field
foreach ($field->fieldparams->get('options', array()) as $option)
{
$data[$option->value] = $option->name;
}

return $data;
}
}
4 changes: 2 additions & 2 deletions plugins/fields/radio/radio.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

defined('_JEXEC') or die;

JLoader::import('fields.list.list', JPATH_PLUGINS);
JLoader::import('components.com_fields.libraries.fieldslistplugin', JPATH_ADMINISTRATOR);

/**
* Fields Radio Plugin
*
* @since __DEPLOY_VERSION__
*/
class PlgFieldsRadio extends PlgFieldsList
class PlgFieldsRadio extends FieldsListPlugin
{
}
4 changes: 2 additions & 2 deletions plugins/fields/sql/sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

defined('_JEXEC') or die;

JLoader::import('fields.list.list', JPATH_PLUGINS);
JLoader::import('components.com_fields.libraries.fieldslistplugin', JPATH_ADMINISTRATOR);

/**
* Fields Sql Plugin
*
* @since __DEPLOY_VERSION__
*/
class PlgFieldsSql extends PlgFieldsList
class PlgFieldsSql extends FieldsListPlugin
{
/**
* Transforms the field into an XML element and appends it as child on the given parent. This
Expand Down

0 comments on commit e8043e8

Please sign in to comment.