Answers.Body: <p>Perhaps I am misunderstanding what you are trying to accomplish, but it seems like you should be able to accomplish what you are looking for with Craft's native <code>EntriesfieldType</code>.
  Regardless, I'll write up a few options for creation your own Entry select modal as well as a custom FieldType.</p>
  
  <p>If you check out Craft's <code>BaseElementFieldType</code> which all of Craft's Element Field Types extend, you'll see that it has an attribute called <code>$inputTemplate</code> with the value <code>'_includes/forms/elementSelect'</code>. This is the template that is rendered for element fields on the entry edit pages, and is accessible from the forms macro!
  To create an element select modal from within a template, you can call </p>
  
  <pre><code>{{ forms.elementSelect({options}) }}
  </code></pre>
  
  <p>Don't forget to import the forms via <code>{% import "_includes/forms" as forms %}</code></p>
  
  <p>If you check out the elementSelect template, you can see all of the fields that are required to create an element select modal. This is really handy and it becomes really easy to create an element select modal. Inspecting an element field type's <code>getInputTemplateVariables()</code> will show you what variables are passed. Use of a debugger here and setting breakpoints to walk through the flow of execution helps a lot as well.</p>
  
  <p>If you check out the EntriesFieldType, you'll see that it relies almost entirely on the logic in the BaseElementFieldType, and just updates the <code>$elementType</code> property, and overrides the <code>getAddButtonLabel()</code> method. So the input template variables defined in the base class are the ones that are used. </p>
  
  <p>Now if you are defining a custom field type, you have this handy tool to create a modal. You can create an input template and make this call, and then implement <code>getInputHtml()</code> to pass the necessary variables and render the template. </p>
  
  <p>Note: If you have never done this before, check out <a href="https://craftcms.com/docs/plugins/field-types" rel="nofollow noreferrer">Craft's documentation</a> on how to create your own custom field.</p>
  
  <p>Let's say you want to create a custom field type that is similar to the Entries field type but you want it to have some additional functionality outside of what you get with the EntriesFieldType. No problem! Craft has made it really easy to do this. What you can do is create a field type like you would a normally, (follow the same naming conventions - PluginName_NameFieldType.php within a fieldtypes directory in your plugin), but instead of extending <code>BaseFieldType</code>, you can extend <code>EntriesFieldType</code> and get all of the functionality of the <code>EntriesFieldType</code> for free! Then you can override methods as you see fit in order to create your custom functionality.</p>
  
  <p>For instance, lets say you want your entries select field to only be able to select fields from your Companies channel, you could override the <code>inputSelectionCritieria()</code> method and return</p>
  
  <pre><code>$criteria['id'] = $ids
  </code></pre>
  
  <p>Where $ids is an array of element ids.</p>
  
  <p>Note: Criteria returned by this method and passed to elementSelect is <strong>not</strong> a CriteriaModel, but is an array with some pretty specific keys. For some more detailed documentation, <a href="https://craftcms.com/classreference/elementtypes/EntryElementType#getSources-detail" rel="nofollow noreferrer">check this out</a>, and also check out the <code>BaseElementFieldType</code> implementation.</p>
  
  <p>Extending the EntriesFieldType is really handy because you can rely on Craft for most of the logic. For instance, if you want your Entry select modal to be exactly the same as the default one, then you don't have to define your own input template. You can modify the input variables as you see fit by overriding methods and adding your own custom functionality, and then let the <code>BaseElementFieldType</code> render the modal itself. </p>
  
  <p>This post has been focused on backend stuff for modals and field types, but it is also possible to extend the modal js for a more custom experience. <a href="https://craftcms.stackexchange.com/questions/895/is-it-possible-to-use-the-elementtype-modal-ui-for-a-custom-field-type-plugin">Check out this stack post</a> and Peter Tell's <a href="https://github.com/page-8/craft-manytomany" rel="nofollow noreferrer">Many to Many plugin</a> for a complete example.</p>
  
  <p>The best tool to figuring out what is going on is inspecting the Craft core as well as other plugins. There isn't great documentation on how to do a lot of this stuff, so if you have more specific questions, please feel free to leave them here! I'd love to expand this answer to have more detailed explanations. Hopefully this can help get you started!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-04T16:42:38.477Z
Answers.LastActivityDate: 2015-12-04T16:42:38.477Z
Answers.LastEditDate: 2017-04-13T12:50:59.930Z
Answers.UserDisplayName: kcolls
Body: <p>I'd like to have an elementSelectField in my plugin. The goal is to select a Company which is defined by my plugin Model. So when clicked, it'll display a list of companies in the Dialog box and you select or search for them, then close it (Same way as selecting Assets).</p>
  
  <p>Is there an example I can see out there or perhaps some guidance? Do I need my own FieldType?</p>
  
  <p>Cheers,
  Ryan</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-12-02T00:26:20.487Z
