1. Fabrik 3.9 has been released. If you have updated Joomla to 3.9, this is a required update.
    Dismiss Notice

Custom data attribute in database join select options

Discussion in 'Community' started by simon.delmotte, Mar 7, 2019.

  1. simon.delmotte

    simon.delmotte Member

    Level: Community
    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
     
  2. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    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
     
  3. simon.delmotte

    simon.delmotte Member

    Level: Community
    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
     
  4. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    You'll have to query the database yourself. Something like ...

    Code (Text):

    <?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
     
  5. cheesegrits

    cheesegrits Support Gopher Staff Member

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

    Code (Text):

    <?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
     
  6. simon.delmotte

    simon.delmotte Member

    Level: Community
    Hello,

    I've tested and it is working great.

    Thank you very much for your support.

    Best regards
     

Share This Page