Skip to content

Commit

Permalink
Merge branch 'release/3.0.6'
Browse files Browse the repository at this point in the history
Release/3.0.6
  • Loading branch information
mayurngondhkar committed Aug 22, 2022
2 parents 64ca09c + 08a7763 commit 9eb2296
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 40 deletions.
5 changes: 5 additions & 0 deletions css/embed_media_back_button.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
a#entity-embed-dialog-tide-media-browser-home {
text-decoration: none;
color: #0074bd;
padding: 0 0 0.75em;
}
89 changes: 89 additions & 0 deletions js/embed_media_back_button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* @file
* Adds the back button for embeded media select modal in CKEditor.
* Everytime the user goes to different page within the media embed select modal
* it will save the filter values to retrive the search result when user
* comes back to the first page.
*/

(function ($, Drupal) {
'use strict';

Drupal.behaviors.tide_media_browser_iframe = {
attach: function (context, settings) {
if (!window.CKEDITOR) {
return;
}
let iframePath, itemName, licenseType, mediaType, status, site
const iframeName = 'entity_browser_iframe_tide_media_browser_iframe'
const homePagePath = '/entity-browser/iframe/tide_media_browser_iframe'
const backButton = $('#entity-embed-dialog-tide-media-browser-home')
$(document).ready(function () {
// Remove the filter values on modal first load if any.
localStorage.removeItem('tideMediaBrowserNameFilterVal');
localStorage.removeItem('tideMediaBrowserLicenseTypeFilterVal');
localStorage.removeItem('tideMediaBrowserMediaTypeFilterVal');
localStorage.removeItem('tideMediaBrowserStatusFilterVal');
localStorage.removeItem('tideMediaBrowserSiteFilterVal');
$('iframe').on('load', function() {
if ($('iframe') && $('iframe').length > 0) {
for (let i = 0; i < $('iframe').length; i++) {
if ($('iframe')[i].name && $('iframe')[i].name === iframeName) {
iframePath = $('iframe')[i].contentWindow.location.pathname
// Shows the back button when iframe is not in the first page.
if (iframePath && iframePath !== homePagePath) {
backButton.css('display', 'block');
}
// Hides the back button for the first page.
if (iframePath && iframePath === homePagePath) {
backButton.css('display', 'none');
}
}
}
}
});
});
// Disable back button when it is on the first page.
backButton.click(function() {
history.back()
$('iframe').on('load', function() {
// Get the filter values on iframe load.
itemName = localStorage.getItem('tideMediaBrowserNameFilterVal')
$(this).contents().find('input[name=name]').val(itemName)
licenseType = checkFilterValueInStorage('tideMediaBrowserLicenseTypeFilterVal')
$(this).contents().find('select[name=field_license_type_target_id_1]').val(licenseType)
mediaType = checkFilterValueInStorage('tideMediaBrowserMediaTypeFilterVal')
$(this).contents().find('select[name=bundle]').val(mediaType)
status = checkFilterValueInStorage('tideMediaBrowserStatusFilterVal')
$(this).contents().find('select[name=status]').val(status)
site = checkFilterValueInStorage('tideMediaBrowserSiteFilterVal')
$(this).contents().find('select[name=field_media_site_target_id]').val(site)
// Trigger ajax call submit to retrive the user search for current session.
// Will only trigger if there is any filter value set for this session.
if (checkFilterValuesOnLoad (itemName, licenseType, mediaType, status, site)) {
$(this).contents().find('input[value=Apply]').trigger('click');
}
})
});
// Check filter values on load.
function checkFilterValuesOnLoad (itemName, licenseType, mediaType, status, site) {
const defaultValue = "All"
if ((itemName && itemName !== 'undefined') ||
(licenseType && licenseType !== defaultValue) ||
(mediaType && mediaType !== defaultValue) ||
(status && status !== defaultValue) ||
(site && site !== defaultValue)) {
return true
}
return false
}
// Value check on localStorage, if empty set to default.
function checkFilterValueInStorage (filterValName) {
if (localStorage.getItem(filterValName)) {
return localStorage.getItem(filterValName)
}
return 'All'
}
},
};
}(jQuery, Drupal));
31 changes: 31 additions & 0 deletions js/embed_media_iframe_form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @file
* Retrieve the name user input for embed media select iframe.
* Everytime the user goes to different page within the media embed select modal
* it will save the name field value to retrive the search result when user
* comes back to the first page.
*/

(function ($, Drupal) {
'use strict';

Drupal.behaviors.tide_media_media_form = {
attach: function (context, settings) {
const nameField = $('#views-exposed-form-tide-media-browser-media-browser input[name=name]')
const licenseType = $('#views-exposed-form-tide-media-browser-media-browser select[name=field_license_type_target_id_1]')
const mediaType = $('#views-exposed-form-tide-media-browser-media-browser select[name=bundle]')
const status = $('#views-exposed-form-tide-media-browser-media-browser select[name=status]')
const site = $('#views-exposed-form-tide-media-browser-media-browser select[name=field_media_site_target_id]')
const formSubmit = $('#views-exposed-form-tide-media-browser-media-browser input[type=submit]')
// On every submit it will save the namefield value to the localstorage.
formSubmit.click(function () {
// Store user set values on every click.
localStorage.setItem('tideMediaBrowserNameFilterVal', nameField.val());
localStorage.setItem('tideMediaBrowserLicenseTypeFilterVal', licenseType.val());
localStorage.setItem('tideMediaBrowserMediaTypeFilterVal', mediaType.val());
localStorage.setItem('tideMediaBrowserStatusFilterVal', status.val());
localStorage.setItem('tideMediaBrowserSiteFilterVal', site.val());
})
}
};
}(jQuery, Drupal));
21 changes: 21 additions & 0 deletions tide_media.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,24 @@ media_browser:
css:
theme:
css/media_browser.css: {}

