2

I'm building a module that will read parameters from the current page's K2 extra fields, and use them to load modules.

I've successfully loaded the current page's item, but it's "extra_fields" array is missing the extra field name. It's structured like this:

extra_fields {
    [0]=> { "id"=>"5", "value"=>"Field 1 Value" }
    [1]=> { "id"=>"6", "value"=>"Field 2 Value" }
    [2]=> { "id"=>"7", "value"=>"Field 3 Value" }
}

The "id" variables match the extra field ids, but I don't know how to access them from my module. I'm guessing it'll involve getting them through the database using their id, but I don't know how to do that.

4
  • It's a bit unclear what you are doing... a module that will load other modules? Why don't you load the modules directly? Also from your question it looks like the data that define which modules to load is kept in the extra fields, why the fields groups are useful? *Groups is the way to assign certain fields to selected categories.
    – FFrewin
    Commented Jul 14, 2016 at 21:20
  • For a few reasons. For one, I need to personalize multiple modules on every single K2 item and doing it through extra fields saves time. Also, it means I can adjust the modules without having to create a menu item for the K2 item. Regarding the field groups, I said it wrong. I meant the extra fields. I need to know the name of the extra field, not just its value.
    – Obscerno
    Commented Jul 15, 2016 at 11:55
  • I've edited the question to correct that, thanks.
    – Obscerno
    Commented Jul 15, 2016 at 12:03
  • Yes the k2 extra fields comes in pairs of id,value. Isn't sufficient to get the value using the id of the field for your module?
    – FFrewin
    Commented Jul 15, 2016 at 12:51

1 Answer 1

2

The extra fields are saved in json encoded format, so first you will need to have:

$arrExtraFields = json_decode($extra_fields);

Once you have that, you will need to loop through the array $arrExtraFields and and then query the table #__k2_extra_fields (e.g. SELECT name FROM #__k2_extra_fields WHERE id='the-current-id';) in order to get the name of the current extra field (by id).

0

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.