48b1b541731b25dc41667fa30883e2c0ac0ac7d5
[yaffs-website] / web / modules / contrib / video_embed_field / src / Plugin / Field / FieldType / VideoEmbedField.php
1 <?php
2
3 namespace Drupal\video_embed_field\Plugin\Field\FieldType;
4
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;
10
11 /**
12  * Plugin implementation of the video_embed_field field type.
13  *
14  * @FieldType(
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" = {}}
22  * )
23  */
24 class VideoEmbedField extends FieldItemBase {
25
26   /**
27    * The embed provider plugin manager.
28    *
29    * @var \Drupal\video_embed_field\ProviderManagerInterface
30    */
31   protected $providerManager;
32
33   /**
34    * {@inheritdoc}
35    */
36   public function __construct($definition, $name = NULL, TraversableTypedDataInterface $parent = NULL, $provider_manager = NULL) {
37     parent::__construct($definition, $name, $parent);
38     $this->providerManager = $provider_manager;
39   }
40
41   /**
42    * {@inheritdoc}
43    */
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);
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public static function schema(FieldStorageDefinitionInterface $field_definition) {
53     return [
54       'columns' => [
55         'value' => [
56           'type' => 'varchar',
57           'length' => 256,
58         ],
59       ],
60     ];
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
67     $properties['value'] = DataDefinition::create('string')
68       ->setLabel(t('Video url'))
69       ->setRequired(TRUE);
70     return $properties;
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function isEmpty() {
77     $value = $this->get('value')->getValue();
78     return empty($value);
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
85     $form = [];
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(),
92     ];
93     return $form;
94   }
95
96   /**
97    * {@inheritdoc}
98    */
99   public static function defaultFieldSettings() {
100     return [
101       'allowed_providers' => [],
102     ];
103   }
104
105 }