Skip to main content
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:

  1. Create Field Type
  2. Create Field Widget
  3. Create Field Formatter

Youtube Plugin

A Drupal field with formatter and widget plugins to show embedded video of youtube from URL.

Create Field Plugin

  1. Create info yml file
  2. Create Field directories 
    mkdir -p src/Plugin/Field` && `cd src/Plugin/Field` && `mkdir Field{Formatter,Type,Widget}
  3. Add Class files
    YoutubeFieldType, YoutubeFieldFormatter, YoutubeFieldWidget
    in corresponding directories.
  4. 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/

 

Preview

Field settings

Fields

Frontend preview

Youtube plugin

YouTube Demo

This is custom field