<p>Making sure you include <code>use craft\db\Query;</code> at the top of your Class, you can do something like this:</p>

<pre class="lang-php prettyprint-override"><code>$fields = (new Query())
    -&gt;select(['id','handle','name','instructions','type'])
    -&gt;from(['{{%fields}}'])
    -&gt;all();

foreach ($fields as $field) {
    $output[(int) $field['id']] = array(
        'id'            =&gt; (int) $field['id'],
        'handle'        =&gt; $field['handle'],
        'name'          =&gt; $field['name'],
        'instructions'  =&gt; $field['instructions'],
        'type'          =&gt; $field['type']
    );
}
</code></pre>

CC BY-SA 4.0
2019-06-01T13:37:46.320Z
@Oil

I tried the direct query as you suggested and i got the array https://pastebin.com/DW4D0pRE,

so i still need to pregmatch on type .
inrsaurabh
CC BY-SA 4.0
2019-06-01T13:56:59.480Z
I'm not sure I understand what you are trying to do?
Oli
CC BY-SA 4.0
2019-06-01T13:58:36.230Z
As per field type I will show input of same type in frontend.
inrsaurabh
CC BY-SA 4.0
2019-06-01T14:03:03.657Z
I'm confused. Do you mean field type or input type? Field type for me would be something like `craft\fields\PlainText` which the above gives you..?
Oli
CC BY-SA 4.0
2019-06-01T14:18:41.443Z
@Oil I tried to update my question.By the way, your custom query is still helpful for me, as i don't think of query directly. +1
inrsaurabh
CC BY-SA 4.0
2019-06-02T09:10:20.317Z
Oh I see! I may be wrong but I don't think you'll get that this way. Maybe make a custom map yourself?
Oli
CC BY-SA 4.0
2019-06-02T09:15:56.073Z
Currently what I planned to get the field types of all the selected fields from my plugin settings page, than iterate over each field type and make relevant Html for frontend template. Chances are code may not be optimized initially, or extra lines of code are there 
inrsaurabh
CC BY-SA 4.0
2019-06-01T13:12:24.753Z
2019-06-01T13:12:24.753Z
Oli
<p>I want to show some input fields on frontend template form.</p>

<blockquote>
  <p>Type of input is dependent on the type of fields selected on the backend.</p>
</blockquote>

<p><strong>For example,</strong> if the text field is selected in the backend I will create a field of <code>type="text"</code>. If a number field is selected I will show an input <code>type="number"</code> in the template.</p>

<p>I am trying to get <code>the field type</code> in my plugin.</p>

<pre><code>$fields = Craft::$app-&gt;fields-&gt;getAllFields();
        foreach ($fields as $field) {
             $output[(int) $field-&gt;id] = array(
                'id'            =&gt; (int) $field-&gt;id,
                'handle'        =&gt; $field-&gt;handle,
                'name'          =&gt; $field-&gt;name,
                'instructions'  =&gt; $field-&gt;instructions
            );
        }
</code></pre>

<p>I checked the /vendor/craftcms/cms/src/services/Fields.php but not found any method.</p>

<p><strong>Update:</strong>
I checked a few files and i found a method which returns the string.</p>

<pre><code>$field-&gt;displayName();
</code></pre>

<ol>
<li>Number</li>
<li>Lightswitch</li>
</ol>

<p>Is it the only way to get the type of field.</p>

<plugin-development><craft3><fieldtypes>
Loop through all created fields and get the field type
# Loop through all created fields and get the field type

(Asked by: username_0 on 2019-06-01)

I want to show some input fields on frontend template form.

Type of input is dependent on the type of fields selected on the backend.

For example, if the text field is selected in the backend I will create a field of type="text". If a number field is selected I will show an input type="number" in the template.
I am trying to get the field type in my plugin.
$fields = Craft::$app->fields->getAllFields();
        foreach ($fields as $field) {
             $output[(int) $field->id] = array(
                'id'            => (int) $field->id,
                'handle'        => $field->handle,
                'name'          => $field->name,
                'instructions'  => $field->instructions
            );
        }

I checked the /vendor/craftcms/cms/src/services/Fields.php but not found any method.
Update:
I checked a few files and i found a method which returns the string.
$field->displayName();


Number
Lightswitch

Is it the only way to get the type of field.

**Tags:** plugin-development, craft3, fieldtypes

---

## 1 answers

### Answer by username_1 on 2019-06-01. Score: 2

Making sure you include use craft\db\Query; at the top of your Class, you can do something like this:
$fields = (new Query())
    ->select(['id','handle','name','instructions','type'])
    ->from(['{{%fields}}'])
    ->all();

foreach ($fields as $field) {
    $output[(int) $field['id']] = array(
        'id'            => (int) $field['id'],
        'handle'        => $field['handle'],
        'name'          => $field['name'],
        'instructions'  => $field['instructions'],
        'type'          => $field['type']
    );
}

> **Comments:**
> **username_0**: @Oil  I tried the direct query as you suggested and i got the array https://pastebin.com/DW4D0pRE,  so i still need to pregmatch on type .  
> **username_1**: I'm not sure I understand what you are trying to do?  
> **username_0**: As per field type I will show input of same type in frontend.  
> **username_1**: I'm confused. Do you mean field type or input type? Field type for me would be something like `craft\fields\PlainText` which the above gives you..?  
> **username_0**: @Oil I tried to update my question.By the way, your custom query is still helpful for me, as i don't think of query directly. +1  
> **username_1**: Oh I see! I may be wrong but I don't think you'll get that this way. Maybe make a custom map yourself?  
> **username_0**: Currently what I planned to get the field types of all the selected fields from my plugin settings page, than iterate over each field type and make relevant Html for frontend template. Chances are code may not be optimized initially, or extra lines of code are there   

---

