Гость
08 Фев 2013 05:55

Hello Cobalt team :)

I had a need for one of my module, to allow user to show only records from a cobalt category.

Then I develop my own version of the common cobalt records module, still using the nice API provided by the component, to allow user to set also the category id.

To do this, I really had to have a look on the form fields definitions you already define in /libraries/mint/forms/fields/, to find what I can use to do it...

And then I understand that only the 'meresourcescattree' definition was almost what I need, but not really finished and still buggy.

I just finish to modify it, to made one witch is able to show all the Cobalt categories, grouped by section. It is not dependent on the section field, because no JS was added, but it's working just how I need.

Can you please have a look on the file I let, and maybe consider to add this one to your fields declaration for future evolutions and third party development?

I hope it could be useful for others ;)

Christophe


Here is the code:

<?php

/**

 * Cobalt Categories field list

 * @author        Christophe CROSAZ - Abstrakt Graphics

 * @author        Этот адрес электронной почты защищен от спам-ботов. У вас должен быть включен JavaScript для просмотра.

 * 

 * Cobalt by MintJoomla

 * a component for Joomla! 1.7 - 2.5 CMS ( http://www.joomla.org )

 * Author Website:  http://www.mintjoomla.com/ 

 * @copyright    Copyright (C) 2012 MintJoomla ( http://www.mintjoomla.com ). All rights reserved.

 * @license        GNU/GPL  http://www.gnu.org/copyleft/gpl.html 

 */

defined('JPATH_PLATFORM') or die;

jimport('joomla.html.html');

jimport('joomla.form.formfield');

jimport('joomla.form.helper');

JFormHelper::loadFieldClass('list');

/**

 * Form Field class for the Joomla Framework.

 * @since        1.6

 */

class JFormFieldCcategories extends JFormFieldList

{

    /**

     * The form field type.

     *

     * @var        string

     * @since    1.6

     */

    protected $type = 'Ccategories';



    /**

     * Method to get the field options.

     *

     * @return    array    The field option objects.

     * @since    1.6

     */

    protected function getOptions()

    {

        // Initialise variables.

        $options = array();



        $db        = JFactory::getDbo();

        $query    = $db->getQuery(true);



        $options = array();



        $query->select('id, name as text');

        $query->from('#__js_res_sections');

        $db->setQuery($query);

        $sections = $db->loadObjectList();

//        echo '<pre>';print_r(parent::getOptions());echo '</pre>';

        foreach ( $sections as $section )

        {

            //Looking for sub categories:

            $query            = $db->getQuery(true);

            // Prevent parenting to children of this item.

            $query->select('id as value, title as text, level');

            $query->from('#__js_res_categories');

            $query->where('section_id = ' . $section->id);

            $query->where('published = 1');

            $query->order('lft ASC');

            $db->setQuery($query);

            $categories        = $db->loadObjectList();



            if (count($categories)) {

                // Pad the option text with spaces using depth level as a multiplier.

                for ( $i = 0, $n = count($categories); $i < $n; $i ++ )

                {

                    $categories[$i]->text = str_repeat('- ', $categories[$i]->level) . $categories[$i]->text;

                }

                //Add this part to the selection

                $options[$section->text]    = $categories;

            }

        }



        // Merge any additional options in the XML definition.

        $options = array_merge(array(parent::getOptions()), $options);



        return $options;

    }

    /**

     * 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   11.1

     */

    protected function getInput()

    {

        // Initialize variables.

        $html = array();

        $attr = '';

        // Initialize some field attributes.

        $attr .= $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';

        $attr .= ((string) $this->element['disabled'] == 'true') ? ' disabled="disabled"' : '';

        $attr .= $this->element['size'] ? ' size="' . (int) $this->element['size'] . '"' : '';

        $attr .= $this->multiple ? ' multiple="multiple"' : '';

        // Initialize JavaScript field attributes.

        $attr .= $this->element['onchange'] ? ' onchange="' . (string) $this->element['onchange'] . '"' : '';

        // Get the field groups.

        $groups = (array) $this->getOptions();

        // Create a read-only list (no name) with a hidden input to store the value.

        if ((string) $this->element['readonly'] == 'true')

        {

            $html[] = JHtml::_(

                'select.groupedlist', $groups, null,

                array(

                    'list.attr' => $attr, 'id' => $this->id, 'list.select' => $this->value, 'group.items' => null, 'option.key.toHtml' => false,

                    'option.text.toHtml' => false

                )

            );

            $html[] = '<input type="hidden" name="' . $this->name . '" value="' . $this->value . '"/>';

        }

        // Create a regular list.

        else

        {

            $html[] = JHtml::_(

                'select.groupedlist', $groups, $this->name,

                array(

                    'list.attr' => $attr, 'id' => $this->id, 'list.select' => $this->value, 'group.items' => null, 'option.key.toHtml' => false,

                    'option.text.toHtml' => false

                )

            );

        }

        return implode($html);

    }

}

Последние изменения: 02 Март 2014


Sergey
Total posts: 13,748
09 Фев 2013 07:27

Added to Cobalt 8 and 7


Sergey
Total posts: 13,748
09 Фев 2013 07:28

And Chris, if you need anything else to be added to Cobalt core to make your wonderful extensions, just tell me.

Работает на Cobalt