- md5
- fbc262e54e7dc29edc67fec1945e00d2
- sha1
- b96b007eb9f93605b26c5df920639afae0e9a658
- sha256
- f57dae5dfc65e7ee57e00252c72e9c5f7edd19a44d1e0d7622053147f72745f8
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
<?php
/**
* Joomla! Content Management System
*
* @copyright (C) 2010 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Form\Field;
\defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Form\FormField;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
/**
* Form Field class for the Joomla Platform.
* Provides a grouped list select field.
*
* @since 1.7.0
*/
class GroupedlistField extends FormField
{
/**
* The form field type.
*
* @var string
* @since 1.7.0
*/
protected $type = 'Groupedlist';
/**
* Name of the layout being used to render the field
*
* @var string
* @since 4.0.0
*/
protected $layout = 'joomla.form.field.groupedlist';
/**
* Method to get the field option groups.
*
* @return array The field option objects as a nested array in groups.
*
* @since 1.7.0
* @throws \UnexpectedValueException
*/
protected function getGroups()
{
$groups = array();
$label = 0;
foreach ($this->element->children() as $element)
{
switch ($element->getName())
{
// The element is an <option />
case 'option':
// Initialize the group if necessary.
if (!isset($groups[$label]))
{
$groups[$label] = array();
}
$disabled = (string) $element['disabled'];
$disabled = ($disabled === 'true' || $disabled === 'disabled' || $disabled === '1');
// Create a new option object based on the <option /> element.
$tmp = HTMLHelper::_(
'select.option', ($element['value']) ? (string) $element['value'] : trim((string) $element),
Text::alt(trim((string) $element), preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname)), 'value', 'text',
$disabled
);
// Set some option attributes.
$tmp->class = (string) $element['class'];
// Set some JavaScript option attributes.
$tmp->onclick = (string) $element['onclick'];
// Add the option.
$groups[$label][] = $tmp;
break;
// The element is a <group />
case 'group':
// Get the group label.
if ($groupLabel = (string) $element['label'])
{
$label = Text::_($groupLabel);
}
// Initialize the group if necessary.
if (!isset($groups[$label]))
{
$groups[$label] = array();
}
// Iterate through the children and build an array of options.
foreach ($element->children() as $option)
{
// Only add <option /> elements.
if ($option->getName() !== 'option')
{
continue;
}
$disabled = (string) $option['disabled'];
$disabled = ($disabled === 'true' || $disabled === 'disabled' || $disabled === '1');
// Create a new option object based on the <option /> element.
$tmp = HTMLHelper::_(
'select.option', ($option['value']) ? (string) $option['value'] : Text::_(trim((string) $option)),
Text::_(trim((string) $option)), 'value', 'text', $disabled
);
// Set some option attributes.
$tmp->class = (string) $option['class'];
// Set some JavaScript option attributes.
$tmp->onclick = (string) $option['onclick'];
// Add the option.
$groups[$label][] = $tmp;
}
if ($groupLabel)
{
$label = \count($groups);
}
break;
// Unknown element type.
default:
throw new \UnexpectedValueException(sprintf('Unsupported element %s in GroupedlistField', $element->getName()), 500);
}
}
reset($groups);
return $groups;
}
/**
* Method to get the field input markup fora grouped list.
* Multiselect is enabled by using the multiple attribute.
*
* @return string The field input markup.
*
* @since 1.7.0
*/
protected function getInput()
{
$data = $this->getLayoutData();
// Get the field groups.
$data['groups'] = (array) $this->getGroups();
return $this->getRenderer($this->layout)->render($data);
}
}
The same content is in 49 releases, Joomla 4.0.0-rc1 to 4.2.0-beta2. See them all, or every version of this file.