Jump to content

Repeater/table fields with pre-defined default fields


a-ok
 Share

Recommended Posts

I'm sure I've seen something like this somewhere in either the modules or on one of @ryan's blog posts but I can't seem to find it anywhere.

I current have 6 text fields which are meta details that relate to a project (size, year, location, status etc) – some of them are filled out/used and some aren't depending on the project. On the front-end these fields link up to a label and icon. The client is wanting to add to the list as they see fit (add a label (multi-language), text (multi-language) and icon (image upload)) so my thought would be to add make this into a repeater field – the issue is that most of the original 6 text fields (size, year, location, status etc) are applicable per project so to have to re-add them every time per project to a repeater is a bit tedious. The client also wants to be able to drag/drop the order of these fields so I can't simply create these 6 fixed text fields then add a repeater for extra fields. You see my issue. I'm sure something existed that allowed you to set some pre-defined defaults for a repeater/table then you can add more to them.

Ideally, it would be possible to add more, choose the order they appear in across projects, edit headlines, edit icons, and they would be conditional on the front-end based on whether anything was input in them.

Let me know if I'm missing something obvious or if this just doesn't exist ;)

Thanks!

Link to comment
Share on other sites

You can do this with a hook to Pages:added() in /site/ready.php

$pages->addHookAfter('added', function(HookEvent $event) {
    $page = $event->arguments(0);
    if($page->template->name !== 'project') return; // Only for this template
    // Define your default labels and values
    $defaults = [
        'Size' => '',
        'Year' => '2017',
        'Location' => 'New Zealand',
        'Status' => '',
    ];
    // Add default items to repeater field
    foreach($defaults as $key => $value) {
        $item = $page->meta_fields->getNew();
        // Populate subfields
        $item->field_1 = $key;
        $item->field_2 = $value;
        $item->save();
        $page->meta_fields->add($item);
        $page->save();
    }
});

 

  • Like 2
Link to comment
Share on other sites

On 18/09/2017 at 10:36 PM, Robin S said:

You can do this with a hook to Pages:added() in /site/ready.php


$pages->addHookAfter('added', function(HookEvent $event) {
    $page = $event->arguments(0);
    if($page->template->name !== 'project') return; // Only for this template
    // Define your default labels and values
    $defaults = [
        'Size' => '',
        'Year' => '2017',
        'Location' => 'New Zealand',
        'Status' => '',
    ];
    // Add default items to repeater field
    foreach($defaults as $key => $value) {
        $item = $page->meta_fields->getNew();
        // Populate subfields
        $item->field_1 = $key;
        $item->field_2 = $value;
        $item->save();
        $page->meta_fields->add($item);
        $page->save();
    }
});

 

Going to look at this today! Will report back and thank you for your help.

Link to comment
Share on other sites

  • 5 months later...

I think i have similar problem...

Theres a standard list with items:
User1 / Description1
User2 / Descriptopn2
... 

Looks like a nice application for a repeater ;) but normally, i have ~18 Users and i just want to add a new description on a new page:

- Create a new page with the "userplan" template
- Repeater ("employeerepeater") with fields "username", "description"
- automatically create 5 repeater items with pre filled "username" fields, description fields are empty

Is it possible with repeaters? :) 

Link to comment
Share on other sites

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...