MediaDirectoriesUiHelper.php in Media Directories 2.x
File
modules/media_directories_ui/src/MediaDirectoriesUiHelper.php
View source
<?php
namespace Drupal\media_directories_ui;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\file\FileInterface;
use Drupal\taxonomy\Entity\Term;
class MediaDirectoriesUiHelper {
protected $entityTypeManager;
protected $settings;
public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory) {
$this->entityTypeManager = $entity_type_manager;
$this->settings = $config_factory
->get('media_directories_ui.settings');
}
public function getMediaType(FileInterface $file = NULL) {
if ($file === NULL) {
return NULL;
}
$types = $this->entityTypeManager
->getStorage('media_type')
->loadMultiple();
$extension = pathinfo($file
->getFileUri(), PATHINFO_EXTENSION);
if (empty($extension)) {
$extension = pathinfo($file
->getFilename(), PATHINFO_EXTENSION);
}
$combined_media_types = $this->settings
->get('combined_upload_media_types');
foreach ($types as $type) {
if (!in_array($type
->id(), $combined_media_types, TRUE)) {
continue;
}
$source_field = $type
->getSource()
->getConfiguration()['source_field'];
$field_config = $this->entityTypeManager
->getStorage('field_config')
->load('media.' . $type
->id() . '.' . $source_field);
if (in_array($extension, explode(' ', $field_config
->getSetting('file_extensions')))) {
return $type;
}
}
return NULL;
}
public function getValidExtensions() {
$valid_extensions = [];
$combined_media_types = $this->settings
->get('combined_upload_media_types');
$types = $this->entityTypeManager
->getStorage('media_type')
->loadMultiple();
foreach ($types as $type) {
if (!in_array($type
->id(), $combined_media_types, TRUE)) {
continue;
}
$source_field = $type
->getSource()
->getConfiguration()['source_field'];
$field_config = $this->entityTypeManager
->getStorage('field_config')
->load('media.' . $type
->id() . '.' . $source_field);
$valid_extensions = array_merge($valid_extensions, explode(' ', $field_config
->getSetting('file_extensions')));
}
$valid_extensions = array_unique($valid_extensions);
return implode(' ', $valid_extensions);
}
public function termIsAnAnchestorOf(Term $term, Term $anchestor) {
if ($term === NULL || $anchestor === NULL) {
return FALSE;
}
$anchestors = $this->entityTypeManager
->getStorage('taxonomy_term')
->loadAllParents($term
->id());
foreach ($anchestors as $one_of_the_anchestors) {
if ($anchestor
->id() == $one_of_the_anchestors
->id()) {
return TRUE;
}
}
return FALSE;
}
public function termIsAChildOf(Term $term, $parent) {
if ($term === NULL) {
return FALSE;
}
$parents = $this->entityTypeManager
->getStorage('taxonomy_term')
->loadParents($term
->id());
if ($parent === NULL) {
if (count($parents) == 0) {
return TRUE;
}
else {
return FALSE;
}
}
foreach ($parents as $one_of_the_parents) {
if ($parent
->id() == $one_of_the_parents
->id()) {
return TRUE;
}
}
return FALSE;
}
}