T
o be able to customize Drupal a nice addition would be to define your own Field Type e.g a Youtube field where the user just enters the youtube url and then it is shown as embedded video on the page.
In this article we would create a simple Youtube Field.
Creating a custom field involved three sections:
- Create Field Type
- Create Field Widget
- Create Field Formatter
Youtube Plugin
A Drupal field with formatter and widget plugins to show embedded video of youtube from URL.
Create Field Plugin
- Create info yml file
- Create Field directories
mkdir -p src/Plugin/Field` && `cd src/Plugin/Field` && `mkdir Field{Formatter,Type,Widget}
- Add Class files
YoutubeFieldType, YoutubeFieldFormatter, YoutubeFieldWidget
- Add template file, css file, libraries yml file, and module file.
Tips
When using in views, inside style settings choose
'Use field template'
Download
To download from local server use:
git clone ssh://ali@g10:2222/home/ali/repos/modules/youtube/