4

I use the field type "repeatable" in a Joomla module I've created but I don't know how to get the values out of it and use it in the module's default.php to create the output.

My repeatable field:

                    <field name="mitarbeiter"
                    type="Repeatable"
                    icon="list"
                    description="Mitarbeiter eingeben"
                    label="Mitarbeiter eingeben"
                    <fields name="params">
                        <fieldset hidden="true" name="mitarbeiter_template_modal" repeat="true">

                        <field name="mitarbeitername"
                            label="Name"
                            size="30"
                            type="text" />

                        <field name="mitarbeiterfunktion"
                            label="Funktion"
                            size="30"
                            type="text" />

                        <field name="mitarbeitertelefonnummer"
                            label="Funktion"
                            size="30"
                            type="text" />

                        <field name="mitarbeiteremail"
                            label="Funktion"
                            size="30"
                            type="text" />

                        <field name="mitarbeiterbild"
                            label="Bild-URL"
                            size="30"
                            type="text" />

                        </fieldset>
                    </fields>
                </field>

Normally I get the value of a field in the mod_modulename.php:

$fieldvalue = $parameters->get('fieldname');
4

I was hoping someone would eventually ask this question on here. I wrote a function for my first usage of repeatable form fields and use it for every extension I make now.

Here is how you do it:

Firstly, get the json object for the parameter like so:

$mitarbeiter = $params->get('mitarbeiter');

Then decode the json object:

$json = json_decode($mitarbeiter, true);

Then can then group each set of fields by key, like so:

public function group_by_key($array) 
{
    $result = array();

    foreach ($array as $sub) 
    {
        foreach ($sub as $k => $v) 
        {
            $result[$k][] = $v;
        }
    }
    return $result;
}

$filtered_array = group_by_key($json);

And finally, you'll need to iterate though it using a foreach loop, like so:

foreach ($filtered_array as $index=>$value) 
{   
    echo $value[0] . '<br>';   // mitarbeitername
    echo $value[1] . '<br>';   // mitarbeiterfunktion
    echo $value[2] . '<br>';   // mitarbeitertelefonnummer
    echo $value[3] . '<br>';   // mitarbeiteremail
    echo $value[4] . '<br>';   // mitarbeiterbild
}

Hope this helps

  • Hi! Thanks for your help! Youre solution looks pretty good. Unfortunately im not a php programmer so ive got a problem. I put the code in my mod_module.php and the foeach loop in the tmlp/default.php. Now the page is not working anymore, its showing a problem on line 29 which is the line the public function starts. Any idea what im doing wrong? Thx for your help. – Peesen87 Apr 30 '15 at 8:29
  • Could you specify the "problem" please? If any errors are being shown, please let me know – Lodder Apr 30 '15 at 8:41
  • Error message: Parse error: syntax error, unexpected 'public' (T_PUBLIC) I put the code inside my mod_modulname.php – Peesen87 Apr 30 '15 at 10:15
  • Try changing public function to just function – Lodder Apr 30 '15 at 10:16
  • @Peesen87 - You're more than welcome. Could you please accept the answer to mark it as solved by clicking the "tick" icon. – Lodder Apr 30 '15 at 10:19
4

The above answer from @Lodder is correct, but you will also need to wrap the function with

if (!function_exists('yourFunctionname')) {
    // ... proceed to declare your function
}

If you do not, you can only render this function once per page.

So altogether it should look like this:

$mitarbeiter = $params->get('mitarbeiter');
$json = json_decode($mitarbeiter, true);

if (!function_exists('group_by_key')) {
  public function group_by_key($array) 
  {
      $result = array();

      foreach ($array as $sub) 
      {
          foreach ($sub as $k => $v) 
          {
              $result[$k][] = $v;
          }
      }
      return $result;
  }
}
$filtered_array = group_by_key($json);

foreach ($filtered_array as $index=>$value) 
{   
    echo $value[0] . '<br>';   // mitarbeitername
    echo $value[1] . '<br>';   // mitarbeiterfunktion
    echo $value[2] . '<br>';   // mitarbeitertelefonnummer
    echo $value[3] . '<br>';   // mitarbeiteremail
    echo $value[4] . '<br>';   // mitarbeiterbild
}
  • Isn't it that part = if (!function_exists('yourFunctionname')) { public function group_by_key($array) should be = if (!function_exists('group_by_key')) { public function group_by_key($array) – webchun Jul 31 '15 at 3:22
  • Yes, good catch , I will edit – NicholasByDesign Jul 31 '15 at 3:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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