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 files browser for custom field on Drupal 8/9

By editor , 4 December, 2020

You can use IMCE to select a file.

if (\Drupal::moduleHandler()->moduleExists('imce') && \Drupal\imce\Imce::access()) {
  $form['values']['image-imce-field'] = [
    '#type' => 'textfield',
    '#title' => $this->t('Image'),
    '#attached' => ['library' => ['imce/drupal.imce.input']],
    '#attributes' => ['class' => ['imce-url-input']],
    '#default_value' => '',
  ];
}

Convert file path to FID

public static function getFidFromPath($path) {
  $uri = str_replace('/sites/default/files/', 'public://', $path);
  $query = \Drupal::entityQuery('file');
  $query->condition('uri', $uri);
  $query->sort('created', 'DESC');
  $fids = $query->execute();
  if ($fids) {
    $fids = array_values($fids);
    return $fids[0] ??  NULL;
  }
  return NULL;
}

 

Tags

  • Drupal 8
  • Code
  • Drupal 9
  • Images
Use IMCE on custom page

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