Commit 15045e6c authored by stBorchert's avatar stBorchert Committed by anon

Issue #2700365 by stBorchert, anon: Allow limiting FileMatcher to specific extensions

parent 1c78c409
...@@ -23,26 +23,32 @@ class FileMatcher extends EntityMatcher { ...@@ -23,26 +23,32 @@ class FileMatcher extends EntityMatcher {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getSummary() { public function getSummary() {
$summery = parent::getSummary(); $summary = parent::getSummary();
$summery[] = $this->t('Show image dimensions: @show_image_dimensions', [ if (!empty($this->configuration['extensions'])) {
$summary[] = $this->t('Limit matches to the following extensions: @extensions', [
'@extensions' => str_replace(' ', ', ', $this->configuration['extensions']),
]);
}
$summary[] = $this->t('Show image dimensions: @show_image_dimensions', [
'@show_image_dimensions' => $this->configuration['images']['show_dimensions'] ? $this->t('Yes') : $this->t('No'), '@show_image_dimensions' => $this->configuration['images']['show_dimensions'] ? $this->t('Yes') : $this->t('No'),
]); ]);
$summery[] = $this->t('Show image thumbnail: @show_image_thumbnail', [ $summary[] = $this->t('Show image thumbnail: @show_image_thumbnail', [
'@show_image_thumbnail' => $this->configuration['images']['show_thumbnail'] ? $this->t('Yes') : $this->t('No'), '@show_image_thumbnail' => $this->configuration['images']['show_thumbnail'] ? $this->t('Yes') : $this->t('No'),
]); ]);
if ($this->moduleHandler->moduleExists('image') && $this->configuration['images']['show_thumbnail']) { if ($this->moduleHandler->moduleExists('image') && $this->configuration['images']['show_thumbnail']) {
$image_style = ImageStyle::load($this->configuration['images']['thumbnail_image_style']); $image_style = ImageStyle::load($this->configuration['images']['thumbnail_image_style']);
if (!is_null($image_style)) { if (!is_null($image_style)) {
$summery[] = $this->t('Thumbnail style: @thumbnail_style', [ $summary[] = $this->t('Thumbnail style: @thumbnail_style', [
'@thumbnail_style' => $image_style->label(), '@thumbnail_style' => $image_style->label(),
]); ]);
} }
} }
return $summery; return $summary;
} }
/** /**
...@@ -50,6 +56,8 @@ class FileMatcher extends EntityMatcher { ...@@ -50,6 +56,8 @@ class FileMatcher extends EntityMatcher {
*/ */
public function defaultConfiguration() { public function defaultConfiguration() {
return parent::defaultConfiguration() + [ return parent::defaultConfiguration() + [
'extensions' => '',
'status' => FILE_STATUS_PERMANENT,
'images' => [ 'images' => [
'show_dimensions' => FALSE, 'show_dimensions' => FALSE,
'show_thumbnail' => FALSE, 'show_thumbnail' => FALSE,
...@@ -80,6 +88,16 @@ class FileMatcher extends EntityMatcher { ...@@ -80,6 +88,16 @@ class FileMatcher extends EntityMatcher {
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state); $form = parent::buildConfigurationForm($form, $form_state);
$extensions = str_replace(' ', ', ', $this->configuration['extensions']);
$form['extensions'] = [
'#type' => 'textfield',
'#title' => $this->t('Allowed file extensions'),
'#default_value' => $extensions,
'#description' => $this->t('Separate extensions with a space or comma and do not include the leading dot.'),
'#element_validate' => [['\Drupal\file\Plugin\Field\FieldType\FileItem', 'validateExtensions']],
'#maxlength' => 256,
];
$form['images'] = array( $form['images'] = array(
'#type' => 'details', '#type' => 'details',
'#title' => t('Image file settings'), '#title' => t('Image file settings'),
...@@ -122,6 +140,8 @@ class FileMatcher extends EntityMatcher { ...@@ -122,6 +140,8 @@ class FileMatcher extends EntityMatcher {
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state); parent::submitConfigurationForm($form, $form_state);
$this->configuration['extensions'] = $form_state->getValue('extensions');
$values = $form_state->getValue('images'); $values = $form_state->getValue('images');
if (!$values['show_thumbnail']) { if (!$values['show_thumbnail']) {
$values['thumbnail_image_style'] = NULL; $values['thumbnail_image_style'] = NULL;
...@@ -135,7 +155,18 @@ class FileMatcher extends EntityMatcher { ...@@ -135,7 +155,18 @@ class FileMatcher extends EntityMatcher {
*/ */
protected function buildEntityQuery($search_string) { protected function buildEntityQuery($search_string) {
$query = parent::buildEntityQuery($search_string); $query = parent::buildEntityQuery($search_string);
$query->condition('status', FILE_STATUS_PERMANENT); if (isset($this->configuration['status'])) {
$query->condition('status', $this->configuration['status']);
}
if (!empty($this->configuration['extensions'])) {
$extensions = explode(' ', $this->configuration['extensions']);
$group = $query->orConditionGroup();
foreach ($extensions as $extension) {
$group->condition('filename', '%\.' . $this->database->escapeLike($extension), 'LIKE');
}
$query->condition($group);
}
return $query; return $query;
} }
......
<?php
namespace Drupal\linkit\Tests\Matchers;
use Drupal\file\Entity\File;
use Drupal\linkit\Tests\LinkitTestBase;
/**
* Tests file matcher.
*
* @group linkit
*/
class FileMatcherTest extends LinkitTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['file_test', 'file'];
/**
* The matcher manager.
*
* @var \Drupal\linkit\MatcherManager
*/
protected $manager;
/**
* List of images uploaded to test the matcher.
*
* @var array
*/
protected $images = [];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->drupalLogin($this->adminUser);
$this->manager = $this->container->get('plugin.manager.linkit.matcher');
$image_files = $this->drupalGetTestFiles('image');
foreach ($image_files as $file) {
$image = File::create((array) $file);
$this->assertTrue(is_file($image->getFileUri()), "The image file we're going to upload exists.");
// Upload with replace to guarantee there's something there.
$edit = array(
'file_test_replace' => FILE_EXISTS_REPLACE,
'files[file_test_upload]' => drupal_realpath($image->getFileUri()),
);
$this->drupalPostForm('file-test/upload', $edit, t('Submit'));
$this->assertResponse(200, 'Received a 200 response for posted test file.');
$this->images[$file->name] = $image;
}
}
/**
* Tests file matcher.
*/
public function testFileMatcherWithDefaultConfiguration() {
/** @var \Drupal\linkit\MatcherInterface $plugin */
$plugin = $this->manager->createInstance('entity:file', [
'settings' => [
'status' => 0,
],
]);
$matches = $plugin->getMatches('image-test');
$this->assertEqual(6, count($matches), 'Correct number of matches');
}
/**
* Tests file matcher with extension filer.
*/
public function testFileMatcherWithExtensionFiler() {
/** @var \Drupal\linkit\MatcherInterface $plugin */
$plugin = $this->manager->createInstance('entity:file', [
'settings' => [
'extensions' => 'png',
'status' => 0,
],
]);
$matches = $plugin->getMatches('image-test');
$this->assertEqual(1, count($matches), 'Correct number of matches with extension filter');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment