You are here

public function ImceController::page in FileField Sources 8

Outputs the IMCE browser for FileField.

File

src/Controller/ImceController.php, line 31

Class

ImceController
Controller routines for imce routes.

Namespace

Drupal\filefield_sources\Controller

Code

public function page($entity_type, $bundle_name, $field_name, Request $request) {

  // Check access.
  if (!\Drupal::moduleHandler()
    ->moduleExists('imce') || !Imce::access() || !($instance = $this->entityTypeManager
    ->getStorage('field_config')
    ->load($entity_type . '.' . $bundle_name . '.' . $field_name))) {
    throw new AccessDeniedHttpException();
  }
  $settings = $instance
    ->getSettings();
  $imceFM = Imce::userFM(\Drupal::currentUser(), $settings['uri_scheme'], $request);

  // Override scanner.
  if (!empty($imceFM)) {
    $scanner = \Drupal::service('filefield_sources.imce_scanner');
    $widget = $this->entityTypeManager
      ->getStorage('entity_form_display')
      ->load($entity_type . '.' . $bundle_name . '.' . 'default');

    // Full mode.
    if (!empty($widget['third_party_settings']['filefield_sources']['filefield_sources']['source_imce']['imce_mode'])) {
      $imceFM
        ->setConf('scanner', [
        $scanner,
        'customScanFull',
      ]);

      // Set context.
      $scanner
        ->setContext([
        'scheme' => $imceFM
          ->getConf('scheme'),
      ]);
    }
    else {
      $imceFM
        ->setConf('scanner', [
        $scanner,
        'customScanRestricted',
      ]);

      // Make field directory the only accessible one.
      $field_uri = static::getUploadLocation($settings);
      static::disablePerms($imceFM, $field_uri, [
        'browse_files',
      ]);

      // Set context.
      $scanner
        ->setContext([
        'entity_type' => $entity_type,
        'field_name' => $field_name,
        'uri' => $field_uri,
        'is_rool' => $is_root,
      ]);
    }

    // Disable absolute URLs.
    \Drupal::configFactory()
      ->getEditable('imce.settings')
      ->set('abs_urls', FALSE);
    return $imceFM
      ->pageResponse();
  }
}