3

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, Ryan

  • 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? – kcolls Dec 2 '15 at 0:26
  • 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. – Ryan Dec 3 '15 at 0:31
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 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, 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 and Peter Tell's Many to Many plugin 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!

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.