Scroll
Field Types, Widgets and Formatters are plugins
In Drupal 8, all field types, widgets, and formatters are plugins.
They are automatically discovered if placed in the expected directory structure and use the correct annotation.
The directory structure for a module that implements all three looks like this:
foo_bar_fields
- foo_bar_fields.info.yml
- src/
- Plugin/
- Field/
- FieldType/
- FooItem.php
- FieldFormatter/
- FooBarFormatter.php
- FieldWidget/
- FooBarWidget.php
- FieldType/
- Field/
- Plugin/
In this tutorial, we will create a custom field type, field formatter, and field widget that generates a random string and displays it.
The module will be named random and will follow this structure:
random
- random.info.yml
- src/
- Plugin/
- Field/
- FieldType/
- RandomItem.php
- FieldFormatter/
- RandomDefaultFormatter.php
- FieldWidget/
- RandomDefaultWidget.php
- FieldType/
- Field/
- Plugin/
Source URL:
Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.