1 file.module file_managed_file_browser_open($form, $form_state)

AJAX callback for managed_file elements to open a browser modal dialog.

See also

file_managed_file_process()

File

core/modules/file/file.module, line 1716
Defines a "managed_file" Form API field and a "file" field for Field module.

Code

function file_managed_file_browser_open($form, $form_state) {
  // Get the parent "managed_file" element.
  $parents = $form_state['triggering_element']['#array_parents'];
  array_pop($parents);
  $element = backdrop_array_get_nested_value($form, $parents);

  // Pull the view to open off of the managed_file #browser_view property.
  $view_name = $element['#browser_view'];
  $view = views_embed_view($view_name);
  if (empty($view)) {
    $view = t('The view "@view_name" is not available.', array('@view_name' => $view_name));
    if (user_access('administer views')) {
      $view .= ' ' . t('Check the <a href="!url">Views administration page</a> to enable or create the "@view_name" view as needed.', array('!url' => url('admin/structure/views'), '@view_name' => $view_name));
    }
  }

  $submit_form = backdrop_get_form('file_managed_file_browser_form');

  $title = t('Select from Image Library');
  $options = array(
    'dialogClass' => 'file-browser-container',
    'width' => '90%',
    'modal' => TRUE,
  );
  $html = '';
  $html .= theme('status_messages');
  $html .= '<div class="file-browser">';
  $html .= '<div class="file-browser-view">';
  $html .= $view;
  $html .= '</div>';
  $html .= backdrop_render($submit_form);
  $html .= '</div>';

  $settings = array(
    'file' => array(
      'browser' => array(
        'selectedFid' => NULL,
        'currentFidElement' => '[name="' . $element['fid']['#name'] . '"]',
      ),
    ),
  );

  $commands = array();
  $commands[] = ajax_command_settings($settings, TRUE);
  $commands[] = ajax_command_open_dialog('#file-browser-modal', $title, $html, $options);
  return array('#type' => 'ajax', '#commands' => $commands);
}