3 namespace Drupal\video_embed_field\Plugin\Field\FieldType;
5 use Drupal\Core\Field\FieldItemBase;
6 use Drupal\Core\Field\FieldStorageDefinitionInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\TypedData\DataDefinition;
9 use Drupal\Core\TypedData\TraversableTypedDataInterface;
12 * Plugin implementation of the video_embed_field field type.
15 * id = "video_embed_field",
16 * label = @Translation("Video Embed"),
17 * description = @Translation("Stores a video and then outputs some embed code."),
18 * category = @Translation("Media"),
19 * default_widget = "video_embed_field_textfield",
20 * default_formatter = "video_embed_field_video",
21 * constraints = {"VideoEmbedValidation" = {}}
24 class VideoEmbedField extends FieldItemBase {
27 * The embed provider plugin manager.
29 * @var \Drupal\video_embed_field\ProviderManagerInterface
31 protected $providerManager;
36 public function __construct($definition, $name = NULL, TraversableTypedDataInterface $parent = NULL, $provider_manager = NULL) {
37 parent::__construct($definition, $name, $parent);
38 $this->providerManager = $provider_manager;
44 public static function createInstance($definition, $name = NULL, TraversableTypedDataInterface $parent = NULL) {
45 $provider_manager = \Drupal::service('video_embed_field.provider_manager');
46 return new static($definition, $name, $parent, $provider_manager);
52 public static function schema(FieldStorageDefinitionInterface $field_definition) {
66 public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
67 $properties['value'] = DataDefinition::create('string')
68 ->setLabel(t('Video url'))
76 public function isEmpty() {
77 $value = $this->get('value')->getValue();
84 public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
86 $form['allowed_providers'] = [
87 '#title' => $this->t('Allowed Providers'),
88 '#description' => $this->t('Restrict users from entering information from the following providers. If none are selected any video provider can be used.'),
89 '#type' => 'checkboxes',
90 '#default_value' => $this->getSetting('allowed_providers'),
91 '#options' => $this->providerManager->getProvidersOptionList(),
99 public static function defaultFieldSettings() {
101 'allowed_providers' => [],