I'm new to Joomla and PHP development. I created 2 database tables - prodcategories and languages:
CREATE TABLE #__bbb_languages (
id INTEGER AUTO_INCREMENT not null,
language VARCHAR(2),
CONSTRAINT pk_#__bbb_languages PRIMARY KEY (id)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO #__bbb_languages (language)
VALUES ('de'), ('fr');
CREATE TABLE #__bbb_prodcategories (
id INTEGER AUTO_INCREMENT not null,
name VARCHAR(255),
language INTEGER,
FOREIGN KEY (language) REFERENCES #__bbb_languages(id),
CONSTRAINT pk_#__bbb_prodcategories PRIMARY KEY (id)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO #__bbb_prodcategories (name, language)
VALUES ('IP-Produkte', 1), ('IP-Produits', 2);
I'm going through this http://library.logicsistemi.it/en/joomla/developing-joomla-25-mvc-components/30-developing-mvc-components-joomla25-part8 tutorial and am now at the point of adding and editing records. I created a custom field Language:
models/fields/language.php
<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');
jimport('joomla.form.formfield');
JFormHelper::addFieldPath(JPATH_COMPONENT . '/models/fields');
class JFormFieldLanguage extends JFormField {
protected $type = 'Language';
public function getInput() {
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('id, language');
$query->from('#__bbb_languages');
$db->setQuery((string)$query);
$messages = $db->loadObjectList();
$options[] = JHTML::_('select.option','',JText::_('Please choose a language'));
foreach($messages as $message) {
$options[] = JHtml::_('select.option', $message->id, $message->language);
}
return JHTML::_('select.genericlist', $options, $name=$this->name, $attribs = null, $key='value', $text='text');
}
}
models/forms/prodcategorie.xml
<?xml version="1.0" encoding="UTF-8"?>
<form>
<fieldset addfieldpath="/administrator/components/com_bbb/models/fields">
<field
name="id"
type="hidden" />
<field
name="name"
type="text"
label="NAME"
size="40"
class="inputbox" />
<field name="language"
type="Language"
label="Language"
required="true"/>
</fieldset>
</form>
models/prodcategorie.php
<?php
defined('_JEXEC') or die();
jimport( 'joomla.application.component.modeladmin' );
class BbbModelProdcategorie extends JModelAdmin
{
public function getForm($data = array(), $loadData = true)
{
// Get the form
$form = $this->loadForm('com_bbb.prodcategorie', 'prodcategorie',
array('control' => 'jform', 'load_data' => $loadData));
if (!$form) {
return false;
} else {
return $form;
}
}
public function loadFormData()
{
// Load form data
$data = $this->getItem();
return $data;
}
}
views/prodcategorie/tmpl/edit.php
<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
$option = JRequest::getCmd('option');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');
?>
<form action="index.php" method="post" name="adminForm" id="prodcategorie-admin-form" class="form-validate">
<input type="hidden" name="option" value="<?=$option?>" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="id" value="<?=$this->item->id?>" />
<?php echo JHtml::_('form.token'); ?>
<fieldset class="adminform">
<legend><?=JText::_( 'DETAILS' ); ?></legend>
<ul class="adminformlist">
<? foreach ($this->form->getFieldset() as $field) { ?>
<li><?=$field->label?><?=$field->input?></li>
<? } ?>
</ul>
</fieldset>
</form>
The most part of the code is just copy/paste from the tutorial with changed names. I only added the part with custom field myself. My problem is that when I want to edit a prodcategorie, the form shows up the Language field with all options (Please choose a language/de/fr), but I need that the correct language of the prodcategorie is preselected. E.g. if I open the 'IP-Produits' for editing, the 'fr' in Language is preselected in . How to achive this?