diff --git a/includes/media.variables.inc b/includes/media.variables.inc index c11445a..7e958b1 100644 --- a/includes/media.variables.inc +++ b/includes/media.variables.inc @@ -152,6 +152,9 @@ function media_variable_default($name = NULL) { // This is set in media_enable(). It will show a persistant dsm on every page // until the user runs the batch operation provided by media_admin_rebuild_types_form(). 'show_file_type_rebuild_nag' => FALSE, + + // This is array of user-defined view modes + 'view_modes' => array(), ); } diff --git a/media.admin.inc b/media.admin.inc index dbb5d64..0c6fc1c 100644 --- a/media.admin.inc +++ b/media.admin.inc @@ -656,4 +656,67 @@ function media_admin_rebuild_types_batch_complete($success, $results, $operation media_variable_del('show_file_type_rebuild_nag'); } drupal_set_message($message); +} + +/* + * From callback for custom view mode edit form. + */ +function media_admin_view_mode_add($form, $form_state) { + $form['name'] = array( + '#type' => 'textfield', + '#title' => t('Name'), + '#description' => t('The human-readable name of new view mode.'), + '#required' => TRUE, + ); + + $form['machine_name'] = array( + '#type' => 'machine_name', + '#title' => t('Machine readable name'), + '#machine_name' => array( + 'exists' => '_media_check_view_mode_exists', + ), + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Save'), + ); + + $redirect = isset($_GET['destination']) && !url_is_external($_GET['destination']) ? drupal_parse_url($_GET['destination']) : 'admin/config/media/types'; + $redirect = is_array($redirect) ? $redirect['path'] : $redirect; + $form['cancel'] = array( + '#markup' => l(t('Cancel'), $redirect), + ); + + return $form; +} + +/* + * Submit callback form custom view mode edit form. + */ +function media_admin_view_mode_add_submit(&$form, &$form_state) { + if ($form_state['clicked_button']['#type'] == 'submit') { + // Read form values + $machine_name = $form_state['values']['machine_name']; + $human_name = $form_state['values']['name']; + + // Get list of current list of custom view modes and add new + $view_modes = media_variable_get('view_modes'); + $view_modes[$machine_name] = $human_name; + + // Save populated list of view modes and set message + media_variable_set('view_modes', $view_modes); + drupal_set_message(t('Custom view mode %name was succesfully created.', array('%name' => $human_name))); + } +} + +/** + * Checks if custom view mode already exists. + * + * @param $machine Machine name to be tested. + * @return TRUE if a view mode with given machine name does not exist yet. + */ +function _media_check_view_mode_exists($machine) { + $view_modes = media_field_view_modes('media'); + return isset($view_modes[$machine]); } \ No newline at end of file diff --git a/media.fields.inc b/media.fields.inc index de28823..feedcce 100644 --- a/media.fields.inc +++ b/media.fields.inc @@ -88,6 +88,12 @@ function media_field_view_modes($obj_type) { 'media_large' => array('label' => t('Large'), 'custom settings' => FALSE), 'media_original' => array('label' => t('Original'), 'custom settings' => FALSE), ); + + // Add custom user-submitted view modes + $view_modes = media_variable_get('view_modes'); + foreach ($view_modes as $machine => $mode) { + $modes[$machine] = array('label' => $mode, 'custom settings' => TRUE); + } } return $modes; } @@ -454,3 +460,20 @@ function media_field_update_instance($instance, $prior_instance) { media_filter_invalidate_caches(); } } + +/** + * Implements hook_form_FORM_ID_alter(). + * + * Adds "Add new view mode" to "manage display" pages for media entity type. + */ +function media_form_field_ui_display_overview_form_alter(&$form, &$form_state, &$form_id) { + if ($form['#entity_type'] == 'media') { + $form['modes']['add_mode'] = array( + '#prefix' => '', + '#markup' => l(t('Add new view mode'), 'admin/config/media/types/view-mode/add', array( + 'query' => array(drupal_get_destination())) + ), + ); + } +} \ No newline at end of file