Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Use IMCE on custom page

By editor , 18 September, 2017
Question

How to insert IMCE file browser to a custom form or a custom page.

Example 1 (for drupal 7)


Step 1. Add file brows

Create a simple js file in your module directory (called yourModuleName.js) and enter the following Javascript.

function openFileBrowser() {
  window.open('/imce?app=yourApp|url@your-input-id', '', 'width=760,height=560,resizable=1');
}

2) Make sure to include your js file:

$path = drupal_get_path('module', 'yourModuleName');
drupal_add_js($path . '/yourModuleName.js');


3) Create the form element where you want the path to the file to be generated:

$form['your-input-name'] = array(   
  '#title' => t('Your Title'),  
  '#type' => 'textfield',   
  '#attributes' => array('readonly' => 'readonly'), // So that the user cannot manually enter a value  
> );

 

4) Create a button that will execute the Javascript function:

$form['your-button-name'] = array(
  '#type' => 'submit',
  '#value' => t('Browse Server'),
  '#attributes' => array('onclick' => 'openFileBrowser();'),
);


Example 2 using inline JS (for drupal 7)


$inlinejs = <<<JS
function openFileBrowser() {
  window.open('/imce?app=yourApp|url@file-input-id', '', 'width=760,height=560,resizable=1');
}
JS;
drupal_add_js($inlinejs, ['type' => 'inline', 'scope' => 'footer', 'weight' => 5]);


$output = [];
$output['open-imce-btn'] = [
  '#type' => 'submit',
  '#value' => t('Open IMCE'),
  '#attributes' => ['onclick' => 'openFileBrowser();'],
];

Tags

  • Drupal 8
  • Code
  • Drupal 7
  • Module
Use IMCE files browser for custom field on Drupal 8/9

Comments

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer