X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fmedia_entity_document%2Fsrc%2FPlugin%2FMediaEntity%2FType%2FDocument.php;fp=web%2Fmodules%2Fcontrib%2Fmedia_entity_document%2Fsrc%2FPlugin%2FMediaEntity%2FType%2FDocument.php;h=c75fb7d5043df46ac2692658894c1e725c0e535b;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/modules/contrib/media_entity_document/src/Plugin/MediaEntity/Type/Document.php b/web/modules/contrib/media_entity_document/src/Plugin/MediaEntity/Type/Document.php new file mode 100644 index 000000000..c75fb7d50 --- /dev/null +++ b/web/modules/contrib/media_entity_document/src/Plugin/MediaEntity/Type/Document.php @@ -0,0 +1,123 @@ + $this->t('File MIME'), + 'size' => $this->t('Size'), + ]; + } + + /** + * {@inheritdoc} + */ + public function getField(MediaInterface $media, $name) { + $source_field = $this->configuration['source_field']; + + // Get the file document. + /** @var \Drupal\file\FileInterface $file */ + $file = $media->{$source_field}->entity; + + // Return the field. + switch ($name) { + case 'mime': + return !$file->filemime->isEmpty() ? $file->getMimeType() : FALSE; + + case 'size': + $size = $file->getSize(); + return $size ? $size : FALSE; + } + + return FALSE; + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + /** @var \Drupal\media_entity\MediaBundleInterface $bundle */ + $bundle = $form_state->getFormObject()->getEntity(); + $options = []; + $allowed_field_types = ['file']; + + /** @var \Drupal\Core\Field\FieldDefinitionInterface $field */ + foreach ($this->entityFieldManager->getFieldDefinitions('media', $bundle->id()) as $field_name => $field) { + if (in_array($field->getType(), $allowed_field_types) && !$field->getFieldStorageDefinition()->isBaseField()) { + $options[$field_name] = $field->getLabel(); + } + } + + $form['source_field'] = [ + '#type' => 'select', + '#title' => $this->t('Field with source information'), + '#description' => $this->t('Field on media entity that stores Document file. You can create a bundle without selecting a value for this dropdown initially. This dropdown can be populated after adding fields to the bundle.'), + '#default_value' => empty($this->configuration['source_field']) ? NULL : $this->configuration['source_field'], + '#options' => $options, + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function thumbnail(MediaInterface $media) { + $source_field = $this->configuration['source_field']; + /** @var \Drupal\file\FileInterface $file */ + $file = $media->{$source_field}->entity; + + if ($file) { + $mimetype = $file->getMimeType(); + $mimetype = explode('/', $mimetype); + $thumbnail = $this->config->get('icon_base') . "/{$mimetype[0]}-{$mimetype[1]}.png"; + + if (!is_file($thumbnail)) { + $thumbnail = $this->config->get('icon_base') . "/{$mimetype[1]}.png"; + + if (!is_file($thumbnail)) { + $thumbnail = $this->config->get('icon_base') . '/document.png'; + } + } + } + else { + $thumbnail = $this->config->get('icon_base') . '/document.png'; + } + + return $thumbnail; + } + + /** + * {@inheritdoc} + */ + public function getDefaultName(MediaInterface $media) { + // The default name will be the filename of the source_field, if present. + $source_field = $this->configuration['source_field']; + + /** @var \Drupal\file\FileInterface $file */ + if (!empty($source_field) && ($file = $media->{$source_field}->entity)) { + return $file->getFilename(); + } + + return parent::getDefaultName($media); + } + +}