View source
<?php
namespace Drupal\video\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\file\Entity\File;
use Drupal\file\Plugin\Field\FieldType\FileItem;
use Drupal\Component\Utility\Random;
class VideoItem extends FileItem {
protected $entityManager;
public static function defaultStorageSettings() {
return [
'default_video' => [
'uuid' => NULL,
'data' => NULL,
],
] + parent::defaultStorageSettings();
}
public static function defaultFieldSettings() {
$settings = [
'file_extensions' => '',
'file_directory' => 'videos/[date:custom:Y]-[date:custom:m]',
] + parent::defaultFieldSettings();
unset($settings['description_field']);
return $settings;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'target_id' => [
'description' => 'The ID of the file entity.',
'type' => 'int',
'unsigned' => TRUE,
],
'data' => [
'description' => "Additional video metadata.",
'type' => 'varchar',
'length' => 512,
],
],
'indexes' => [
'target_id' => [
'target_id',
],
],
'foreign keys' => [
'target_id' => [
'table' => 'file_managed',
'columns' => [
'target_id' => 'fid',
],
],
],
];
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties = parent::propertyDefinitions($field_definition);
unset($properties['display']);
unset($properties['description']);
$properties['data'] = DataDefinition::create('string')
->setLabel(t('Additional video metadata'))
->setDescription(t("Additional metadata for the uploadded or embedded video."));
return $properties;
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$element = [];
return $element;
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$element = parent::fieldSettingsForm($form, $form_state);
unset($element['description_field']);
unset($element['file_directory']);
unset($element['file_extensions']);
unset($element['max_filesize']);
return $element;
}
public function preSave() {
parent::preSave();
}
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$random = new Random();
$settings = $field_definition
->getSettings();
$dirname = static::doGetUploadLocation($settings);
file_prepare_directory($dirname, FILE_CREATE_DIRECTORY);
$destination = $dirname . '/' . $random
->name(10, TRUE) . '.mp4';
$data = $random
->paragraphs(3);
$file = file_save_data($data, $destination, FILE_EXISTS_ERROR);
$values = [
'target_id' => $file
->id(),
];
return $values;
}
public function isDisplayed() {
return TRUE;
}
protected function getEntityManager() {
if (!isset($this->entityManager)) {
$this->entityManager = \Drupal::entityManager();
}
return $this->entityManager;
}
}