3

I want to make a module that parses the manufacturers from virtuemart, and the user will select which manufacturers he wants to be shown in the index page. Its the first time I try to create a custom field type.

I created my "custom select list" -the values from the list aren't saved somewhere, so I cant obtain it back.

$fmanuf = $params->get('foreignmanuf');

var_dump($fmanuf); ---> returns NULL

XML:

<fieldset name="brand_params">          
    <field name="foreignmanuf" type="Manulist"  label="Foreign Brands" multiple="true"  description="Foreign Cologne Brands" /> 
</fieldset>

manulist.php:

<?php

defined('JPATH_PLATFORM') or die;

class JFormFieldManulist extends JFormField
{

    protected $type = 'Manulist';

    protected function getInput()
    {
    $db= JFactory::getDbo();
    $query = $db->getQuery(true);
    $query->select('*');
    $query->from('#__virtuemart_manufacturers_en_gb'); 
    $db->setQuery($query);
    $options = $db->loadObjectList();
    $longstring= '<select multiple="multiple">';

    $counter=1;
foreach( $options as $row ) {

        $longstring .= '<option value="' . $counter . '">'. $row->mf_name . ' </option>' ; 
        $counter++; 
    }
    $longstring.= '</select>';
    return $longstring;
}
    protected function getOptions()
    {

        $options = array();

        foreach ($this->element->children() as $option)
        {


            if ($option->getName() != 'option')
            {
                continue;
            }

            $tmp = JHtml::_(
                'select.option', (string) $option['value'],
                JText::alt(trim((string) $option), preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname)), 'value', 'text',
                ((string) $option['disabled'] == 'true')
            );


            $tmp->class = (string) $option['class'];

            $tmp->onclick = (string) $option['onclick'];

            $options[] = $tmp;
        }

        reset($options);

        return $options;
    }
}

I only edited the getInput function, should I somehow edit the getOptions() also ??

  • Custom field types when used are automatically saved if integrated properly into a component, which is why using with a module isn't saving properly. Sounds like you could attach an onchange listener to the field or hook into the form submit event to reload page with the selected options. Can you explain more the business logic of what should happen when user selects manufacturer? – Brian Bolli Dec 2 '14 at 1:28
  • Nothing complex, the user just select which manufacturers he want to be shown in the index page.I was thinking of creating a new table and put the ids of manufacturers there, but i think is better find the right way to parse the values because i will it again in future – IseNgaRt Dec 2 '14 at 7:27
  • up for this issue. I am also facing this situation. – Break the Law Dec 16 '14 at 7:10
3

XML file

<fieldset name="brand_params" addfieldpath="/modules/mod_manufacturers/fields" >    
    <field name="foreignmanuf"
           type="Manulist"
           label="Foreign Brands"
           multiple="true"
           description="Foreign Cologne Brands" />`
</fieldset>

Now to create a custom field type:

mod_manufacturers/fields/manulist.php

<?php    
defined('JPATH_PLATFORM') or die;
JFormHelper::loadFieldClass('list');
class JFormFieldManulist extends JFormFieldList
{
    protected $type = 'Manulist';

    protected function getOptions()
    {
        $db= JFactory::getDbo();
        $query = $db->getQuery(true)
                    ->select('*')
                    ->from('#__virtuemart_manufacturers_en_gb'); 
        $db->setQuery($query);
        $items = $db->loadObjectList();

        $options = array();
        if ($items)
        {
            foreach($items as $item)
            {
                $options[] = JHtml::_('select.option', $item->virtuemart_manufacturer_id, $item->mf_name, $item->slug);
            }
        }

        return array_merge(parent::getOptions(), $options);
    }
}

mod_manufacturers.php

$imagemanuf = $params->get('manufimages');

Now all the parameters are stored in $imagemanuf.

In the module, parse them like this:

Note from the editor (mickmackusa), the following code snippet has typographical and logical faults and therefore should not be used by researchers in its current form. I was unable to repair the code without breaching fair editor practices.

tmpl/default.php

for ($i=0;$i<$imagecount;$i++) {
  $id=$imagemanuf[$i];
  $db= JFactory::getDbo();
    $ready = $db->getQuery(true);
    $query="SELECT file_url FROM #__virtuemart_medias  WHERE virtuemart_media_id = (
    SELECT virtuemart_media_id from #__virtuemart_manufacturer_medias where virtuemart_manufacturer_id = $id )";
    $db->setQuery($query);
    $options=$db->loadObjectList();
}
  • I was unable to completely edit your post because it contains broken code and I didn't want to overreach in my duties. Please notice that $ready is not used (and is an inappropriate variable name). MySQL queries should not be executed in a loop because of poor performance which makes it bad practice -- a WHERE virtuemart_manufacturer_id IN () seems suitable. Please build the subquery and query with Joomla query methods. Finally, saving the each iterated result set into $options will keep overwriting the previously saved values and this is simply not right. Please edit when you can. – mickmackusa Jun 10 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.