Use IMCE files browser for custom field on Drupal 8/9 | Drupal 8

Use IMCE files browser for custom field on Drupal 8/9

Submitted by editor on Fri, 12/04/2020 - 11:34

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;
}

 

Comments

JackStephens (not verified)

Tue, 12/08/2020 - 23:09

Pretty company https://order.studentshare.org definitely knows what perfection means. An absolute understanding of quality, customer – oriented management and reasonable prices. They offer all possible services any student might need: technical writing, academic writing, copywriting, website content, science papers, test and even IT projects. Flexible discount system and participation in loyalty programs.

Add new comment

Plain text

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