Jump to content

Add InputfieldRepeater to module configuration-page


Yannick Albert
 Share

Recommended Posts

How could we add a repeater-field to our module configuration pages? I tried to add it like any other input field, but no success! (Just as a sidenote: Unfortunately my php skills are really restricted)

This is my current state, but it just throws an exception:

Error: Exception: Unknown column 'field_title.count' in 'field list' (in wire\core\Database.php line 118)
public static function getModuleConfigInputfields(array $data) {
 
    $data = array_merge(self::getDefaultData(), $data);
    $inputfields = new InputfieldWrapper();
 
 
    ...
 
 
    $field = wire("fields")->get("title");
    $field->type = $modules->get("FieldtypeRepeater");
 
    $repeater = wire("modules")->get("InputfieldRepeater");
    $repeater->name = "somethingUnique";
    $repeater->add($field);
    $repeater->page = wire("page");
    $inputfields->add($repeater);
 
 
    ...
 
 
    return $inputfields;
}

Any idea what I'm doing wrong, or how we could achive this?

Link to comment
Share on other sites

Are you wanting to add an existing repeater field, or create a new one (including all it's subfields) from scratch and then add that to the module config page. I am guessing the latter, but just wanted to confirm first.

Link to comment
Share on other sites

Are you wanting to add an existing repeater field, or create a new one (including all it's subfields) from scratch and then add that to the module config page. I am guessing the latter, but just wanted to confirm first.

Yeah, you are on the right track. I just asked me how to to create a new repeater, including some fields, via pure API. (And without adding them first to any existing page, except the module-configuration page. Don't if it is already possible?!)

Thanks so far :)

Link to comment
Share on other sites

Ok, well this will hopefully get you started. I haven't seen much on the forum or the docs about doing this. This code is an adaption from what I have just put together to support repeaters in the page tree migrator module. Because the migrator needs to take the IDs from existing repeaters, convert them to names, and then back to IDs, I think what I ended up having to do was probably more complicated than what should be needed in your situation. It worked perfectly for my needs and from some quick testing I think version should work for your needs, but there might be a much simpler way to do it. You will need to adjust the first three lines, and then the lines under both the //Add fields to.. comments. 

This code creates the repeater field, the necessary fieldgroup and template, but you would still need to add the actual repeater field to the required template, but I get the feeling you have that sorted already with the code you posted.

$titlefield = wire("fields")->get("title");
$bodyfield = wire("fields")->get("body");
$repeater_name = "newrepeater";

$f = new Field();
$f->type = $this->modules->get("FieldtypeRepeater");
$f->name = $repeater_name;

$repeater_fg = new Fieldgroup();
$repeater_fg->name = "repeater_$repeater_name";

//Add fields to fieldgroup - add others as necessary
$repeater_fg->append($titlefield);
$repeater_fg->append($bodyfield);

$repeater_fg->save();

$repeater_template = new Template();
$repeater_template->name = "repeater_$repeater_name";
$repeater_template->flags = 8;
$repeater_template->noChildren = 1;
$repeater_template->noParents = 1;
$repeater_template->noGlobal = 1;
$repeater_template->slashUrls = 1;
$repeater_template->fieldgroup = $repeater_fg;

$repeater_template->save();

$repeater_page = "for-field-{$f->id}";
$f->parent_id = $this->pages->get("name=$repeater_page")->id;
$f->template_id = $repeater_template->id;
$f->repeaterReadyItems = 3;

//Add fields to the repeater - add others as necessary
$f->repeaterFields = $titlefield;
$f->repeaterFields = $bodyfield;

$f->save();
  • Like 3
Link to comment
Share on other sites

Ok, well this will hopefully get you started. I haven't seen much on the forum or the docs about doing this. This code is an adaption from what I have just put together to support repeaters in the page tree migrator module. Because the migrator needs to take the IDs from existing repeaters, convert them to names, and then back to IDs, I think what I ended up having to do was probably more complicated than what should be needed in your situation. It worked perfectly for my needs and from some quick testing I think version should work for your needs, but there might be a much simpler way to do it. You will need to adjust the first three lines, and then the lines under both the //Add fields to.. comments. 

This code creates the repeater field, the necessary fieldgroup and template, but you would still need to add the actual repeater field to the required template, but I get the feeling you have that sorted already with the code you posted.

$titlefield = wire("fields")->get("title");
$bodyfield = wire("fields")->get("body");
$repeater_name = "newrepeater";

$f = new Field();
$f->type = $this->modules->get("FieldtypeRepeater");
$f->name = $repeater_name;

$repeater_fg = new Fieldgroup();
$repeater_fg->name = "repeater_$repeater_name";

//Add fields to fieldgroup - add others as necessary
$repeater_fg->append($titlefield);
$repeater_fg->append($bodyfield);

$repeater_fg->save();

$repeater_template = new Template();
$repeater_template->name = "repeater_$repeater_name";
$repeater_template->flags = 8;
$repeater_template->noChildren = 1;
$repeater_template->noParents = 1;
$repeater_template->noGlobal = 1;
$repeater_template->slashUrls = 1;
$repeater_template->fieldgroup = $repeater_fg;

$repeater_template->save();

$repeater_page = "for-field-{$f->id}";
$f->parent_id = $this->pages->get("name=$repeater_page")->id;
$f->template_id = $repeater_template->id;
$f->repeaterReadyItems = 3;

//Add fields to the repeater - add others as necessary
$f->repeaterFields = $titlefield;
$f->repeaterFields = $bodyfield;

$f->save();

Awesome, good stuff!

Seems to be the perfect starting point to work with repeaters to me... I'll take a look deeper in it, later this day. But nevertheless, thanks for the great adaption!

Cheers,

Yannick

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

×
×
  • Create New...