In this short presentation, Pravin Kumar, Drupal Consultant, has explained Annotations, why it is used and How to extend FieldWidgets by using Annotations-based Plugins. FieldWidgets are used to render the fields in the edit form.
After completing this, you will have moderate-level knowledge of Annotations and how to use it as the presentation also explains the process of extending FieldWidgets step-by-step.
----------------------------------------------------------
Get Socialistic
Our website: http://valuebound.com/
LinkedIn: http://bit.ly/2eKgdux
Facebook: https://www.facebook.com/valuebound/
Twitter: http://bit.ly/2gFPTi8
3. ● Widgets are basically handling how fields are displayed in
forms.
● In Drupal 8, FieldWidgets have moved from hook
implementations to plugins.
● To extend FieldWidgets in Drupal 8, we need a class with
the FieldWidgets annotation (@Fieldwidget)
INTRO
4. Annotations are one of the
new concept in Drupal 8
that is written in php code
comments.
In sidebar, you can see a
class or function and it
contains metadata about
the function or class.
Annotations
/**
* Plugin implementation of the
'text_textfield' widget.
*
* @FieldWidget(
* id = "text_textfield",
* module = "text",
* label = @Translation("Text
field"),
* field_types = {
* "text"
* }
* )
*
class TextfieldWidget extends
WidgetBase {
5. ● The annotation meta-data lives in the same file and is an integral
part of the class that implements the plugin.
● This makes it easier to find and easier to create a new custom
plugin by simply copying an existing one.
● In addition, Implementation used by Drupal to parse the annotation
simply tokenizes the text of the file without including it as a PHP
file, so memory use is minimized.
● The annotations syntax comes from the Doctrine project. Which is
nothing but the docblock annotations.
Why Annotations?
6. Use Case
Sample use case from Inline Entity Form
Original node removed from backend when remove contacts.
Contacts field is a Entity reference field with unlimited
cardinality.
By showing this contact with Inline Entity Form - complex
FieldWidgets on node edit page.
To avoid this we can extend this FieldWidget...