Diff
checker
Texto
Texto
Imágenes
Documentos
Excel
Carpetas
Legal
Enterprise
Aplicación de escritorio
Precios
Iniciar sesión
Descargar Diffchecker Desktop
Comparar texto
Encuentra la diferencia entre dos archivos de texto
Herramientas
Historial
Editor live
Ocultar espacios en blanco
Ocultar sin cambios
Sin ajuste de línea
Vista
Dividido
Unificado
Nivel de detalle
Inteligente
Palabra
Letra
Estilos de texto
Cambiar apariencia
Resaltado de sintaxis
Elegir sintaxis
Ignorar
Transformar texto
Ir al primer cambio
Editar entrada
Diffchecker Desktop
La forma más segura de usar Diffchecker. ¡Obtén la app de Diffchecker Desktop: tus diffs nunca salen de tu computadora!
Obtener Desktop
Untitled diff
Creado
hace 11 años
El diff nunca expira
Borrar
Exportar
Compartir
Explicar
93 eliminaciones
Líneas
Total
Eliminado
Caracteres
Total
Eliminado
Para continuar usando esta función, actualice a
Diff
checker
Pro
Ver precios
376 líneas
Copiar todo
32 adiciones
Líneas
Total
Añadido
Caracteres
Total
Añadido
Para continuar usando esta función, actualice a
Diff
checker
Pro
Ver precios
321 líneas
Copiar todo
diff --git a/media_multiselect.js b/media_multiselect.js
diff --git a/media_multiselect.js b/media_multiselect.js
Copiar
Copiado
Copiar
Copiado
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];
Copiar
Copiado
Copiar
Copiado
-
// 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) {
Copiar
Copiado
Copiar
Copiado
+ // 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;
- })
- })
+ });
+ });
Copiar
Copiado
Copiar
Copiado
Text moved with changes from lines 29-32 (99.1% similarity)
+
+ Drupal.ajax.prototype.beforeSerialize.call(this, element, options);
+ };
+
+ var wrapper = $(this).closest('div.form-wrapper');
Copiar
Copiado
Copiar
Copiado
// Call the prototype, so we preseve any existing functionality in there.
-
// Call the prototype, so we preseve any existing functionality in there.
- Drupal.ajax.prototype.beforeSerialize.call(this, element, options)
- Drupal.ajax.prototype.beforeSerialize.call(this, element, options)
- }
- }
-
-
- var button = this;
- var button = this;
Copiar
Copiado
Copiar
Copiado
Text moved with changes to lines 22-26 (99.1% similarity)
+ Drupal.ajax.prototype.beforeSerialize.call(this, element, options);
+ };
+
+ var wrapper = $(this).closest('div.form-wrapper');
// 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');
}
}
}
}
}
}
- }
- }
Copiar
Copiado
Copiar
Copiado
-
})(jQuery);
+ };
})(jQuery);
\ No newline at end of file
\ No newline at end of file
Copiar
Copiado
Copiar
Copiado
+ };
+})(jQuery);
diff --git a/media_multiselect.module b/media_multiselect.module
diff --git a/media_multiselect.module b/media_multiselect.module
Copiar
Copiado
Copiar
Copiado
index
1ab4d38..8e498fd
100644
index
6caa609..666acb4
100644
--- a/media_multiselect.module
--- a/media_multiselect.module
+++ b/media_multiselect.module
+++ b/media_multiselect.module
Copiar
Copiado
Copiar
Copiado
@@ -
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;
}
}
Copiar
Copiado
Copiar
Copiado
-
// 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);
Copiar
Copiado
Copiar
Copiado
+ // 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
Copiar
Copiado
Copiar
Copiado
@@ -
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;
Copiar
Copiado
Copiar
Copiado
-
+ $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
Copiar
Copiado
Copiar
Copiado
+ $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 {
+ $element[$element['#file_upload_delta']]['#add_button_title'] = t('Add media');
+ $element[$element['#file_upload_delta']]['#add_button_title'] = t('Add media');
+ }
+ }
+
+
+ // Add our JS.
+ // Add our JS.
$element['#attached'] = array(
$element['#attached'] = array(
'js' => array(
'js' => array(
drupal_get_path('module', 'media_multiselect') . '/media_multiselect.js',
drupal_get_path('module', 'media_multiselect') . '/media_multiselect.js',
),
),
);
);
- // Attach the browser JS (this is normally done by the media-elements,
- // 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
- // but on empty nodes, there arent any media-elements, as we unset the
- // empty placeholder)
- // empty placeholder)
- module_load_include('inc', 'media', 'includes/media.browser');
- module_load_include('inc', 'media', 'includes/media.browser');
- media_attach_browser_js($element);
- media_attach_browser_js($element);
-
-
$element['#after_build'][] = 'media_multiselect_add_js_settings';
$element['#after_build'][] = 'media_multiselect_add_js_settings';
-
-
- // Change the AJAX info for the add-more button.
- // Change the AJAX info for the add-more button.
- $element['add_more']['#ajax']['event'] = 'media_select';
- $element['add_more']['#ajax']['event'] = 'media_select';
- $element['add_more']['#ajax']['callback'] = 'media_multiselect_ajax_callback';
- $element['add_more']['#ajax']['callback'] = 'media_multiselect_ajax_callback';
- $element['add_more']['#submit'][0] = 'media_multiselect_ajax_submit';
- $element['add_more']['#submit'][0] = 'media_multiselect_ajax_submit';
- $element['add_more']['#media_multiselect_value_handler'] = 'media_multiselect_add_more_value_handler';
- $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
- // If we now no longer has any items, change the theme to not get the
- // empty table
- // empty table
- if ($element['#max_delta'] < 0) {
- if ($element['#max_delta'] < 0) {
Copiar
Copiado
Copiar
Copiado
- $element['add_more']['#value'] = t('Select
media
');
- $element['add_more']['#value'] = t('Select
');
- $element['#theme'] = 'media_multiselect_empty_field';
- $element['#theme'] = 'media_multiselect_empty_field';
Copiar
Copiado
Copiar
Copiado
- }
else
if ($element['#max_delta'] == $real_cardinality -1 ) {
- }
-
else
if ($element['#max_delta'] == $real_cardinality -1 ) {
- $element['add_more']['#access'] = FALSE;
- $element['add_more']['#access'] = FALSE;
Copiar
Copiado
Copiar
Copiado
- } else {
- $element['add_more']['#value'] = t('Add media');
+
+
+ $element['#process'][] = 'media_multiselect_process_widget_form';
+ $element['#process'][] = 'media_multiselect_process_widget_form';
+
+
+ return $element;
+ return $element;
+}
+}
+
+
+/**
+/**
+ * Change the AJAX info for the add-more button.
+ * Change the AJAX info for the add-more button.
+ */
+ */
+function media_multiselect_change_add_button($element) {
+function media_multiselect_change_add_button($element) {
+ $button = $element['browse_button'];
+ $button = $element['browse_button'];
+
+
+ $button['#ajax']['event'] = 'media_select';
+ $button['#ajax']['event'] = 'media_select';
+ $button['#ajax']['callback'] = 'media_multiselect_ajax_callback';
+ $button['#ajax']['callback'] = 'media_multiselect_ajax_callback';
+ $button['#submit'][0] = 'media_multiselect_ajax_submit';
+ $button['#submit'][0] = 'media_multiselect_ajax_submit';
+ $button['#media_multiselect_value_handler'] = 'media_multiselect_add_more_value_handler';
+ $button['#media_multiselect_value_handler'] = 'media_multiselect_add_more_value_handler';
+
+
+ if (isset($element['#add_button_access']) && $element['#add_button_access'] === FALSE) {
+ if (isset($element['#add_button_access']) && $element['#add_button_access'] === FALSE) {
+ $button['#access'] = FALSE;
+ $button['#access'] = FALSE;
Copiar
Copiado
Copiar
Copiado
+
}
}
+
else {
else {
- $element['add_more']['#value'] = t('Add');
+ $button['#title'] = $element['#add_button_title'];
+ $button['#title'] = $element['#add_button_title'];
}
}
- $element['#process'] = array(
- $element['#process'] = array(
- 'media_multiselect_process_widget_form',
- 'media_multiselect_process_widget_form',
- );
- );
-
-
+ // Remove 'browse' class because media module will overwrite click events for
+ // Remove 'browse' class because media module will overwrite click events for
+ // the buttons with this class.
+ // the buttons with this class.
+ $button['#attributes']['class'] = array_values(array_diff($button['#attributes']['class'], array('browse')));
+ $button['#attributes']['class'] = array_values(array_diff($button['#attributes']['class'], array('browse')));
+ $button['#attributes']['class'][] = 'multi-browse';
+ $button['#attributes']['class'][] = 'multi-browse';
+
+
+ $element['browse_button'] = $button;
+ $element['browse_button'] = $button;
+
+
return $element;
return $element;
}
}
Copiar
Copiado
Copiar
Copiado
@@ -
148
,66 +155,43 @@ function media_multiselect_field_widget_form(&$form, &$form_state, $field, $inst
@@ -
151
,66 +155,43 @@ function media_multiselect_field_widget_form(&$form, &$form_state, $field, $inst
* of the value-elements with their own #process-instruction.
* of the value-elements with their own #process-instruction.
*/
*/
function media_multiselect_process_widget_form($element) {
function media_multiselect_process_widget_form($element) {
- $element['add_more']['#media_multiselect_field_wrapper'] = $element['#array_parents'];
- $element['add_more']['#media_multiselect_field_wrapper'] = $element['#array_parents'];
+ $element[$element['#file_upload_delta']]['browse_button']['#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);
- preg_match('/id="(.*?)"/', $element['#prefix'], $matches);
- list($_, $wrapper_id) = $matches;
- list($_, $wrapper_id) = $matches;
- for ($i = 0; $i <= $element['#max_delta']; $i++) {
- for ($i = 0; $i <= $element['#max_delta']; $i++) {
Copiar
Copiado
Copiar
Copiado
+
for ($i = 0; $i <= $element['#file_upload_delta']; $i++) {
+
for ($i = 0; $i <= $element['#file_upload_delta']; $i++) {
$element[$i] += element_info('media');
$element[$i] += element_info('media');
- $element[$i]['#wrapper_id'] = $wrapper_id;
- $element[$i]['#wrapper_id'] = $wrapper_id;
- $element[$i]['#process'][] = 'media_multiselect_improve_remove_button';
- $element[$i]['#process'][] = 'media_multiselect_improve_remove_button';
$element[$i]['#media_multiselect_field_wrapper'] = $element['#array_parents'];
$element[$i]['#media_multiselect_field_wrapper'] = $element['#array_parents'];
}
}
- return $element;
- return $element;
-}
-}
-
-
-/**
-/**
- * #process-callback for each value-element
- * #process-callback for each value-element
- * Alters the remove-button to be a ajax-submit instead, so we can remove the
- * Alters the remove-button to be a ajax-submit instead, so we can remove the
- * element from the form, instead of just blanking it.
- * element from the form, instead of just blanking it.
- */
- */
-function media_multiselect_improve_remove_button($element) {
-function media_multiselect_improve_remove_button($element) {
- $element['remove'] = array(
- $element['remove'] = array(
- '#type' => 'submit',
- '#type' => 'submit',
Copiar
Copiado
Copiar
Copiado
- '#value' => t('Remove
media
'),
- '#value' => t('Remove
'),
- '#weight' => $element['remove']['#weight'],
- '#weight' => $element['remove']['#weight'],
- '#submit' => array('media_multiselect_ajax_submit'),
- '#submit' => array('media_multiselect_ajax_submit'),
- '#media_multiselect_value_handler' => 'media_multiselect_remove_button_value_handler',
- '#media_multiselect_value_handler' => 'media_multiselect_remove_button_value_handler',
- '#name' => $element['#field_name'] . '_remove_' . $element['#delta'],
- '#name' => $element['#field_name'] . '_remove_' . $element['#delta'],
- '#ajax' => array(
- '#ajax' => array(
- 'callback' => 'media_multiselect_ajax_callback',
- 'callback' => 'media_multiselect_ajax_callback',
- 'wrapper' => $element['#wrapper_id'],
- 'wrapper' => $element['#wrapper_id'],
- ),
- ),
- '#media_multiselect_field_wrapper' => array_slice($element['#array_parents'], 0, -1),
- '#media_multiselect_field_wrapper' => array_slice($element['#array_parents'], 0, -1),
- );
- );
+ // Prefix and suffix used for Ajax replacement.
+ // Prefix and suffix used for Ajax replacement.
+ $element['#prefix'] = '<div id="' . $element['#id'] . '-ajax-wrapper">';
+ $element['#prefix'] = '<div id="' . $element['#id'] . '-ajax-wrapper">';
+ $element['#suffix'] = '</div>';
+ $element['#suffix'] = '</div>';
return $element;
return $element;
}
}
/**
/**
- * Element-process callback. See @media_multiselect_field_widget_form
- * Element-process callback. See @media_multiselect_field_widget_form
+ * Element-process callback. See @media_multiselect_change_add_button
+ * Element-process callback. See @media_multiselect_change_add_button
*/
*/
function media_multiselect_add_js_settings($element) {
function media_multiselect_add_js_settings($element) {
static $processed = array();
static $processed = array();
-
-
+
+
// Add the media-options so the add-more button has the correct options for
// Add the media-options so the add-more button has the correct options for
// opening the dialog.
// opening the dialog.
// We use a static variable to prevent we add the settings more than once
// We use a static variable to prevent we add the settings more than once
- $id = $element['add_more']['#id'];
- $id = $element['add_more']['#id'];
+ $id = $element[$element['#file_upload_delta']]['browse_button']['#id'];
+ $id = $element[$element['#file_upload_delta']]['browse_button']['#id'];
if (empty($processed[$id])) {
if (empty($processed[$id])) {
drupal_add_js(array(
drupal_add_js(array(
'media' => array(
'media' => array(
'multi_select' => array(
'multi_select' => array(
'elements' => array(
'elements' => array(
- $id => $element['#media_options'],
- $id => $element['#media_options'],
+ $id => $element[$element['#file_upload_delta']]['#media_options'],
+ $id => $element[$element['#file_upload_delta']]['#media_options'],
),
),
),
),
),
),
), 'setting');
), 'setting');
$processed[$id] = TRUE;
$processed[$id] = TRUE;
}
}
-
-
+
+
return $element;
return $element;
}
}
Copiar
Copiado
Copiar
Copiado
@@ -21
6
,7 +200,7 @@ function media_multiselect_add_js_settings($element) {
@@ -21
9
,7 +200,7 @@ function media_multiselect_add_js_settings($element) {
*/
*/
function theme_media_multiselect_empty_field($vars) {
function theme_media_multiselect_empty_field($vars) {
$element = $vars['element'];
$element = $vars['element'];
-
-
+
+
$attributes['class'] = array('form-item');
$attributes['class'] = array('form-item');
if (!empty($element['#type'])) {
if (!empty($element['#type'])) {
$attributes['class'][] = 'form-type-' . strtr($element['#type'], '_', '-');
$attributes['class'][] = 'form-type-' . strtr($element['#type'], '_', '-');
Copiar
Copiado
Copiar
Copiado
@@ -23
1
,15 +215,14 @@ function theme_media_multiselect_empty_field($vars) {
@@ -23
4
,15 +215,14 @@ function theme_media_multiselect_empty_field($vars) {
$output = '<div' . drupal_attributes($attributes) . '>' . "\n";
$output = '<div' . drupal_attributes($attributes) . '>' . "\n";
$output .= theme('form_element_label', $element);
$output .= theme('form_element_label', $element);
$output .= '<div class="description">' . $element['#description'] . "</div>\n";
$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['add_more']) . '</div>';
+ $output .= '<div class="clearfix">' . drupal_render($element[$element['#file_upload_delta']]['browse_button']) . '</div>';
+ $output .= '<div class="clearfix">' . drupal_render($element[$element['#file_upload_delta']]['browse_button']) . '</div>';
$output .= "</div>\n";
$output .= "</div>\n";
-
-
+
+
return $output;
return $output;
}
}
/**
/**
- * AJAX callback. See @media_multiselect_field_widget_form and
- * AJAX callback. See @media_multiselect_field_widget_form and
- * @media_multiselect_improve_remove_button
- * @media_multiselect_improve_remove_button
+ * AJAX callback. See @media_multiselect_change_add_button
+ * AJAX callback. See @media_multiselect_change_add_button
*
*
* Returns the element in the form identified by the path in the
* Returns the element in the form identified by the path in the
* "#media_multiselect_field_wrapper"-property
* "#media_multiselect_field_wrapper"-property
Copiar
Copiado
Copiar
Copiado
@@ -27
2,11
+255,
10
@@ function media_multiselect_ajax_callback($form, $form_state) {
@@ -27
5,8
+255,
7
@@ function media_multiselect_ajax_callback($form, $form_state) {
}
}
/**
/**
- * AJAX add-more button #submit. See @media_multiselect_field_widget_form and
- * AJAX add-more button #submit. See @media_multiselect_field_widget_form and
- * @media_multiselect_improve_remove_button.
- * @media_multiselect_improve_remove_button.
+ * AJAX add-more button #submit. See @media_multiselect_change_add_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,
* Finds the current value of the field identified by #media_multiselect_field_wrapper,
Copiar
Copiado
Copiar
Copiado
-
* 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
+ * 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) {
* 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'];
$langcode = $element['#language'];
$parents = $element['#field_parents'];
$parents = $element['#field_parents'];
$field_state = field_form_get_state($parents, $field_name, $langcode, $form_state);
$field_state = field_form_get_state($parents, $field_name, $langcode, $form_state);
-
-
- // Remove the 'add more' value, because its not a file.
- // Remove the 'add more' value, because its not a file.
- unset($value['add_more']);
- unset($value['add_more']);
// Filter the existing values so only fid > 0 remains.
// Filter the existing values so only fid > 0 remains.
$value = array_filter($value, '_media_multiselect_filter_empty_value');
$value = array_filter($value, '_media_multiselect_filter_empty_value');
Copiar
Copiado
Copiar
Copiado
@@ -311,7 +290,7 @@ function media_multiselect_ajax_submit($form, &$form_state) {
@@ -34
9
,20 +325,6 @@ function media_multiselect_add_more_value_handler($form, $form_state, $value) {
// 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) {
@@ -34
5
,20 +325,6 @@ function media_multiselect_add_more_value_handler($form, $form_state, $value) {
}
}
/**
/**
- * #media_multiselect_value_handler-callback for the 'remove'-buttons.
- * #media_multiselect_value_handler-callback for the 'remove'-buttons.
- * Finds the delta of the pushed button, and removes that from the $value-array.
- * 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) {
-function media_multiselect_remove_button_value_handler($form, $form_state, $value) {
- $button = $form_state['triggering_element'];
- $button = $form_state['triggering_element'];
- $delta = $button['#array_parents'][count($button['#array_parents'])-2];
- $delta = $button['#array_parents'][count($button['#array_parents'])-2];
-
-
- // Remove the unwanted delta..
- // Remove the unwanted delta..
- unset($value[$delta]);
- unset($value[$delta]);
-
-
- return $value;
- return $value;
-}
-}
-
-
-/**
-/**
* Helper function to remove empty file values (fid=0)
* Helper function to remove empty file values (fid=0)
*/
*/
function _media_multiselect_filter_empty_value($value) {
function _media_multiselect_filter_empty_value($value) {
Diferencias guardadas
Texto original
Abrir archivo
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) {
Texto modificado
Abrir archivo
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) {
Encontrar la diferencia