Version 1
[yaffs-website] / web / modules / contrib / video / src / Plugin / Field / FieldType / VideoItem.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\video\Plugin\Field\FieldType\VideoItem.
6  */
7
8 namespace Drupal\video\Plugin\Field\FieldType;
9
10 use Drupal\Core\Field\FieldDefinitionInterface;
11 use Drupal\Core\Field\FieldStorageDefinitionInterface;
12 use Drupal\Core\Form\FormStateInterface;
13 use Drupal\Core\StreamWrapper\StreamWrapperInterface;
14 use Drupal\Core\TypedData\DataDefinition;
15 use Drupal\file\Entity\File;
16 use Drupal\file\Plugin\Field\FieldType\FileItem;
17
18 /**
19  * Plugin implementation of the 'video' field type.
20  *
21  * @FieldType(
22  *   id = "video",
23  *   label = @Translation("Video"),
24  *   description = @Translation("This field stores the ID of an video file or embedded video as an integer value."),
25  *   category = @Translation("Reference"),
26  *   default_widget = "video_embed",
27  *   default_formatter = "video_embed_player",
28  *   column_groups = {
29  *     "file" = {
30  *       "label" = @Translation("File"),
31  *       "columns" = {
32  *         "target_id", "data"
33  *       }
34  *     },
35  *   },
36  *   list_class = "\Drupal\file\Plugin\Field\FieldType\FileFieldItemList",
37  *   constraints = {"ReferenceAccess" = {}, "FileValidation" = {}}
38  * )
39  */
40 class VideoItem extends FileItem {
41
42   /**
43    * The entity manager.
44    *
45    * @var \Drupal\Core\Entity\EntityManagerInterface
46    */
47   protected $entityManager;
48
49   /**
50    * {@inheritdoc}
51    */
52   public static function defaultStorageSettings() {
53     return array(
54       'default_video' => array(
55         'uuid' => NULL,
56         'data' => NULL
57       ),
58     ) + parent::defaultStorageSettings();
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public static function defaultFieldSettings() {
65     $settings = array(
66       'file_extensions' => '',
67       'file_directory' => 'videos/[date:custom:Y]-[date:custom:m]',
68     ) + parent::defaultFieldSettings();
69     // Remove field option.
70     unset($settings['description_field']);
71     return $settings;
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public static function schema(FieldStorageDefinitionInterface $field_definition) {
78     return array(
79       'columns' => array(
80         'target_id' => array(
81           'description' => 'The ID of the file entity.',
82           'type' => 'int',
83           'unsigned' => TRUE,
84         ),
85         'data' => array(
86           'description' => "Additional video metadata.",
87           'type' => 'varchar',
88           'length' => 512,
89         ),
90       ),
91       'indexes' => array(
92         'target_id' => array('target_id'),
93       ),
94       'foreign keys' => array(
95         'target_id' => array(
96           'table' => 'file_managed',
97           'columns' => array('target_id' => 'fid'),
98         ),
99       ),
100     );
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
107     $properties = parent::propertyDefinitions($field_definition);
108     
109     // unset the default values from the file module
110     unset($properties['display']);
111     unset($properties['description']);
112
113     $properties['data'] = DataDefinition::create('string')
114       ->setLabel(t('Additional video metadata'))
115       ->setDescription(t("Additional metadata for the uploadded or embedded video."));
116
117     return $properties;
118   }
119
120   /**
121    * {@inheritdoc}
122    */
123   public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
124     $element = array();
125     return $element;
126   }
127
128   /**
129    * {@inheritdoc}
130    */
131   public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
132     // Get base form from FileItem.
133     $element = parent::fieldSettingsForm($form, $form_state);
134     
135     // Remove the description option.
136     unset($element['description_field']);
137     unset($element['file_directory']);
138     unset($element['file_extensions']);
139     unset($element['max_filesize']);
140     return $element;
141   }
142
143   /**
144    * {@inheritdoc}
145    */
146   public function preSave() {
147     parent::preSave();
148   }
149
150   /**
151    * {@inheritdoc}
152    */
153   public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
154     $random = new Random();
155     $settings = $field_definition->getSettings();
156
157     // Prepare destination.
158     $dirname = static::doGetUploadLocation($settings);
159     file_prepare_directory($dirname, FILE_CREATE_DIRECTORY);
160
161     // Generate a file entity.
162     $destination = $dirname . '/' . $random->name(10, TRUE) . '.mp4';
163     $data = $random->paragraphs(3);
164     $file = file_save_data($data, $destination, FILE_EXISTS_ERROR);
165     $values = array(
166       'target_id' => $file->id(),
167     );
168     return $values;
169   }
170
171   /**
172    * {@inheritdoc}
173    */
174   public function isDisplayed() {
175     // Video items do not have per-item visibility settings.
176     return TRUE;
177   }
178
179   /**
180    * Gets the entity manager.
181    *
182    * @return \Drupal\Core\Entity\EntityManagerInterface.
183    */
184   protected function getEntityManager() {
185     if (!isset($this->entityManager)) {
186       $this->entityManager = \Drupal::entityManager();
187     }
188     return $this->entityManager;
189   }
190
191 }