Comments.Text: I've done this a few times recently but need a little more info on what you are trying to accomplish. What are Companies in your plugin context? Are they entries or models/records that you have defined on your own? When you say you'd like to have an elementSelectField in your plugin, does that mean within your plugins control panel admin template or settings template, or that you want to define a field type for use in your site?
Comments.UserDisplayName: kcolls
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-12-03T00:31:41.560Z
Comments.Text: Correct - I have defined my own, and you are right, I have that field in my plugin. So in my plugin, I have my own custom entry, and inside that they can use the selectField to choose a company.
Comments.UserDisplayName: Ryan
Tags: <form><elementtype>
Title: Plugin: Linking elementSelectField to own plugin models
ThreadText: # Plugin: Linking elementSelectField to own plugin models
  
  (Asked by: username_0 on 2015-11-28)
  
  I'd like to have an elementSelectField in my plugin. The goal is to select a Company which is defined by my plugin Model. So when clicked, it'll display a list of companies in the Dialog box and you select or search for them, then close it (Same way as selecting Assets).
  
  Is there an example I can see out there or perhaps some guidance? Do I need my own FieldType?
  
  Cheers,
  username_0
  
  **Tags:** form, elementtype
  
  > **Comments:**
  > **username_1**: I've done this a few times recently but need a little more info on what you are trying to accomplish. What are Companies in your plugin context? Are they entries or models/records that you have defined on your own? When you say you'd like to have an elementSelectField in your plugin, does that mean within your plugins control panel admin template or settings template, or that you want to define a field type for use in your site?  
  > **username_0**: Correct - I have defined my own, and you are right, I have that field in my plugin. So in my plugin, I have my own custom entry, and inside that they can use the selectField to choose a company.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-12-04. Score: 2
  
  Perhaps I am misunderstanding what you are trying to accomplish, but it seems like you should be able to accomplish what you are looking for with Craft's native `EntriesfieldType`.
  Regardless, I'll write up a few options for creation your own Entry select modal as well as a custom FieldType.
  
  If you check out Craft's `BaseElementFieldType` which all of Craft's Element Field Types extend, you'll see that it has an attribute called `$inputTemplate` with the value `'_includes/forms/elementSelect'`. This is the template that is rendered for element fields on the entry edit pages, and is accessible from the forms macro!
  To create an element select modal from within a template, you can call
  
  ```
  {{ forms.elementSelect({options}) }}
  
  ```
  
  Don't forget to import the forms via `{% import "_includes/forms" as forms %}`
  
  If you check out the elementSelect template, you can see all of the fields that are required to create an element select modal. This is really handy and it becomes really easy to create an element select modal. Inspecting an element field type's `getInputTemplateVariables()` will show you what variables are passed. Use of a debugger here and setting breakpoints to walk through the flow of execution helps a lot as well.
  
  If you check out the EntriesFieldType, you'll see that it relies almost entirely on the logic in the BaseElementFieldType, and just updates the `$elementType` property, and overrides the `getAddButtonLabel()` method. So the input template variables defined in the base class are the ones that are used.
  
  Now if you are defining a custom field type, you have this handy tool to create a modal. You can create an input template and make this call, and then implement `getInputHtml()` to pass the necessary variables and render the template.
  
  Note: If you have never done this before, check out [Craft's documentation](https://craftcms.com/docs/plugins/field-types) on how to create your own custom field.
  
  Let's say you want to create a custom field type that is similar to the Entries field type but you want it to have some additional functionality outside of what you get with the EntriesFieldType. No problem! Craft has made it really easy to do this. What you can do is create a field type like you would a normally, (follow the same naming conventions - PluginName\_NameFieldType.php within a fieldtypes directory in your plugin), but instead of extending `BaseFieldType`, you can extend `EntriesFieldType` and get all of the functionality of the `EntriesFieldType` for free! Then you can override methods as you see fit in order to create your custom functionality.
  
  For instance, lets say you want your entries select field to only be able to select fields from your Companies channel, you could override the `inputSelectionCritieria()` method and return
  
  ```
  $criteria['id'] = $ids
  
  ```
  
  Where $ids is an array of element ids.
  
  Note: Criteria returned by this method and passed to elementSelect is **not** a CriteriaModel, but is an array with some pretty specific keys. For some more detailed documentation, [check this out](https://craftcms.com/classreference/elementtypes/EntryElementType#getSources-detail), and also check out the `BaseElementFieldType` implementation.
  
  Extending the EntriesFieldType is really handy because you can rely on Craft for most of the logic. For instance, if you want your Entry select modal to be exactly the same as the default one, then you don't have to define your own input template. You can modify the input variables as you see fit by overriding methods and adding your own custom functionality, and then let the `BaseElementFieldType` render the modal itself.
  
  This post has been focused on backend stuff for modals and field types, but it is also possible to extend the modal js for a more custom experience. [Check out this stack post](https://craftcms.stackexchange.com/questions/895/is-it-possible-to-use-the-elementtype-modal-ui-for-a-custom-field-type-plugin) and Peter Tell's [Many to Many plugin](https://github.com/page-8/craft-manytomany) for a complete example.
  
  The best tool to figuring out what is going on is inspecting the Craft core as well as other plugins. There isn't great documentation on how to do a lot of this stuff, so if you have more specific questions, please feel free to leave them here! I'd love to expand this answer to have more detailed explanations. Hopefully this can help get you started!
  
  
  ---
  
  