1

I have used the Joomla Component Creator to make a component and a module.

Now I'm trying to modify the output of the module.

I can use

echo ($element->my_field)

to echo the value of my field in the module. But I cannot figure out how to echo the field label.

In helper.php there is a function to render headers which looks like this:

public static function renderTranslatableHeader(&$params, $field)
    {
        return JText::_(
            'MOD_VECKOMATSEDEL_HEADER_FIELD_' . str_replace('#__', '', strtoupper($params->get('table'))) . '_' . strtoupper($field)
        );
    }

I create this variable in my module

$label = ModVeckomatsedelHelper::renderTranslatableHeader($params, $params->get('field'));

if I then echo $label it echoes the label of the first field in the table.

How can I pass the field name as an argument?

My guess was echo $label->my_field but that's not correct.

7
  • Couldn't find info about Component Creator, but took a look in another module and realized that this works: echo JText::_('MOD_MYMODULE_HEADER_FIELD_MYTABLE_MY_FIELD') Commented Dec 12, 2015 at 10:06
  • The module starts by getting all items $element = ModMyModuleHelper::getItem($params); Printing that array gives both the field label and the field value, but print_r ($element->my_field) will only print the field value. Commented Dec 13, 2015 at 15:49
  • Can you show us the print_r of $element or the getItem($params) source code?
    – Kitase88
    Commented Dec 15, 2015 at 9:34
  • print_r ($element) gives me: stdClass Object ( [id] => 15 [asset_id] => 153 [ordering] => 12 [state] => 1 [checked_out] => 0 [checked_out_time] => 0000-00-00 00:00:00 [created_by] => 36 [mon_lunch] => Good meal) Commented Dec 15, 2015 at 9:50
  • And the function: public static function getItem(&$params) { $db = JFactory::getDbo(); $query = $db->getQuery(true); $vecka = date(W); /* @var $params Joomla\Registry\Registry $query ->select('*') ->from($params->get('item_table')) ->where('vecka = ' . intval($vecka)); $db->setQuery($query); $element = $db->loadObject(); return $element; } Commented Dec 15, 2015 at 9:54

1 Answer 1

2

The renderTranslatableHeaderfunction returns a string based on JText (multilanguage purpose). If I've understood correctly your question, just add the field name to the function's second parameter and it will give you back the translated label from language file.

$label_to_print = ModVeckomatsedelHelper::renderTranslatableHeader($params,"fieldname");
1
  • 1
    That works great. I can understand that you're not sure if you understood my question, I believe I get variables and functions a bit mixed up. Commented Dec 15, 2015 at 19:47

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.