2

I have a working list based on an sql query. Now I would like to add some spacers (not selectable items).

When doing this manually in xml you can add the disabled attr to that item, but how can I do that when filling the list like so:

$cities = array(
    array('value' => 1, 'text' => 'New York'),
    array('value' => 2, 'text' => 'Chicago'),
    array('value' => 3, 'text' => 'San Francisco'),
);

I have tried to add 'disabled' => 'true' to the item array and some different versions of 'attribute' => 'disabled', but none of them worked so far.

EDIT: What it should look like:

<option value="" disabled>---Letters---</option>
<option value="x">X</option>
<option value="y">Y</option>
<option value="z">Z</option>
<option value="" disabled>---Numbers---</option>
<option value="1">1</option>
<option value="2">2</option>
...

In the rendered output, Letters and Numbers should just be a "divider" in the select list that is not selectable - "optgroups" are the right keyword, yes.

Edit 2: Okay I've got it now, partly, with the information from mickmackusa:

public function getOptions() {
    $type = $this->element['context'];
    $component = $this->element['component'] ? $this->element['component'] : 'com_content';
    $context = $component . '.' . $type;

    $jcFields  = FieldsHelper::getFields($context, '*', true); // get custom field names by article id

    $options = array();

    $options[] = JHTML::_('select.optgroup', '---Article---');
    $options[] = JHTML::_( 'select.option', 'nxItemId', '***Item ID***' );

    $options[] = JHTML::_('select.optgroup', '---Customfields---');
    foreach ($jcFields as $jcField){
        $options[] = JHTML::_( 'select.option', $jcField->name, $jcField->title );
    }
    
    return $options;
}

The second optgroup header (---Customfields---) will not be rendered.

4
  • Do you mean to add <optgroup> tags? They aren't selectable. Commented Mar 31, 2021 at 0:24
  • Yes "optgroup" would be the terminus - the list should be "like" in my edit - similar to the Joomla fields with "use global" and "from template"
    – Marco
    Commented Mar 31, 2021 at 7:20
  • If your field class curently extends JFormFieldList, you should try JFormFieldGroupedList instead.
    – Sharky
    Commented Mar 31, 2021 at 8:21
  • @Sharky - That sounds interesting - unfortunately i did not found any docs or examples on how to extend JFormFieldGroupedList how to build the options - can you give me a simple example for two groups? (maybe as response so it could be the solution)
    – Marco
    Commented Mar 31, 2021 at 11:17

1 Answer 1

2

My question could be solved by the comments of mickmackusa and Sharky. I have finally extended the JFormFieldGroupedList as follows:

defined('JPATH_PLATFORM') or die;
JFormHelper::loadFieldClass('groupedlist');
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php'); //load fields helper

class JFormFieldNxCustomfieldListKey extends JFormFieldGroupedList
{
    protected $type = 'nxCustomfieldListKey';

    public function getGroups() {

        $jcArticleFields  = FieldsHelper::getFields('com_content.article', '*', true); // get custom field names by article id
        $jcContactFields  = FieldsHelper::getFields('com_contact.contact', '*', true); // get custom field names by article id

        $groups = [];

        // Add Module Style Field
        $tmp = JText::_('MOD_NX_GROUP_BASIC');
        $groups[$tmp][] = JHtml::_('select.option', '', JText::_('MOD_NX_EXPOSER_DISABLED'));
        $groups[$tmp][] = JHtml::_('select.option', 'nxItemId', 'Item ID');


        // Create one new option object for each available style, grouped by templates
        $groups['Article Fields'] = [];
        $groups['Contact CustomFields'] = [];

        $grp = JText::_('MOD_NX_ARTICLE_FIELDS');
        foreach ($jcArticleFields as $jcField){
            $groups[$grp][] = JHtml::_('select.option', $jcField->name, $jcField->title);
        }

        $grp = JText::_('MOD_NX_CONTACT_FIELDS');
        foreach ($jcContactFields as $jcField){
            $groups[$grp][] = JHtml::_('select.option', $jcField->name, $jcField->title);
        }

        return $groups;
    }
}
6
  • Excellent job resolving your question. May I ask why reset() is called? I don't see that the array pointer was ever advanced, so my feeling is that this line of code does nothing. Do you know otherwise? Also, I would not bother declaring the single-use variable $tmp -- just push the JHtml... value directly into $groups[$grp][]. Commented Mar 31, 2021 at 12:31
  • Yeah its a bit of a "Frankenstein Monster" - indeed it needs to be improved. Basically its based on the chromestyle.php from joomlatools found in github.com/joomlatools/joomlatools-platform/blob/master/lib/…
    – Marco
    Commented Mar 31, 2021 at 13:02
  • 1
    In that case, let's break the bad cycle of copy-pasta and fix what can be sensibly fixed. Commented Mar 31, 2021 at 13:16
  • It never ceases to amaze me how many people don't have a basic understanding of regex. [\s\t] should be \s and [a-z0-9\-\_] should be [\w-]. Underscores don't have special meaning to regex, so they don't need escaping. ...but I digress. Commented Mar 31, 2021 at 13:24
  • hey i was on the way to do that but had first to implement „my code“ into my module. - what do you mean with the regex @mickmackusa?
    – Marco
    Commented Apr 1, 2021 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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