Dynamic fields in panel

Dynamic fields in panel

Hi,

Here is what I want to achieve:
In a panel page, I’d like to add fields of a certain type (in the present case it is a Multiselect field) in function of other pages with a certain template.

For example:
I got pages category 1, category 2, …, category n, with the category template. On another panel page, ie project, I would like n fields of type multiselect, with 1st field querying its options from page category 1 children, 2nd field from category 2, etc.

I was thinking of creating a custom Field, but without success so far.
Does anyone know of an existing solution or could lead me to build my own?

I hope I was clear enough.
Thanks

I’m not sure if I understand. So do you need a dynamic number of fields or is the number of fields fixed? Or is it just that each field needs to query the children of another category?

What information do you want to query from the subpages of each category?

Thank you for your answer, texnixe.

I want a field for each existing page with a specific template.

Let’s say I have this pages structure:

- Home
- Projects
- Categories
  - Clients
    - John Doe
    - Jane Dae
  - Types
    - Food
    - Drinks

When I add a project, I would like to have those fields on creation page:

Title
Text
Clients (mulitiselect)
  - John Doe
  - Jane Dae
Types (mulitiselect)
  - Food
  - Drinks

So when a new page is created with the template category (like clients or types), It will be automatically added to project’s panel pages as a new multiselect field.

In another way, I’d like to replace those lines in my project blueprint:

clients:
	label: Clients
	type: multiselect
	options: query
	query:
		page: clients
types:
	label: Types
	type: multiselect
	options: query
	query:
		page: types

with something like:

categories:
	type: categories
	options: query
	query:
		template: category

Ideally.

Is it more clear?

It’s probably doable but I think you would have to integrate the multi-select field into your new field.

Thank you, I’ll see what I can do.
Maybe I’ll find another approach to the problem.

Sorry to bother again.
I am trying to work on this idea of creating dynamics fields based on other page content. I remove the multiselect of the equation to make it simpler (replace with simple input). I get all pages with a ‘type’ template and create an input field for each one.
Here is what I have so far:

class TypesField extends BaseField {

    public function fields() {
        $fields = array();
        foreach (site()->index()->filterBy('intendedTemplate', 'type') as $t) {
            $fields[] = new InputField();
        }

        return $fields;
    }

    public function content() {
        return tpl::load(__DIR__ . DS . 'template.php', array('fields' => $this->fields()));
    }

    public function label() {
        return null;
    }

    public function result() {
        // ???
    }
}

I can get the input to show on my panel page. But my problem now is to get the content of the inputs on save so I can write them in the content. I suppose it should be done in the result method and return a yaml string. But I can’t find how to read the values…
Has nobody an idea?
Thanks

I found the (obvious) response to my answer: I didn’t put any name to the inputs.
So in the end it looks more like something like this:

class TypesField extends BaseField {
    private $inputs = array();

    public function input() {
        $div = new Brick('div');
        foreach (site()->index()->filterBy('intendedTemplate', 'type') as $t) {
            $input = new Brick('input', null);
            $input->addClass('input');
            $input->attr(array(
                'type'         => $this->type(),
                'required'     => $this->required(),
                'name'         => $this->name() . '[]',
                'autocomplete' => $this->autocomplete() === false ? 'off' : 'on',
                'autofocus'    => $this->autofocus(),
                'readonly'     => $this->readonly(),
                'disabled'     => $this->disabled(),
                'id'           => $this->id()
            ));
            $div->append($input);
        }

        return $div;
    }

    public function content() {
        return tpl::load(__DIR__ . DS . 'template.php', array('field' => $this));
    }

    public function label() {
        return null;
    }

    public function result() {
        $result = parent::result();
        return yaml::encode($result);
    }
}

The code is still missing a lot of stuff, but now I’ll be trying to use a structure field instead of a simple input.
Still if anyone has an idea or a clue of how I can achieve that, he’s more than welcome to help!

Did you solve this. I would like to create structured data based off the values selected in the panel. So a multiselect then within the structured data I would dynamically generate out the fields.