Jump to content

Repeaterfield created through API doesn't show properly in CMS


arjen
 Share

Recommended Posts

Hi,

I'm working on some automation of repetitive tasks. I'm creating a module which creates a couple of fields and a repeater to hold those fields. I'm running into something I don't understand.

This is my code:

<?php

// Create an array to store the repeaterField data
$fields = array();

// Create the body field
$f = new Field();
$f->type = wire()->modules->get("FieldtypeText");
$f->name = "subject";
$f->save();

// Add the field to the array
array_push($fields, $f->id);

// Create the body field
$f = new Field();
$f->type = wire()->modules->get("FieldtypeText");
$f->name = "author";
$f->save();

// Add the field to the array
array_push($fields, $f->id);

// Create a repeater field
$f = new Field();
$f->type = wire()->modules->get("FieldtypeRepeater");
$f->name = "quotes";
$f->repeaterFields = $fields; // Save the array as repeaterFields
$f->save();

So far, so good. All fields get made and are available in the CMS. However, when looking in the CMS the $fields array seems empty, because there seems to be no fields assigned (see screenshot below).

post-365-0-90133300-1373286424_thumb.png

However when checking directly in the API using:

print_r(wire()->fields->get('repeater')->repeaterFields);

It does display the original values (the id of the subject and the id of the author).

When editing the repeater field through the CMS and assigning a field it overwrites the repeaterFields value. From that point on everything works as expected.

There has to be a step I'm missing. Can anyone help me out? Many thanks in advance.

Link to comment
Share on other sites

What is this?

$f->repeaterFields = $repeaterFields;

Where does $repeaterFields come from?

Also shouldn't it be something like:

$f->repeaterFields->add($field);
Link to comment
Share on other sites

Thanks soma! That was a typo. It should be $fields. I updated the code.

Regarding the:

$f->repeaterFields->add($field);

I'm not sure I'm following you. How can this be converted to hold multiple fields?

When trying to add one $f (field) I'm getting an error: Fatal error: Call to a member function add() on a non-object.

Edited by arjen
Link to comment
Share on other sites

I'm not sure about repeaters API to create a new fields and repeater. 

repeaterFields doesn't seem really the way to add fields. This is only used on the field config UI to add and remove fields. So it exists but it's processed through a POST.

Repeaters use a template/fieldgroup where the fields are added. So a way to add fields I found by trying is after saving the repeater you get the template used by this repeater with the prefix "repeater" and the fieldname 

$t = $fieldgroups->get("repeater_quotes");
$t->add("title");
$t->save();
  • Like 2
Link to comment
Share on other sites

  • 3 months later...

Hey arjen,

I don't suppose you solved this did you - I have having the same issue - looking through the DB tables I am yet to find anything obviously different from a repeater created via the admin vs one created via the API - will keep looking, but am hoping you might already have the answer :)

EDIT: always the way - as soon as you post, you figure it out :) - the entries are missing from the fieldgroups_fields table. Hopefully should be easy to sort out!

Link to comment
Share on other sites

I spend another few hours, but didn't really hit the nail. Coincidentally I was looking into this today again since it is still on my todo list. Will post an update in the next few days.

Link to comment
Share on other sites

  • 4 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...