Commit f3d8104b authored by anon's avatar anon

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

This reverts commit 15045e6c.
parent 15045e6c
...@@ -23,32 +23,26 @@ class FileMatcher extends EntityMatcher { ...@@ -23,32 +23,26 @@ class FileMatcher extends EntityMatcher {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getSummary() { public function getSummary() {
$summary = parent::getSummary(); $summery = parent::getSummary();
if (!empty($this->configuration['extensions'])) { $summery[] = $this->t('Show image dimensions: @show_image_dimensions', [
$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'),
]); ]);
$summary[] = $this->t('Show image thumbnail: @show_image_thumbnail', [ $summery[] = $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)) {
$summary[] = $this->t('Thumbnail style: @thumbnail_style', [ $summery[] = $this->t('Thumbnail style: @thumbnail_style', [
'@thumbnail_style' => $image_style->label(), '@thumbnail_style' => $image_style->label(),
]); ]);
} }
} }
return $summary; return $summery;
} }
/** /**
...@@ -56,8 +50,6 @@ class FileMatcher extends EntityMatcher { ...@@ -56,8 +50,6 @@ 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,
...@@ -88,16 +80,6 @@ class FileMatcher extends EntityMatcher { ...@@ -88,16 +80,6 @@ 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'),
...@@ -140,8 +122,6 @@ class FileMatcher extends EntityMatcher { ...@@ -140,8 +122,6 @@ 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;
...@@ -155,18 +135,7 @@ class FileMatcher extends EntityMatcher { ...@@ -155,18 +135,7 @@ class FileMatcher extends EntityMatcher {
*/ */
protected function buildEntityQuery($search_string) { protected function buildEntityQuery($search_string) {
$query = parent::buildEntityQuery($search_string); $query = parent::buildEntityQuery($search_string);
if (isset($this->configuration['status'])) { $query->condition('status', FILE_STATUS_PERMANENT);
$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