Skip to content

Commit

Permalink
Category field extends ModalSelectField
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedik committed Nov 6, 2023
1 parent bb8e527 commit f65f7d5
Showing 1 changed file with 98 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
namespace Joomla\Component\Categories\Administrator\Field\Modal;

use Joomla\CMS\Factory;
use Joomla\CMS\Form\FormField;
use Joomla\CMS\Form\Field\ModalSelectField;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\LanguageHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Layout\FileLayout;
use Joomla\CMS\Session\Session;
use Joomla\Database\ParameterType;

Expand All @@ -27,7 +28,7 @@
*
* @since 3.1
*/
class CategoryField extends FormField
class CategoryField extends ModalSelectField
{
/**
* The form field type.
Expand All @@ -37,14 +38,43 @@ class CategoryField extends FormField
*/
protected $type = 'Modal_Category';

/**
* Method to attach a Form 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.
*
* @return boolean True on success.
*
* @see FormField::setup()
* @since __DEPLOY_VERSION__
*/
public function setup(\SimpleXMLElement $element, $value, $group = null)
{
// Check if the value consist with id:alias, extract the id only
if ($value && str_contains($value, ':')) {
[$id] = explode(':', $value, 2);
$value = (int) $id;
}

$result = parent::setup($element, $value, $group);

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

return $result;
}

/**
* Method to get the field input markup.
*
* @return string The field input markup.
*
* @since 1.6
*/
protected function getInput()
protected function getInput1()
{
if ($this->element['extension']) {
$extension = (string) $this->element['extension'];
Expand Down Expand Up @@ -300,8 +330,72 @@ protected function getInput()
*
* @since 3.7.0
*/
protected function getLabel()
protected function getLabel1()
{
return str_replace($this->id, $this->id . '_name', parent::getLabel());
}

/**
* Method to retrieve the title of selected item.
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
protected function getValueTitle()
{
$value = (int) $this->value ?: '';
$title = '';

if ($value) {
try {
$db = $this->getDatabase();
$query = $db->getQuery(true)
->select($db->quoteName('title'))
->from($db->quoteName('#__categories'))
->where($db->quoteName('id') . ' = :value')
->bind(':value', $value, ParameterType::INTEGER);
$db->setQuery($query);

$title = $db->loadResult();
} catch (\Throwable $e) {
Factory::getApplication()->enqueueMessage($e->getMessage(), 'error');
}
}

return $title ?: $value;
}

/**
* Method to get the data to be passed to the layout for rendering.
*
* @return array
*
* @since __DEPLOY_VERSION__
*/
protected function getLayoutData()
{
$data = parent::getLayoutData();
$data['language'] = (string) $this->element['language'];

return $data;
}

/**
* Get the renderer
*
* @param string $layoutId Id to load
*
* @return FileLayout
*
* @since __DEPLOY_VERSION__
*/
protected function getRenderer($layoutId = 'default')
{
$layout = parent::getRenderer($layoutId);
$layout->setComponent('com_categories');
$layout->setClient(1);

return $layout;
}
}

0 comments on commit f65f7d5

Please sign in to comment.