Answers.Body: <p>In order to include a custom field type you first need to register your own <a href="https://docs.craftcms.com/v3/extend/module-guide.html#app" rel="noreferrer">module</a> or <a href="https://docs.craftcms.com/v3/extend/plugin-guide.html" rel="noreferrer">plugin</a> you can create the basic structure for both from <a href="https://pluginfactory.io/" rel="noreferrer">pluginfactory.io</a> and register them as you like. There is also a default module that comes with every Craft 3 installation. You only need to bootstrap it in your <code>app.php</code></p>
  
  <p>Then in your module you need the following code </p>
  
  <pre class="lang-php prettyprint-override"><code>public function init()
  {
      Event::on(Fields::class, Fields::EVENT_REGISTER_FIELD_TYPES, function(RegisterComponentTypesEvent $event) {
          $event-&gt;types[] = MyField::class;
      });
  }
  </code></pre>
  
  <p>And your class could look like the <a href="https://github.com/mmikkel/IncognitoField-Craft3/blob/master/src/fields/IncognitoFieldType.php" rel="noreferrer">Incognito Field</a> for example.
  Basically every field has several methods implemented in the <code>FieldInterface</code> such as <code>getInputHtml</code> to render the HTML or <code>normalizeValue</code> to create a field value based on user or DB input and <code>serializeValue</code> to prepare the value for your DB. The <a href="https://docs.craftcms.com/api/v3/craft-base-fieldinterface.html" rel="noreferrer">interface</a> has everything you need.</p>
  
  <p>A good example to get started is to extend the <code>PlainText</code> field and customize it's functions. You can overwrite the <code>inputHtml</code> function and render a custom template</p>
  
  <pre class="lang-php prettyprint-override"><code>public function getInputHtml($value, ElementInterface $element = null): string
  {
      // Get our id and namespace
      $id = Craft::$app-&gt;getView()-&gt;formatInputId($this-&gt;handle);
      $namespacedId = Craft::$app-&gt;getView()-&gt;namespaceInputId($id);
  
      // Render the input template
      return Craft::$app-&gt;getView()-&gt;renderTemplate(
          'module-id/fields/hidden/_input',
          [
              'name' =&gt; $this-&gt;handle,
              'value' =&gt; $value,
              'field' =&gt; $this,
              'id' =&gt; $id,
              'namespacedId' =&gt; $namespacedId,
          ]
      );
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-09T15:27:48.127Z
Answers.Text: Thank you for answering my question again. I made a new PHP file next to plugin PHP file, but unfortunately the registration function causes error due to an error; "Cannot declare class craft\fields\AssetsWithDL, because the name is already in use". I am looking for the cause now. By the way, I would like to have an assets field with download link, so I try to extend Assets field.
Answers.UserDisplayName: Kota.M
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-09T15:39:21.283Z
Answers.Text: If it's only that you can download a plugin or register a custom js file with about 15 lines that adds the functionality to the default assets field
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-09T17:31:57.850Z
Answers.Text: Is it this one? https://craftcms.stackexchange.com/questions/24089/download-files-in-assets-fields
Answers.UserDisplayName: Kota.M
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-09T17:32:34.873Z
Answers.Text: I added it in main.js, but it does not work. Could I ask where the js function should be in?
Answers.UserDisplayName: Kota.M
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-09T15:04:09.460Z
Answers.LastActivityDate: 2019-04-09T15:04:09.460Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I have been struggling to create my own custom field, so I would like to ask how to create it.</p>
  
  <p>First of all, I don't know where the files should be in.
  I have some custom fields in Craft 2.
  It might be able to be migrated, but it is not.</p>
  
  <p>I have these files under src folder in a plugin.</p>
  
  <pre><code>fields/{fieldtypeName}FieldType.php
  templates/_fieldtypes/{fieldtypeHandle}/input.html
  templates/_fieldtypes/{fieldtypeHandle}/settings.html
  </code></pre>
  
  <p>Then I added a registration as the documentation, but Craft says it is not found.
  <a href="https://docs.craftcms.com/v3/extend/field-types.html" rel="nofollow noreferrer">https://docs.craftcms.com/v3/extend/field-types.html</a></p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-09T14:44:31.157Z
Comments.Text: Could you be a little bit more precise? There are more than enough examples of custom field types in several plugins that shows you how to create one. And there are many example of Craft fields that you can look at. Not sure where you struggle. What is not found? The class? The Template? Do you try to use your old field or create a new one?
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-09T14:51:01.280Z
Comments.Text: I have looked for sample code on the Internet, but I found nothing. However, I didn't see other plugins. I have no idea what the codes like, and where the code should be in. And I would like to create a new one. I tried to migrate the old one because I thought it helps me somewhat.
Comments.UserDisplayName: Kota.M
Tags: <custom-fieldtype>
Title: How to create custom field type in Craft 3
ThreadText: # How to create custom field type in Craft 3
  
  (Asked by: username_0 on 2019-04-09)
  
  I have been struggling to create my own custom field, so I would like to ask how to create it.
  
  First of all, I don't know where the files should be in.
  I have some custom fields in Craft 2.
  It might be able to be migrated, but it is not.
  
  I have these files under src folder in a plugin.
  
  ```
  fields/{fieldtypeName}FieldType.php
  templates/_fieldtypes/{fieldtypeHandle}/input.html
  templates/_fieldtypes/{fieldtypeHandle}/settings.html
  
  ```
  
  Then I added a registration as the documentation, but Craft says it is not found.
  <https://docs.craftcms.com/v3/extend/field-types.html>
  
  **Tags:** custom-fieldtype
  
  > **Comments:**
  > **username_1**: Could you be a little bit more precise? There are more than enough examples of custom field types in several plugins that shows you how to create one. And there are many example of Craft fields that you can look at. Not sure where you struggle. What is not found? The class? The Template? Do you try to use your old field or create a new one?  
  > **username_0**: I have looked for sample code on the Internet, but I found nothing. However, I didn't see other plugins. I have no idea what the codes like, and where the code should be in. And I would like to create a new one. I tried to migrate the old one because I thought it helps me somewhat.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-04-09. Score: 5
  
  In order to include a custom field type you first need to register your own [module](https://docs.craftcms.com/v3/extend/module-guide.html#app) or [plugin](https://docs.craftcms.com/v3/extend/plugin-guide.html) you can create the basic structure for both from [pluginfactory.io](https://pluginfactory.io/) and register them as you like. There is also a default module that comes with every Craft 3 installation. You only need to bootstrap it in your `app.php`
  
  Then in your module you need the following code
  
  ```php
  public function init()
  {
      Event::on(Fields::class, Fields::EVENT_REGISTER_FIELD_TYPES, function(RegisterComponentTypesEvent $event) {
          $event->types[] = MyField::class;
      });
  }
  
  ```
  
  And your class could look like the [Incognito Field](https://github.com/mmikkel/IncognitoField-Craft3/blob/master/src/fields/IncognitoFieldType.php) for example.
  Basically every field has several methods implemented in the `FieldInterface` such as `getInputHtml` to render the HTML or `normalizeValue` to create a field value based on user or DB input and `serializeValue` to prepare the value for your DB. The [interface](https://docs.craftcms.com/api/v3/craft-base-fieldinterface.html) has everything you need.
  
  A good example to get started is to extend the `PlainText` field and customize it's functions. You can overwrite the `inputHtml` function and render a custom template
  
  ```php
  public function getInputHtml($value, ElementInterface $element = null): string
  {
      // Get our id and namespace
      $id = Craft::$app->getView()->formatInputId($this->handle);
      $namespacedId = Craft::$app->getView()->namespaceInputId($id);
  
      // Render the input template
      return Craft::$app->getView()->renderTemplate(
          'module-id/fields/hidden/_input',
          [
              'name' => $this->handle,
              'value' => $value,
              'field' => $this,
              'id' => $id,
              'namespacedId' => $namespacedId,
          ]
      );
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Thank you for answering my question again. I made a new PHP file next to plugin PHP file, but unfortunately the registration function causes error due to an error; "Cannot declare class craft\fields\AssetsWithDL, because the name is already in use". I am looking for the cause now. By the way, I would like to have an assets field with download link, so I try to extend Assets field.  
  > **username_1**: If it's only that you can download a plugin or register a custom js file with about 15 lines that adds the functionality to the default assets field  
  > **username_0**: Is it this one? https://craftcms.stackexchange.com/questions/24089/download-files-in-assets-fields  
  > **username_0**: I added it in main.js, but it does not work. Could I ask where the js function should be in?  
  
  ---
  
  