embed_media_back_button:
js:
js/embed_media_back_button.js: {}
dependencies:
- core/drupal
- core/drupalSettings
- core/jquery

embed_media_iframe_form:
js:
js/embed_media_iframe_form.js: {}
dependencies:
- core/drupal
- core/drupalSettings
- core/jquery

embed_media_back_button_style:
css:
theme:
css/embed_media_back_button.css: {}
86 changes: 46 additions & 40 deletions tide_media.module
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Entity\EntityInterface;
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
use Drupal\Core\Field\WidgetBase;

/**
Expand Down Expand Up @@ -109,9 +109,27 @@ function tide_media_form_alter(&$form, FormStateInterface $form_state, $form_id)
}
}
}
// Add check box to show or hide last updated date for embeded media items.

$entity_element = $form_state->get('entity_element');
if ($form_id === 'entity_embed_dialog') {
// Add back button for medie embed modal in CKEditor.
if ($form_state->get('step') == 'select') {
$form['entity-embed-dialog-tide-media-browser-home'] = [
'#title' => t('<< Previous'),
'#type' => 'link',
'#url' => Url::fromRoute('<none>'),
'#weight' => 0,
'#attributes' => [
'onclick' => "return false",
'id' => ["entity-embed-dialog-tide-media-browser-home"],
'style' => "display:none",
],
];
$form['#attached']['library'][] = 'tide_media/embed_media_back_button';
$form['#attached']['library'][] = 'tide_media/embed_media_back_button_style';

}
// Add check box to show or hide last updated date for embeded media items.
if ($form_state->get('step') == 'embed') {
$form['attributes']['data-show-last-updated'] = [
'#type' => 'checkbox',
Expand All @@ -127,16 +145,6 @@ function tide_media_form_alter(&$form, FormStateInterface $form_state, $form_id)
*/
function tide_media_form_media_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['#attached']['library'][] = 'tide_media/media_form';
// Media files and documents only for sanitisation.
$allowed_form_ids = [
"media_file_add_form",
"media_file_edit_form",
"media_document_add_form",
"media_document_edit_form",
];
if (in_array($form_id, $allowed_form_ids)) {
$form['actions']['submit']['#submit'][] = '_tide_media_file_document_submit';
}
}

/**
Expand Down Expand Up @@ -294,6 +302,7 @@ function tide_media_preprocess_image(&$variables) {
function tide_media_views_pre_render(ViewExecutable $view) {
if ($view->id() === 'tide_media_browser' && (($view->current_display === 'media_browser') || ($view->current_display === 'document_browser'))) {
$view->element['#attached']['library'][] = 'tide_media/media_browser';
$view->element['#attached']['library'][] = 'tide_media/embed_media_iframe_form';
foreach ($view->result as $value) {
if (!empty($value->_relationship_entities['field_media_file_target_id'])) {
$file_type = $value->_relationship_entities['field_media_file_target_id'];
Expand Down Expand Up @@ -383,34 +392,6 @@ function tide_media_entity_bundle_field_info_alter(&$fields, EntityTypeInterface
}
}

/**
* Submit handler for sanitise filename.
*/
function _tide_media_file_document_submit($form, FormStateInterface &$form_state) {
$fid = $form_state->getValue('field_media_file');
if (!empty($fid)) {
$file = File::load($fid[0]['fids'][0]);
$filename_current = $file->getFilename();
// Rename filename.
$replacement = '-';
$tide_common_services = \Drupal::service('tide_core.common_services');
$filename_new = $tide_common_services->sanitiseFilename($filename_current, $replacement);
// Rename URI.
$uri = $file->getFileUri();
$new_uri = str_replace($filename_current, $filename_new, $uri);
// Rename filepath.
$stream_wrapper = \Drupal::service('stream_wrapper_manager')->getViaUri($uri);
$dir_path_current = $stream_wrapper->realpath();
$dir_path_new = str_replace($filename_current, $filename_new, $dir_path_current);
rename($dir_path_current, $dir_path_new);
// Complete the file.
$file->setFileUri($new_uri);
$file->setFilename($filename_new);
$file->setPermanent();
$file->save();
}
}

/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*/
Expand All @@ -433,3 +414,28 @@ function tide_media_field_widget_paragraphs_form_alter(&$element, FormStateInter
}
}
}

/**
* Implements hook_ENTITY_TYPE_presave() for file entities.
*/
function tide_media_file_presave(FileInterface $file) {
// Sanitises file names with spaces.
$filename_current = $file->getFilename();
// Rename filename.
$replacement = '-';
// Helper to sanitise spaces in filename.
$tide_common_services = \Drupal::service('tide_core.common_services');
$filename_new = $tide_common_services->sanitiseFilename($filename_current, $replacement);
// Rename URI.
$uri = $file->getFileUri();
$new_uri = str_replace($filename_current, $filename_new, $uri);
// Rename filepath.
$stream_wrapper = \Drupal::service('stream_wrapper_manager')->getViaUri($uri);
$dir_path_current = $stream_wrapper->realpath();
$dir_path_new = str_replace($filename_current, $filename_new, $dir_path_current);
rename($dir_path_current, $dir_path_new);
// Complete the file.
$file->setFileUri($new_uri);
$file->setFilename($filename_new);
$file->setPermanent();
}

0 comments on commit 9eb2296

Please sign in to comment.