From b67a1d0b23b1404b585c5fd20d4dbc044a7e37ba Mon Sep 17 00:00:00 2001 From: Marco Villegas Date: Fri, 12 Apr 2013 17:55:33 -0500 Subject: [PATCH] Issue #1791970 by drumm, marvil07: Add an option to detect tar.gz as extension instead of gz on views file extension field handler. --- .../Drupal/file/Plugin/views/field/Extension.php | 42 ++++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/core/modules/file/lib/Drupal/file/Plugin/views/field/Extension.php b/core/modules/file/lib/Drupal/file/Plugin/views/field/Extension.php index 376793f..9822cce 100644 --- a/core/modules/file/lib/Drupal/file/Plugin/views/field/Extension.php +++ b/core/modules/file/lib/Drupal/file/Plugin/views/field/Extension.php @@ -19,10 +19,46 @@ */ class Extension extends FieldPluginBase { + protected function defineOptions() { + $options = parent::defineOptions(); + $options['fileextension_detect_tar'] = array('default' => FALSE, 'bool' => TRUE); + return $options; + } + + /** + * Default options form that provides the label widget that all fields + * should have. + */ + public function buildOptionsForm(&$form, &$form_state) { + parent::buildOptionsForm($form, $form_state); + $form['fileextension_detect_tar'] = array( + '#type' => 'checkbox', + '#title' => t('Detect if tar is part of the extension'), + '#description' => t('Detect multiple tar extensions like tar.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['fileextension_detect_tar']) { + $value = $this->get_value($values); + if (preg_match('/\.([^\.]+)$/', $value, $match)) { + return $this->sanitizeValue($match[1]); + } + } + else { + $file_parts = explode('.', basename($this->get_value($values))); + // If there is an extension. + if (count($file_parts) > 1) { + $extension = array_pop($file_parts); + // See if the previous extension is '.tar' and if so, add that, so we + // see 'tar.gz' or 'tar.bz2' instead of just 'gz'. + $last_part_in_name = array_pop($file_parts); + if ($last_part_in_name === 'tar') { + $extension = $last_part_in_name . '.' . $extension; + } + return $this->sanitizeValue($extension); + } } } -- 1.7.10.4