Skip to content
This repository has been archived by the owner on Nov 16, 2021. It is now read-only.

Commit

Permalink
Issue #2830050 by bkosborne: FileUpload widget should allow file exte…
Browse files Browse the repository at this point in the history
…nsions from all supported media bundles (#227)
  • Loading branch information
bkosborne authored and Adam committed Dec 2, 2016
1 parent 8f96673 commit 1ea2fde
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 31 deletions.
Expand Up @@ -13,20 +13,15 @@
*/
abstract class BundleResolverBase extends PluginBase implements BundleResolverInterface, ContainerFactoryPluginInterface {

use SourceFieldTrait;

/**
* The media bundle entity storage handler.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $bundleStorage;

/**
* The configurable field entity storage handler.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $fieldStorage;

/**
* BundleResolverBase constructor.
*
Expand Down Expand Up @@ -58,35 +53,17 @@ public static function create(ContainerInterface $container, array $configuratio
}

/**
* Returns all possible bundles for the field type(s) this plugin supports.
*
* @return MediaBundleInterface[]
* Applicable media bundles, keyed by ID.
* {@inheritdoc}
*/
protected function getPossibleBundles() {
public function getPossibleBundles() {
$plugin_definition = $this->getPluginDefinition();

$filter = function (MediaBundleInterface $bundle) use ($plugin_definition) {
$field = $this->getSourceField($bundle);
$field = $this->getSourceFieldForBundle($bundle);
return $field ? in_array($field->getType(), $plugin_definition['field_types']) : FALSE;
};

return array_filter($this->bundleStorage->loadMultiple(), $filter);
}

/**
* Returns the source field for a media bundle.
*
* @param \Drupal\media_entity\MediaBundleInterface $bundle
* The media bundle entity.
*
* @return \Drupal\Core\Field\FieldConfigInterface
* The configurable source field entity.
*/
protected function getSourceField(MediaBundleInterface $bundle) {
$type_config = $bundle->getType()->getConfiguration();
$id = 'media.' . $bundle->id() . '.' . $type_config['source_field'];
return $this->fieldStorage->load($id);
}

}
Expand Up @@ -23,4 +23,12 @@ interface BundleResolverInterface {
*/
public function getBundle($input);

/**
* Returns all possible bundles for the field type(s) this plugin supports.
*
* @return MediaBundleInterface[]
* Applicable media bundles, keyed by ID.
*/
public function getPossibleBundles();

}
Expand Up @@ -134,6 +134,9 @@ public function getForm(array &$original_form, FormStateInterface $form_state, a
[ManagedFile::class, 'processManagedFile'],
[$this, 'processInitialFileElement'],
],
'#upload_validators' => [
'file_validate_extensions' => [$this->getAllowedFileUploadExtensions()],
],
);

return $form;
Expand Down Expand Up @@ -315,6 +318,23 @@ public function onRemove(array &$form, FormStateInterface $form_state, Request $
return $response->addCommand($command);
}

/**
* Returns a list of acceptable file extensions for the file upload field.
*
* @return array
* The list of acceptable file extensions.
*/
protected function getAllowedFileUploadExtensions() {
$extensions = [];
$possible_bundles = $this->bundleResolver->getPossibleBundles();
foreach ($possible_bundles as $bundle) {
$field = $this->getSourceFieldForBundle($bundle);
$extensions = array_merge($extensions, preg_split('/,?\s+/', $field->getSetting('file_extensions')));
}

return implode(' ', array_unique($extensions));
}

/**
* {@inheritdoc}
*/
Expand Down
Expand Up @@ -21,7 +21,7 @@ class FileUpload extends BundleResolverBase {
public function getBundle($input) {
if ($input instanceof FileInterface) {
foreach ($this->getPossibleBundles() as $bundle) {
$field = $this->getSourceField($bundle);
$field = $this->getSourceFieldForBundle($bundle);
$extensions = preg_split('/,?\s+/', $field->getSetting('file_extensions'));

$extension = pathinfo($input->getFilename(), PATHINFO_EXTENSION);
Expand Down
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\lightning_media;

use Drupal\media_entity\MediaBundleInterface;
use Drupal\media_entity\MediaInterface;

/**
Expand All @@ -26,8 +27,24 @@ trait SourceFieldTrait {
* The source field config entity.
*/
protected function getSourceField(MediaInterface $entity) {
$type_config = $entity->getType()->getConfiguration();
$id = $entity->getEntityTypeId() . '.' . $entity->bundle() . '.' . $type_config['source_field'];
return $this->getSourceFieldForBundle($entity->bundle->entity);
}

/**
* Returns the source field for a media bundle.
*
* @param \Drupal\media_entity\MediaBundleInterface $bundle
* The media bundle entity.
*
* @return \Drupal\Core\Field\FieldConfigInterface
* The configurable source field entity.
*/
protected function getSourceFieldForBundle(MediaBundleInterface $bundle) {
$type_config = $bundle->getType()->getConfiguration();
if (empty($type_config['source_field'])) {
return NULL;
}
$id = 'media.' . $bundle->id() . '.' . $type_config['source_field'];

return $this->fieldStorage->load($id);
}
Expand Down

0 comments on commit 1ea2fde

Please sign in to comment.