Commit e8c6f84c authored by alexpott's avatar alexpott
Browse files

Issue #1791970 by marvil07, drumm: Added Optionally detect tar.* as extension...

Issue #1791970 by marvil07, drumm: Added Optionally detect tar.* as extension instead of * on views file extension field handler.
parent 86bc222b
......@@ -19,10 +19,46 @@
*/
class Extension extends FieldPluginBase {
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['extension_detect_tar'] = array('default' => FALSE, 'bool' => TRUE);
return $options;
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, &$form_state) {
parent::buildOptionsForm($form, $form_state);
$form['extension_detect_tar'] = array(
'#type' => 'checkbox',
'#title' => t('Detect if tar is part of the extension'),
'#description' => t("See if the previous extension is '.tar' and if so, add that, so we see 'tar.gz' or 'tar.bz2' instead of just 'gz'."),
'#default_value' => $this->options['fileextension_max_extension_parts'],
);
}
function render($values) {
$value = $this->get_value($values);
if (preg_match('/\.([^\.]+)$/', $value, $match)) {
return $this->sanitizeValue($match[1]);
if (!$this->options['extension_detect_tar']) {
if (preg_match('/\.([^\.]+)$/', $value, $match)) {
return $this->sanitizeValue($match[1]);
}
}
else {
$file_parts = explode('.', basename($value));
// If there is an extension.
if (count($file_parts) > 1) {
$extension = array_pop($file_parts);
$last_part_in_name = array_pop($file_parts);
if ($last_part_in_name === 'tar') {
$extension = 'tar.' . $extension;
}
return $this->sanitizeValue($extension);
}
}
}
......
<?php
/**
* @file
* Contains \Drupal\file\Tests\Views\ExtensionViewsFieldTest.
*/
namespace Drupal\file\Tests\Views;
use Drupal\views\Tests\ViewUnitTestBase;
use Drupal\views\Tests\ViewTestData;
/**
* Tests the core Drupal\file\Plugin\views\field\Extension handler.
*/
class ExtensionViewsFieldTest extends ViewUnitTestBase {
/**
* {@inheritdoc}
*/
public static $modules = array('file', 'file_test_views');
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = array('file_extension_view');
public static function getInfo() {
return array(
'name' => 'Field: File extension',
'description' => 'Test the core Drupal\file\Plugin\views\field\Extension handler.',
'group' => 'Views Handlers',
);
}
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
ViewTestData::importTestViews(get_class($this), array('file_test_views'));
}
/**
* {@inheritdoc}
*/
protected function dataSet() {
$data = parent::dataSet();
$data[0]['name'] = 'file.png';
$data[1]['name'] = 'file.tar';
$data[2]['name'] = 'file.tar.gz';
$data[3]['name'] = 'file';
return $data;
}
/**
* {@inheritdoc}
*/
protected function viewsData() {
$data = parent::viewsData();
$data['views_test_data']['name']['field']['id'] = 'file_extension';
$data['views_test_data']['name']['real field'] = 'name';
return $data;
}
/**
* Tests file extension views field handler extension_detect_tar option.
*/
public function testFileExtensionTarOption() {
$view = views_get_view('file_extension_view');
$view->setDisplay();
$this->executeView($view);
// Test without the tar option.
$this->assertEqual($view->field['name']->advanced_render($view->result[0]), 'png');
$this->assertEqual($view->field['name']->advanced_render($view->result[1]), 'tar');
$this->assertEqual($view->field['name']->advanced_render($view->result[2]), 'gz');
$this->assertEqual($view->field['name']->advanced_render($view->result[3]), '');
// Test with the tar option.
$view->field['name']->options['extension_detect_tar'] = TRUE;
$this->assertEqual($view->field['name']->advanced_render($view->result[0]), 'png');
$this->assertEqual($view->field['name']->advanced_render($view->result[1]), 'tar');
$this->assertEqual($view->field['name']->advanced_render($view->result[2]), 'tar.gz');
$this->assertEqual($view->field['name']->advanced_render($view->result[3]), '');
}
}
name: 'File test views'
type: module
description: 'Provides default views for views file tests.'
package: Testing
version: VERSION
core: 8.x
dependencies:
- file
- views
hidden: true
base_table: views_test_data
core: '8'
description: ''
status: '1'
display:
default:
display_options:
defaults:
fields: '0'
pager: '0'
pager_options: '0'
sorts: '0'
fields:
age:
field: age
id: age
relationship: none
table: views_test_data
plugin_id: numeric
id:
field: id
id: id
relationship: none
table: views_test_data
plugin_id: numeric
name:
field: name
id: file_extension
relationship: none
table: views_test_data
plugin_id: string
pager:
options:
offset: '0'
type: none
pager_options: { }
sorts:
id:
field: id
id: id
order: ASC
relationship: none
table: views_test_data
plugin_id: numeric
display_plugin: default
display_title: Master
id: default
position: '0'
label: 'Test view for file extension views field handler'
id: file_extension_view
tag: ''
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