Diff
checker
テキスト
テキスト
画像
ドキュメント
Excel
フォルダ
Legal
Enterprise
デスクトップ
料金
ログイン
Diffchecker デスクトップのダウンロード
テキスト比較
2 つのテキスト ファイルの違いを見つける
ツール
履歴
表示
レイアウト
分割
統合
ライブエディター
空白の変更を非表示
未変更行を折りたたむ
折り返しなし
比較精度
スマート
構文
なし
外観を変更
処理
無視
テキスト変換
最初の差分へ移動
入力を編集
Diffchecker Desktop
Diffcheckerを実行する最も安全な方法。Diffchecker Desktopアプリを入手:あなたの差分はコンピューターから出ることはありません!
Desktopを入手
Untitled diff
作成日
12 年前
差分は期限切れになりません
クリア
エクスポート
共有
説明
94 削除
行
合計
削除
文字
合計
削除
この機能を引き続き使用するには、アップグレードしてください
Diff
checker
Pro
価格を見る
376 行
すべてコピー
33 追加
行
合計
追加
文字
合計
追加
この機能を引き続き使用するには、アップグレードしてください
Diff
checker
Pro
価格を見る
321 行
すべてコピー
diff --git a/media_multiselect.js b/media_multiselect.js
diff --git a/media_multiselect.js b/media_multiselect.js
コピー
コピー済み
コピー
コピー済み
index 0523763..
fc48a5d
100644
index 0523763..
bc7c659
100644
--- a/media_multiselect.js
--- a/media_multiselect.js
+++ b/media_multiselect.js
+++ b/media_multiselect.js
@@ -4,35 +4,48 @@
@@ -4,35 +4,48 @@
for (var base in settings.ajax) {
for (var base in settings.ajax) {
var element_settings = settings.ajax[base];
var element_settings = settings.ajax[base];
if (element_settings.event == 'media_select' && !$('#' + base + '.media-multiselect-processed').length) {
if (element_settings.event == 'media_select' && !$('#' + base + '.media-multiselect-processed').length) {
+ $('#' + base).show();
+ $('#' + base).show();
// Bind a click-event to the 'add more' button.
// Bind a click-event to the 'add more' button.
$('#' + base).click((function(element) { return function(event) {
$('#' + base).click((function(element) { return function(event) {
var media_settings = settings.media.multi_select.elements[element];
var media_settings = settings.media.multi_select.elements[element];
コピー
コピー済み
コピー
コピー済み
-
// Add a new beforeSerialize that adds in our fids
// Add a new beforeSerialize that adds in our fids
- Drupal.ajax[element].beforeSerialize = function (element, options) {
- Drupal.ajax[element].beforeSerialize = function (element, options) {
コピー
コピー済み
コピー
コピー済み
+ // Add a new beforeSerialize that adds in our fids.
+ Drupal.ajax[element].beforeSerialize = function(element, options) {
+ Drupal.ajax[element].beforeSerialize = function(element, options) {
// Add the fids to the form_values.
// Add the fids to the form_values.
$(this.media_multiselect_files).each(function() {
$(this.media_multiselect_files).each(function() {
options.data['media_multiselect_fids[' + this.fid + ']'] = this.fid;
options.data['media_multiselect_fids[' + this.fid + ']'] = this.fid;
- })
- })
+ });
+ });
コピー
コピー済み
コピー
コピー済み
Text moved with changes to lines 27-32 (99.8% similarity)
+
// Call the prototype, so we preseve any existing functionality in there.
- Drupal.ajax.prototype.beforeSerialize.call(this, element, options)
- }
-
- var button = this;
+ Drupal.ajax.prototype.beforeSerialize.call(this, element, options);
+ Drupal.ajax.prototype.beforeSerialize.call(this, element, options);
+ };
+ };
+
+
+ var wrapper = $(this).closest('div.form-wrapper');
+ var wrapper = $(this).closest('div.form-wrapper');
コピー
コピー済み
コピー
コピー済み
Text moved with changes from lines 23-28 (99.8% similarity)
- // Call the prototype, so we preseve any existing functionality in there.
- Drupal.ajax.prototype.beforeSerialize.call(this, element, options)
- }
-
- var button = this;
// Launch the Media Browser.
// Launch the Media Browser.
Drupal.media.popups.mediaBrowser(function(files) {
Drupal.media.popups.mediaBrowser(function(files) {
- Drupal.ajax[element].media_multiselect_files = files
- Drupal.ajax[element].media_multiselect_files = files
- $(button).trigger('media_select');
- $(button).trigger('media_select');
+ Drupal.ajax[element].media_multiselect_files = files;
+ Drupal.ajax[element].media_multiselect_files = files;
+ for (var i = 0; i < files.length; i++) {
+ for (var i = 0; i < files.length; i++) {
+ var button = wrapper.find('.multi-browse').first();
+ var button = wrapper.find('.multi-browse').first();
+ var uploadField = button.siblings('.upload');
+ var uploadField = button.siblings('.upload');
+
+
+ // Set the value of the hidden file ID field and trigger a change.
+ // Set the value of the hidden file ID field and trigger a change.
+ uploadField.val(files[i].fid);
+ uploadField.val(files[i].fid);
+ uploadField.trigger('change');
+ uploadField.trigger('change');
+
+
+ // Find the attach button and automatically trigger it.
+ // Find the attach button and automatically trigger it.
+ var attachButton = uploadField.siblings('.attach');
+ var attachButton = uploadField.siblings('.attach');
+ Drupal.ajax[attachButton.attr('id')].options.async = false;
+ Drupal.ajax[attachButton.attr('id')].options.async = false;
+ attachButton.trigger('mousedown');
+ attachButton.trigger('mousedown');
+ }
+ }
}, media_settings.global);
}, media_settings.global);
// Aaaand prevent default.
// Aaaand prevent default.
event.preventDefault();
event.preventDefault();
- }})(base))
- }})(base))
+ }})(base));
+ }})(base));
$('#' + base).addClass('media-multiselect-processed');
$('#' + base).addClass('media-multiselect-processed');
}
}
}
}
}
}
- }
- }
コピー
コピー済み
コピー
コピー済み
-
})(jQuery);
+ };
})(jQuery);
\ No newline at end of file
\ No newline at end of file
コピー
コピー済み
コピー
コピー済み
+ };
+})(jQuery);
diff --git a/media_multiselect.module b/media_multiselect.module
diff --git a/media_multiselect.module b/media_multiselect.module
コピー
コピー済み
コピー
コピー済み
index
1ab4d38..8e498fd
100644
index
6caa609..666acb4
100644
--- a/media_multiselect.module
--- a/media_multiselect.module
+++ b/media_multiselect.module
+++ b/media_multiselect.module
コピー
コピー済み
コピー
コピー済み
@@ -
62
,7 +
62
,7 @@
function media_multiselect_field_widget_settings_form($field, $instance) {
@@ -
84
,7 +
84
,7 @@
function media_multiselect_field_widget_form(&$form, &$form_state, $field, $inst
function media_multiselect_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$form['#entity'] = (!empty($element['#entity'])) ? $element['#entity'] : NULL;
- // We cheat and alter the widget-type to the normal media widget
+ // We cheat and alter the widget-type to the normal media widget.
$instance['widget']['type'] = 'media_generic';
$instance['widget']['module'] = 'media';
@@ -77,13 +77,14 @@
function media_multiselect_field_widget_form(&$form, &$form_state, $field, $inst
$form,
$form_state
);
- } else {
+ }
+ else {
$real_cardinality = $field['cardinality'];
$field['cardinality'] = -1;
}
}
コピー
コピー済み
コピー
コピー済み
-
// Then ask the Field API to generate that widget
// Then ask the Field API to generate that widget
- $element += field_multiple_value_form($field, $instance, $langcode, $items, $form, $form_state);
- $element += field_multiple_value_form($field, $instance, $langcode, $items, $form, $form_state);
コピー
コピー済み
コピー
コピー済み
+ // Then ask the Field API to generate that widget.
+ $element += media_field_widget_form($form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
+ $element += media_field_widget_form($form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
// If we have a != 1, != unlimited field, we have a fixed amount of values
// If we have a != 1, != unlimited field, we have a fixed amount of values
// which we can't ajaxify the 'add-more' button of, as it does not have a
// which we can't ajaxify the 'add-more' button of, as it does not have a
コピー
コピー済み
コピー
コピー済み
@@ -
92,53
+93,59 @@ function media_multiselect_field_widget_form(&$form, &$form_state, $field, $inst
@@ -
93,55
+93,59 @@ function media_multiselect_field_widget_form(&$form, &$form_state, $field, $inst
return $element;
return $element;
}
}
- // Copy over the media-options
- // Copy over the media-options
- $element['#media_options'] = $element[0]['#media_options'];
- $element['#media_options'] = $element[0]['#media_options'];
- $element['#media_options']['global']['multiselect'] = TRUE;
- $element['#media_options']['global']['multiselect'] = TRUE;
コピー
コピー済み
コピー
コピー済み
-
+ $element[$element['#file_upload_delta']]['#media_options']['global']['multiselect'] = TRUE;
+ $element[$element['#file_upload_delta']]['#process'][] = 'media_multiselect_change_add_button';
- do {
- do {
- // Remove the empty placeholder-item at the bottom, and any access items in case
- // Remove the empty placeholder-item at the bottom, and any access items in case
- // we have a limited cardinality field that has too many items chosen!
- // we have a limited cardinality field that has too many items chosen!
- unset($element[$element['#max_delta']]);
- unset($element[$element['#max_delta']]);
- $element['#max_delta'] -= 1;
- $element['#max_delta'] -= 1;
- } while ($real_cardinality != -1 && $element['#max_delta'] >= $real_cardinality);
- } while ($real_cardinality != -1 && $element['#max_delta'] >= $real_cardinality);
-
-
- // Add our JS
- // Add our JS
コピー
コピー済み
コピー
コピー済み
+ $element[$element['#file_upload_delta']]['#media_options']['global']['multiselect'] = TRUE;
+ $element[$element['#file_upload_delta']]['#process'][] = 'media_multiselect_change_add_button';
+
+ // If we no longer have any items, change the theme to avoid an empty table.
+ // If we no longer have any items, change the theme to avoid an empty table.
+ if ($element['#file_upload_delta'] == 0) {
+ if ($element['#file_upload_delta'] == 0) {
+ $element[$element['#file_upload_delta']]['#add_button_title'] = t('Select media');
+ $element[$element['#file_upload_delta']]['#add_button_title'] = t('Select media');
+ }
+ }
+ elseif ($element['#file_upload_delta'] == $real_cardinality - 1) {
+ elseif ($element['#file_upload_delta'] == $real_cardinality - 1) {
+ $element[$element['#file_upload_delta']]['#add_button_access'] = FALSE;
+ $element[$element['#file_upload_delta']]['#add_button_access'] = FALSE;
+ }
+ }
+ else {
+ else {
保存された差分
原文
ファイルを開く
diff --git a/media_multiselect.js b/media_multiselect.js index 0523763..fc48a5d 100644 --- a/media_multiselect.js +++ b/media_multiselect.js @@ -4,35 +4,48 @@ for (var base in settings.ajax) { var element_settings = settings.ajax[base]; if (element_settings.event == 'media_select' && !$('#' + base + '.media-multiselect-processed').length) { + $('#' + base).show(); // Bind a click-event to the 'add more' button. $('#' + base).click((function(element) { return function(event) { var media_settings = settings.media.multi_select.elements[element]; - // Add a new beforeSerialize that adds in our fids - Drupal.ajax[element].beforeSerialize = function (element, options) { + // Add a new beforeSerialize that adds in our fids. + Drupal.ajax[element].beforeSerialize = function(element, options) { // Add the fids to the form_values. $(this.media_multiselect_files).each(function() { options.data['media_multiselect_fids[' + this.fid + ']'] = this.fid; - }) + }); // Call the prototype, so we preseve any existing functionality in there. - Drupal.ajax.prototype.beforeSerialize.call(this, element, options) - } - - var button = this; + Drupal.ajax.prototype.beforeSerialize.call(this, element, options); + }; + + var wrapper = $(this).closest('div.form-wrapper'); // Launch the Media Browser. Drupal.media.popups.mediaBrowser(function(files) { - Drupal.ajax[element].media_multiselect_files = files - $(button).trigger('media_select'); + Drupal.ajax[element].media_multiselect_files = files; + for (var i = 0; i < files.length; i++) { + var button = wrapper.find('.multi-browse').first(); + var uploadField = button.siblings('.upload'); + + // Set the value of the hidden file ID field and trigger a change. + uploadField.val(files[i].fid); + uploadField.trigger('change'); + + // Find the attach button and automatically trigger it. + var attachButton = uploadField.siblings('.attach'); + Drupal.ajax[attachButton.attr('id')].options.async = false; + attachButton.trigger('mousedown'); + } }, media_settings.global); // Aaaand prevent default. event.preventDefault(); - }})(base)) + }})(base)); $('#' + base).addClass('media-multiselect-processed'); } } } - } -})(jQuery); \ No newline at end of file + }; +})(jQuery); diff --git a/media_multiselect.module b/media_multiselect.module index 1ab4d38..8e498fd 100644 --- a/media_multiselect.module +++ b/media_multiselect.module @@ -62,7 +62,7 @@ function media_multiselect_field_widget_settings_form($field, $instance) { function media_multiselect_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { $form['#entity'] = (!empty($element['#entity'])) ? $element['#entity'] : NULL; - // We cheat and alter the widget-type to the normal media widget + // We cheat and alter the widget-type to the normal media widget. $instance['widget']['type'] = 'media_generic'; $instance['widget']['module'] = 'media'; @@ -77,13 +77,14 @@ function media_multiselect_field_widget_form(&$form, &$form_state, $field, $inst $form, $form_state ); - } else { + } + else { $real_cardinality = $field['cardinality']; $field['cardinality'] = -1; } - // Then ask the Field API to generate that widget - $element += field_multiple_value_form($field, $instance, $langcode, $items, $form, $form_state); + // Then ask the Field API to generate that widget. + $element += media_field_widget_form($form, $form_state, $field, $instance, $langcode, $items, $delta, $element); // If we have a != 1, != unlimited field, we have a fixed amount of values // which we can't ajaxify the 'add-more' button of, as it does not have a @@ -92,53 +93,59 @@ function media_multiselect_field_widget_form(&$form, &$form_state, $field, $inst return $element; } - // Copy over the media-options - $element['#media_options'] = $element[0]['#media_options']; - $element['#media_options']['global']['multiselect'] = TRUE; - - do { - // Remove the empty placeholder-item at the bottom, and any access items in case - // we have a limited cardinality field that has too many items chosen! - unset($element[$element['#max_delta']]); - $element['#max_delta'] -= 1; - } while ($real_cardinality != -1 && $element['#max_delta'] >= $real_cardinality); - - // Add our JS + $element[$element['#file_upload_delta']]['#media_options']['global']['multiselect'] = TRUE; + $element[$element['#file_upload_delta']]['#process'][] = 'media_multiselect_change_add_button'; + + // If we no longer have any items, change the theme to avoid an empty table. + if ($element['#file_upload_delta'] == 0) { + $element[$element['#file_upload_delta']]['#add_button_title'] = t('Select media'); + } + elseif ($element['#file_upload_delta'] == $real_cardinality - 1) { + $element[$element['#file_upload_delta']]['#add_button_access'] = FALSE; + } + else { + $element[$element['#file_upload_delta']]['#add_button_title'] = t('Add media'); + } + + // Add our JS. $element['#attached'] = array( 'js' => array( drupal_get_path('module', 'media_multiselect') . '/media_multiselect.js', ), ); - // Attach the browser JS (this is normally done by the media-elements, - // but on empty nodes, there arent any media-elements, as we unset the - // empty placeholder) - module_load_include('inc', 'media', 'includes/media.browser'); - media_attach_browser_js($element); - $element['#after_build'][] = 'media_multiselect_add_js_settings'; - - // Change the AJAX info for the add-more button. - $element['add_more']['#ajax']['event'] = 'media_select'; - $element['add_more']['#ajax']['callback'] = 'media_multiselect_ajax_callback'; - $element['add_more']['#submit'][0] = 'media_multiselect_ajax_submit'; - $element['add_more']['#media_multiselect_value_handler'] = 'media_multiselect_add_more_value_handler'; - - // If we now no longer has any items, change the theme to not get the - // empty table - if ($element['#max_delta'] < 0) { - $element['add_more']['#value'] = t('Select media'); - $element['#theme'] = 'media_multiselect_empty_field'; - } else if ($element['#max_delta'] == $real_cardinality -1 ) { - $element['add_more']['#access'] = FALSE; - } else { - $element['add_more']['#value'] = t('Add media'); + + $element['#process'][] = 'media_multiselect_process_widget_form'; + + return $element; +} + +/** + * Change the AJAX info for the add-more button. + */ +function media_multiselect_change_add_button($element) { + $button = $element['browse_button']; + + $button['#ajax']['event'] = 'media_select'; + $button['#ajax']['callback'] = 'media_multiselect_ajax_callback'; + $button['#submit'][0] = 'media_multiselect_ajax_submit'; + $button['#media_multiselect_value_handler'] = 'media_multiselect_add_more_value_handler'; + + if (isset($element['#add_button_access']) && $element['#add_button_access'] === FALSE) { + $button['#access'] = FALSE; + } + else { + $button['#title'] = $element['#add_button_title']; } - $element['#process'] = array( - 'media_multiselect_process_widget_form', - ); - + // Remove 'browse' class because media module will overwrite click events for + // the buttons with this class. + $button['#attributes']['class'] = array_values(array_diff($button['#attributes']['class'], array('browse'))); + $button['#attributes']['class'][] = 'multi-browse'; + + $element['browse_button'] = $button; + return $element; } @@ -148,66 +155,43 @@ function media_multiselect_field_widget_form(&$form, &$form_state, $field, $inst * of the value-elements with their own #process-instruction. */ function media_multiselect_process_widget_form($element) { - $element['add_more']['#media_multiselect_field_wrapper'] = $element['#array_parents']; + $element[$element['#file_upload_delta']]['browse_button']['#media_multiselect_field_wrapper'] = $element['#array_parents']; - preg_match('/id="(.*?)"/', $element['#prefix'], $matches); - list($_, $wrapper_id) = $matches; - for ($i = 0; $i <= $element['#max_delta']; $i++) { + for ($i = 0; $i <= $element['#file_upload_delta']; $i++) { $element[$i] += element_info('media'); - $element[$i]['#wrapper_id'] = $wrapper_id; - $element[$i]['#process'][] = 'media_multiselect_improve_remove_button'; $element[$i]['#media_multiselect_field_wrapper'] = $element['#array_parents']; } - return $element; -} - -/** - * #process-callback for each value-element - * Alters the remove-button to be a ajax-submit instead, so we can remove the - * element from the form, instead of just blanking it. - */ -function media_multiselect_improve_remove_button($element) { - $element['remove'] = array( - '#type' => 'submit', - '#value' => t('Remove media'), - '#weight' => $element['remove']['#weight'], - '#submit' => array('media_multiselect_ajax_submit'), - '#media_multiselect_value_handler' => 'media_multiselect_remove_button_value_handler', - '#name' => $element['#field_name'] . '_remove_' . $element['#delta'], - '#ajax' => array( - 'callback' => 'media_multiselect_ajax_callback', - 'wrapper' => $element['#wrapper_id'], - ), - '#media_multiselect_field_wrapper' => array_slice($element['#array_parents'], 0, -1), - ); + // Prefix and suffix used for Ajax replacement. + $element['#prefix'] = '<div id="' . $element['#id'] . '-ajax-wrapper">'; + $element['#suffix'] = '</div>'; return $element; } /** - * Element-process callback. See @media_multiselect_field_widget_form + * Element-process callback. See @media_multiselect_change_add_button */ function media_multiselect_add_js_settings($element) { static $processed = array(); - + // Add the media-options so the add-more button has the correct options for // opening the dialog. // We use a static variable to prevent we add the settings more than once - $id = $element['add_more']['#id']; + $id = $element[$element['#file_upload_delta']]['browse_button']['#id']; if (empty($processed[$id])) { drupal_add_js(array( 'media' => array( 'multi_select' => array( 'elements' => array( - $id => $element['#media_options'], + $id => $element[$element['#file_upload_delta']]['#media_options'], ), ), ), ), 'setting'); $processed[$id] = TRUE; } - + return $element; } @@ -216,7 +200,7 @@ function media_multiselect_add_js_settings($element) { */ function theme_media_multiselect_empty_field($vars) { $element = $vars['element']; - + $attributes['class'] = array('form-item'); if (!empty($element['#type'])) { $attributes['class'][] = 'form-type-' . strtr($element['#type'], '_', '-'); @@ -231,15 +215,14 @@ function theme_media_multiselect_empty_field($vars) { $output = '<div' . drupal_attributes($attributes) . '>' . "\n"; $output .= theme('form_element_label', $element); $output .= '<div class="description">' . $element['#description'] . "</div>\n"; - $output .= '<div class="clearfix">' . drupal_render($element['add_more']) . '</div>'; + $output .= '<div class="clearfix">' . drupal_render($element[$element['#file_upload_delta']]['browse_button']) . '</div>'; $output .= "</div>\n"; - + return $output; } /** - * AJAX callback. See @media_multiselect_field_widget_form and - * @media_multiselect_improve_remove_button + * AJAX callback. See @media_multiselect_change_add_button * * Returns the element in the form identified by the path in the * "#media_multiselect_field_wrapper"-property @@ -272,11 +255,10 @@ function media_multiselect_ajax_callback($form, $form_state) { } /** - * AJAX add-more button #submit. See @media_multiselect_field_widget_form and - * @media_multiselect_improve_remove_button. + * AJAX add-more button #submit. See @media_multiselect_change_add_button * * Finds the current value of the field identified by #media_multiselect_field_wrapper, - * passes it to the value-handler identified by #media_multiselect_value_handler, and + * passes it to the value-handler identified by #media_multiselect_value_handler, and * then saves the value back to the field and asks to rebuild the form. */ function media_multiselect_ajax_submit($form, &$form_state) { @@ -287,15 +269,12 @@ function media_multiselect_ajax_submit($form, &$form_state) { // Go one level up in the form, to the widgets container. $element = drupal_array_get_nested_value($form, $button['#media_multiselect_field_wrapper']); $value = drupal_array_get_nested_value($form_state['values'], $button['#media_multiselect_field_wrapper']); - - // Some convinient short-hand variables + + // Some convenient short-hand variables. $field_name = $element['#field_name']; $langcode = $element['#language']; $parents = $element['#field_parents']; $field_state = field_form_get_state($parents, $field_name, $langcode, $form_state); - - // Remove the 'add more' value, because its not a file. - unset($value['add_more']); // Filter the existing values so only fid > 0 remains. $value = array_filter($value, '_media_multiselect_filter_empty_value'); @@ -311,7 +290,7 @@ function media_multiselect_ajax_submit($form, &$form_state) { // Update the Field State to the new amount of items $field_state['items_count'] = count($value); field_form_set_state($parents, $field_name, $langcode, $form_state, $field_state); - + // Update the Form State to make it seem like it received the new files as an input. drupal_array_set_nested_value($form_state['input'], $button['#media_multiselect_field_wrapper'], $value); @@ -328,10 +307,11 @@ function media_multiselect_add_more_value_handler($form, $form_state, $value) { if (!empty($value)) { $last_entry = end($value); $max_weight = $last_entry['_weight']; - } else { + } + else { $max_weight = -1; } - + if (!empty($form_state['input']['media_multiselect_fids'])) { // Add the selected fids to the list of values foreach ($form_state['input']['media_multiselect_fids'] AS $fid) { @@ -345,20 +325,6 @@ function media_multiselect_add_more_value_handler($form, $form_state, $value) { } /** - * #media_multiselect_value_handler-callback for the 'remove'-buttons. - * Finds the delta of the pushed button, and removes that from the $value-array. - */ -function media_multiselect_remove_button_value_handler($form, $form_state, $value) { - $button = $form_state['triggering_element']; - $delta = $button['#array_parents'][count($button['#array_parents'])-2]; - - // Remove the unwanted delta.. - unset($value[$delta]); - - return $value; -} - -/** * Helper function to remove empty file values (fid=0) */ function _media_multiselect_filter_empty_value($value) {
変更されたテキスト
ファイルを開く
diff --git a/media_multiselect.js b/media_multiselect.js index 0523763..bc7c659 100644 --- a/media_multiselect.js +++ b/media_multiselect.js @@ -4,35 +4,48 @@ for (var base in settings.ajax) { var element_settings = settings.ajax[base]; if (element_settings.event == 'media_select' && !$('#' + base + '.media-multiselect-processed').length) { + $('#' + base).show(); // Bind a click-event to the 'add more' button. $('#' + base).click((function(element) { return function(event) { var media_settings = settings.media.multi_select.elements[element]; // Add a new beforeSerialize that adds in our fids - Drupal.ajax[element].beforeSerialize = function (element, options) { + Drupal.ajax[element].beforeSerialize = function(element, options) { // Add the fids to the form_values. $(this.media_multiselect_files).each(function() { options.data['media_multiselect_fids[' + this.fid + ']'] = this.fid; - }) + }); + + Drupal.ajax.prototype.beforeSerialize.call(this, element, options); + }; + + var wrapper = $(this).closest('div.form-wrapper'); - // Call the prototype, so we preseve any existing functionality in there. - Drupal.ajax.prototype.beforeSerialize.call(this, element, options) - } - - var button = this; // Launch the Media Browser. Drupal.media.popups.mediaBrowser(function(files) { - Drupal.ajax[element].media_multiselect_files = files - $(button).trigger('media_select'); + Drupal.ajax[element].media_multiselect_files = files; + for (var i = 0; i < files.length; i++) { + var button = wrapper.find('.multi-browse').first(); + var uploadField = button.siblings('.upload'); + + // Set the value of the hidden file ID field and trigger a change. + uploadField.val(files[i].fid); + uploadField.trigger('change'); + + // Find the attach button and automatically trigger it. + var attachButton = uploadField.siblings('.attach'); + Drupal.ajax[attachButton.attr('id')].options.async = false; + attachButton.trigger('mousedown'); + } }, media_settings.global); // Aaaand prevent default. event.preventDefault(); - }})(base)) + }})(base)); $('#' + base).addClass('media-multiselect-processed'); } } } - } + }; })(jQuery); \ No newline at end of file diff --git a/media_multiselect.module b/media_multiselect.module index 6caa609..666acb4 100644 --- a/media_multiselect.module +++ b/media_multiselect.module @@ -84,7 +84,7 @@ function media_multiselect_field_widget_form(&$form, &$form_state, $field, $inst } // Then ask the Field API to generate that widget - $element += field_multiple_value_form($field, $instance, $langcode, $items, $form, $form_state); + $element += media_field_widget_form($form, $form_state, $field, $instance, $langcode, $items, $delta, $element); // If we have a != 1, != unlimited field, we have a fixed amount of values // which we can't ajaxify the 'add-more' button of, as it does not have a @@ -93,55 +93,59 @@ function media_multiselect_field_widget_form(&$form, &$form_state, $field, $inst return $element; } - // Copy over the media-options - $element['#media_options'] = $element[0]['#media_options']; - $element['#media_options']['global']['multiselect'] = TRUE; + $element[$element['#file_upload_delta']]['#media_options']['global']['multiselect'] = TRUE; + $element[$element['#file_upload_delta']]['#process'][] = 'media_multiselect_change_add_button'; - do { - // Remove the empty placeholder-item at the bottom, and any access items in case - // we have a limited cardinality field that has too many items chosen! - unset($element[$element['#max_delta']]); - $element['#max_delta'] -= 1; - } while ($real_cardinality != -1 && $element['#max_delta'] >= $real_cardinality); - - // Add our JS + // If we no longer have any items, change the theme to avoid an empty table. + if ($element['#file_upload_delta'] == 0) { + $element[$element['#file_upload_delta']]['#add_button_title'] = t('Select media'); + } + elseif ($element['#file_upload_delta'] == $real_cardinality - 1) { + $element[$element['#file_upload_delta']]['#add_button_access'] = FALSE; + } + else { + $element[$element['#file_upload_delta']]['#add_button_title'] = t('Add media'); + } + + // Add our JS. $element['#attached'] = array( 'js' => array( drupal_get_path('module', 'media_multiselect') . '/media_multiselect.js', ), ); - // Attach the browser JS (this is normally done by the media-elements, - // but on empty nodes, there arent any media-elements, as we unset the - // empty placeholder) - module_load_include('inc', 'media', 'includes/media.browser'); - media_attach_browser_js($element); - $element['#after_build'][] = 'media_multiselect_add_js_settings'; - - // Change the AJAX info for the add-more button. - $element['add_more']['#ajax']['event'] = 'media_select'; - $element['add_more']['#ajax']['callback'] = 'media_multiselect_ajax_callback'; - $element['add_more']['#submit'][0] = 'media_multiselect_ajax_submit'; - $element['add_more']['#media_multiselect_value_handler'] = 'media_multiselect_add_more_value_handler'; - - // If we now no longer has any items, change the theme to not get the - // empty table - if ($element['#max_delta'] < 0) { - $element['add_more']['#value'] = t('Select'); - $element['#theme'] = 'media_multiselect_empty_field'; - } - elseif ($element['#max_delta'] == $real_cardinality -1 ) { - $element['add_more']['#access'] = FALSE; + + $element['#process'][] = 'media_multiselect_process_widget_form'; + + return $element; +} + +/** + * Change the AJAX info for the add-more button. + */ +function media_multiselect_change_add_button($element) { + $button = $element['browse_button']; + + $button['#ajax']['event'] = 'media_select'; + $button['#ajax']['callback'] = 'media_multiselect_ajax_callback'; + $button['#submit'][0] = 'media_multiselect_ajax_submit'; + $button['#media_multiselect_value_handler'] = 'media_multiselect_add_more_value_handler'; + + if (isset($element['#add_button_access']) && $element['#add_button_access'] === FALSE) { + $button['#access'] = FALSE; } else { - $element['add_more']['#value'] = t('Add'); + $button['#title'] = $element['#add_button_title']; } - $element['#process'] = array( - 'media_multiselect_process_widget_form', - ); - + // Remove 'browse' class because media module will overwrite click events for + // the buttons with this class. + $button['#attributes']['class'] = array_values(array_diff($button['#attributes']['class'], array('browse'))); + $button['#attributes']['class'][] = 'multi-browse'; + + $element['browse_button'] = $button; + return $element; } @@ -151,66 +155,43 @@ function media_multiselect_field_widget_form(&$form, &$form_state, $field, $inst * of the value-elements with their own #process-instruction. */ function media_multiselect_process_widget_form($element) { - $element['add_more']['#media_multiselect_field_wrapper'] = $element['#array_parents']; + $element[$element['#file_upload_delta']]['browse_button']['#media_multiselect_field_wrapper'] = $element['#array_parents']; - preg_match('/id="(.*?)"/', $element['#prefix'], $matches); - list($_, $wrapper_id) = $matches; - for ($i = 0; $i <= $element['#max_delta']; $i++) { +for ($i = 0; $i <= $element['#file_upload_delta']; $i++) { $element[$i] += element_info('media'); - $element[$i]['#wrapper_id'] = $wrapper_id; - $element[$i]['#process'][] = 'media_multiselect_improve_remove_button'; $element[$i]['#media_multiselect_field_wrapper'] = $element['#array_parents']; } - return $element; -} - -/** - * #process-callback for each value-element - * Alters the remove-button to be a ajax-submit instead, so we can remove the - * element from the form, instead of just blanking it. - */ -function media_multiselect_improve_remove_button($element) { - $element['remove'] = array( - '#type' => 'submit', - '#value' => t('Remove'), - '#weight' => $element['remove']['#weight'], - '#submit' => array('media_multiselect_ajax_submit'), - '#media_multiselect_value_handler' => 'media_multiselect_remove_button_value_handler', - '#name' => $element['#field_name'] . '_remove_' . $element['#delta'], - '#ajax' => array( - 'callback' => 'media_multiselect_ajax_callback', - 'wrapper' => $element['#wrapper_id'], - ), - '#media_multiselect_field_wrapper' => array_slice($element['#array_parents'], 0, -1), - ); + // Prefix and suffix used for Ajax replacement. + $element['#prefix'] = '<div id="' . $element['#id'] . '-ajax-wrapper">'; + $element['#suffix'] = '</div>'; return $element; } /** - * Element-process callback. See @media_multiselect_field_widget_form + * Element-process callback. See @media_multiselect_change_add_button */ function media_multiselect_add_js_settings($element) { static $processed = array(); - + // Add the media-options so the add-more button has the correct options for // opening the dialog. // We use a static variable to prevent we add the settings more than once - $id = $element['add_more']['#id']; + $id = $element[$element['#file_upload_delta']]['browse_button']['#id']; if (empty($processed[$id])) { drupal_add_js(array( 'media' => array( 'multi_select' => array( 'elements' => array( - $id => $element['#media_options'], + $id => $element[$element['#file_upload_delta']]['#media_options'], ), ), ), ), 'setting'); $processed[$id] = TRUE; } - + return $element; } @@ -219,7 +200,7 @@ function media_multiselect_add_js_settings($element) { */ function theme_media_multiselect_empty_field($vars) { $element = $vars['element']; - + $attributes['class'] = array('form-item'); if (!empty($element['#type'])) { $attributes['class'][] = 'form-type-' . strtr($element['#type'], '_', '-'); @@ -234,15 +215,14 @@ function theme_media_multiselect_empty_field($vars) { $output = '<div' . drupal_attributes($attributes) . '>' . "\n"; $output .= theme('form_element_label', $element); $output .= '<div class="description">' . $element['#description'] . "</div>\n"; - $output .= '<div class="clearfix">' . drupal_render($element['add_more']) . '</div>'; + $output .= '<div class="clearfix">' . drupal_render($element[$element['#file_upload_delta']]['browse_button']) . '</div>'; $output .= "</div>\n"; - + return $output; } /** - * AJAX callback. See @media_multiselect_field_widget_form and - * @media_multiselect_improve_remove_button + * AJAX callback. See @media_multiselect_change_add_button * * Returns the element in the form identified by the path in the * "#media_multiselect_field_wrapper"-property @@ -275,8 +255,7 @@ function media_multiselect_ajax_callback($form, $form_state) { } /** - * AJAX add-more button #submit. See @media_multiselect_field_widget_form and - * @media_multiselect_improve_remove_button. + * AJAX add-more button #submit. See @media_multiselect_change_add_button * * Finds the current value of the field identified by #media_multiselect_field_wrapper, * passes it to the value-handler identified by #media_multiselect_value_handler, and @@ -296,9 +275,6 @@ function media_multiselect_ajax_submit($form, &$form_state) { $langcode = $element['#language']; $parents = $element['#field_parents']; $field_state = field_form_get_state($parents, $field_name, $langcode, $form_state); - - // Remove the 'add more' value, because its not a file. - unset($value['add_more']); // Filter the existing values so only fid > 0 remains. $value = array_filter($value, '_media_multiselect_filter_empty_value'); @@ -349,20 +325,6 @@ function media_multiselect_add_more_value_handler($form, $form_state, $value) { } /** - * #media_multiselect_value_handler-callback for the 'remove'-buttons. - * Finds the delta of the pushed button, and removes that from the $value-array. - */ -function media_multiselect_remove_button_value_handler($form, $form_state, $value) { - $button = $form_state['triggering_element']; - $delta = $button['#array_parents'][count($button['#array_parents'])-2]; - - // Remove the unwanted delta.. - unset($value[$delta]); - - return $value; -} - -/** * Helper function to remove empty file values (fid=0) */ function _media_multiselect_filter_empty_value($value) {
違いを見つける