I thought I'd post this here in case it may help someone else. It took a while to figure out.
Backdrop's Form API provides the element managed_file, which allows you to add a widget to your forms to upload new managed files. The documentation for this element is good, except that it doesn't contain much about the property #browser_view, other than saying that it allows developers to indicate a View to use in the file browser dialog.
So, my use case was: I needed to provide a file browser that allowed the user to pick PDF documents from the managed files (not images). These are the steps:
1. Create your form and include the managed_file element. For example:
$form['certificate_template_file'] = array( '#type' => 'managed_file', '#title' => t('Template PDF file'), '#description' => t('The template PDF file to be used for this certificate.'), '#default_value' => $certificate_template->certificate_template_file, '#upload_location' => 'public://certificate_templates', '#upload_validators' => array( 'file_validate_extensions' => array('pdf'), 'file_validate_size' => array(5 * 1024 * 1024), ), '#browser_view' => 'pdf_doc_library', );
The above indicates that the View "pdf_doc_library" will be used for the file browser.
2. Create your View. Show: Files. I used a Grid display.
3. This is important: in the Grid settings, add the Row class "file-browser-file". Backdrop's file browser will look for that class to select the file ID when clicked.
4. Notice the hidden fields Path and Name (image above). The Path field MUST have the option "Use full URL instead of file storage URI" clicked.
The other important part: rewrite the File ID fields as follows:
The important part here is to provide a data-file-url, AND a data-fid with the data from the hidden fields. The rest you can customize as you wish.
5. Then a bit of styling added to a CSS file that gets loaded with the form, such as:
.file-browser-file:hover { background-color: #e5f3ff; } .file-browser-selected { background-color: #cce8ff; }
That's it. Then, in the form, when clicking "Select existing files" you'll get:
A minor annoyance: the dialog has the title "Select from Image Library". That title is, unfortunately, hard-coded in file_managed_file_browser_open(). Perhaps I'll create an issue for this. It'd be easy to pass the title within $form_state.
Recent comments
Correct, if you add multiple condition statements they are treated as AND so it would appear to the system as: node/103 AND node/91 which will never happen. Glad you found the way to...
Blocks (custom) not displaying in Harris sidebars with visibility condition set to more than one url path (node)
YES! That works. This whole thing is extremely bizarre, though. On the site I'm building (not the sandbox demo), I had a view that was displaying multiple images correctly (i.e. once, not...
Duplicated images in view
Maybe "Query settings" : "Distinct". If there are multiple identical items, each will be displayed only once. You can use this to try and remove duplicates from a view, though it...
Duplicated images in view
If there are N images in the field, then that set of N images gets repeated N times, it seems.
Duplicated images in view
Excellent! Yes, that's one of those "too simple and obvious" solutions. I hadn't thought of it either. However, this solves only one part of the problem (empty blocks). It doesn't solve my...
Duplicated images in view