1

I am trying to get all the fields created by admin in my custom plugin setting page.

As we can get entries , in same way can we have option to get the available fields.

 {{ forms.elementSelectField({
    label: 'Entry Select Field',
    id: 'entries',
    name: '',
    elementType: 'craft\\elements\\Entry',
    elements: [craft.entries.one()],
    selectionLabel: 'Select an entry',

}) }}

I tried to look in core file :/vendor/craftcms/cms/src/services/Fields.php

$fields = Craft::$app->fields->getAllFields();

But there is no such method which return the list of existing fields.

Update:

Now i am able to get the list of all the existing fields.

$fieldsData = [];

$fields = Craft::$app->fields->getAllFields();

foreach ($fields as $field)
{

    $fieldsData['name'][] = $field->name;
    $fieldsData['handle'][] = $field->handle;
}

In setting the template of plugin I want to give admin an option to select multi fields (handles ) on the plugin setting page. For this, I am trying to fetch all the created fields first and then pass that array to form micro so that it can populate the multi-select.

But can we populate multi-select like?

{{ forms.multiselectField({
    label: 'Multi Select Field',
    name: '',
    options: {
      fields['handle']: fields['name'],  
    },
    values: '',
}) }}

Or if we create our custom multi-select fields how we can save the user ( admin) input? as in micro, it saves by itself.

  • Can you elaborate on what you're trying to achieve? Maybe the multi-select documentation is helpful: docs.craftcms.com/v3/multi-select-fields.html#templating – Richard May 12 '19 at 11:20
  • I want to give admin an option to select multi fields (handles ) on the plugin setting page. For this i am trying to fetch all the created fields first and then pass that array to form micro so that it can populate the multi-select. – inrsaurabh May 12 '19 at 15:40
1

The fields service does include a method getAllFields:

$fields = Craft::$app->fields->getAllFields(false);
1

Get all the created fields first and encode them.

protected function settingsHtml(): string
{
    $fieldsData = [];
    $fields = Craft::$app->fields->getAllFields();
    foreach ($fields as $field)
    {
        $fieldsData[$field->handle] = $field->name;
    }

}

In settings.twig template

{{ forms.multiselectField({
    label: 'Select Fields',
    name: 'fieldSelected',
    id: 'fieldSelected',
    required:     true,
    options: fields,
    values: settings['fieldSelected']
}) }}

Output:

select fields

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.