The form API is the most basic way to define custom forms in Drupal. It is using form elements to show HTML controls for form fields.
(Field) Widgets on the other hand are part of the entity fields API:
Field API widgets specify how fields are displayed in edit forms.
[...]
Widgets are Form API elements with additional processing capabilities.
They control how the form elements of fields attached to entities are rendered and how data provided by the user is written back to the respective fields. Remember, entity fields might have a cardinality. So Widgets must be able to not only provide a simple checkbox, but also form elements for multiple boolean values on the same entity field. They are basically another abstraction layer on top of the form API, and thus not known to the form API itself, which you'd use to create your custom forms.
To use the very same HTML controls in an entity field widget and for the value of a plain form API form field, you'd first have to create an according custom form element (which then could be included in your form via the #type
property), and use this element in a custom field widget as well. This is how you'd do it for very complex elements like file upload elements with drag'n'drop functionality and so on.
More often, using the generic form API controls and adding CSS classes (#attributes
), custom process (#process
) or pre-render functions (#pre_render
) in conjunction with a bit custom CSS and/or JavaScript is sufficient to achieve similar UI behavior between field widgets and their custom form element counterparts. You might even be able to re-use JavaScript or CSS provided by a library attached to a widget when adding the very same CSS classes to the form element and attaching the widget library to your form. A look into the source code of the widget you'd love to use in your form might give you an according idea.