• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

Custom data attribute in database join select options

Hello,

Is it possible to add data attributes in the option tags of a databasejoin render as a select element?

I would like to have something like:
<option value="fabrik_element_id" data-something="some_fabrik_value">fabrik_element_label</option>

Thank you
 
Not built in, no.

I think the only way to do it would be to override the layout ...

plugins/fabrik_element/databasejoin/layouts/fabrik-element-databasejoin-form-dropdownlist.php

... (see wiki for where to put layout overrides), and replace the use of JHTML::_('select.genericlist', ...) with your own code to build the select markup. The built in JTHML::_() won't do it, it doesn't allow for things like data attributes on individual options. At least, as far as I know. It seems to restrict what options you can specify.

-- hugh
 
Hello Hugh,

Thank you for your reply.

I could manage to override the layout of the databasejoin select element with the following code (please let me know is it could be better):

<?php
defined('JPATH_BASE') or die;
$d = $displayData;
?>
<select <?php echo $d->attributes; ?>>
<?php foreach ($d->options as $option) : ?>
<option value="<?php echo $option->value; ?>"><?php echo $option->text; ?></option>
<?php endforeach; ?>
</select>

Now I would like to add my custom attribute.

I would like the value of this attribute is the value of a third element from the source table of the databasejoin (a yes/no element).
In other words, in the databasejoin element I'm getting the id and the label from the source table. But how can I get the value of another element?

Best regards
 
You'll have to query the database yourself. Something like ...

Code:
<?php
defined('JPATH_BASE') or die;
$d = $displayData;
$db = JFactory::getDbo();

// build a query to load row data from joined table
$query = $db->getQuery(true);
$query->select('id, your_other_field')->from('your_join_table');
$db->setQuery($query);
$results = $db->loadObjectList('id');

?>
<select <?php echo $d->attributes; ?>>
<?php foreach ($d->options as $option) : ?>
<option data-something="<?php echo $results[$option->value]->your_other_field; ?>" value="<?php echo $option->value; ?>"><?php echo $option->text; ?></option>
<?php endforeach; ?>
</select>

In other words, select id and your_other_field of all rows in the joined table into an array (keyed by the id), then use $option->value to reference that array to get the your_other_field value, with $results[$option->value]->your_other_field.

This assumes you are using 'id' as the value (FK) for the join.

If you are applying any WHERE clause to your join element, you would want to apply the same where() to this query.

-- hugh
 
Oh, and if you are using a "please select" with a value of (say) 0, you'll need to account for that ...

Code:
<?php foreach ($d->options as $option) : ?>
<?php if ($option->value != '0'): ?>
<option data-something="<?php echo $results[$option->value]->your_other_field; ?>" value="<?php echo $option->value; ?>"><?php echo $option->text; ?></option>
<?php else :?>
<option data-something="0" value="<?php echo $option->value; ?>"><?php echo $option->text; ?></option>
<?php endif; ?>
<?php endforeach; ?>

... to avoid an error with a null array reference. Obviously you can set the data-something to whatever you want for that case, or leave it out entirely.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top