سؤال

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?

هل كانت مفيدة؟

المحلول

Thanks, I found the solution. If someone has the same problem:
I get the id of prodcategorie from URL with

 $prodcategorieid=JRequest::getInt('id');

After that I query the database once again in models/fields/language.php (btw I changed the name in order to not get confused with standard Language field), select the language for this prodcategorie and make it selected with that line:

return JHTML::_('select.genericlist', $options, $name=$this->name, $attribs = null, $key='value', $text='text', $selected = $langid[0]->id);

$langid is the result of db query.
It's surely not the perferct solution, but it works for me.

نصائح أخرى

You should possible think about using the standard Languages field:

http://docs.joomla.org/Language_form_field_type

You should probably also think about using the Joomla Component Creator. It creates the code for you, so you do not end up wasting so much time on little typical issues like this and can focus on developing the things that makes your component unique.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top