You are here

function LinkitSearchPluginFile::createPath in Linkit 7.3

Overrides LinkitSearchPluginEntity::createPath().

If 'Direct download' is enabled, make the link point to the file entity download endpoint.

Overrides LinkitSearchPluginEntity::createPath

File

plugins/linkit_search/file.class.php, line 101
Define Linkit file search plugin class.

Class

LinkitSearchPluginFile
Reprecents a Linkit file search plugin.

Code

function createPath($entity) {
  $url_type = isset($this->conf['url_type']) ? $this->conf['url_type'] : $this
    ->getDefaultUrlType();

  // We can only support the download type if we have version 2.x of the file_entity module.
  if ($url_type == LINKIT_FILE_URL_TYPE_DOWNLOAD && !(module_exists('file_entity') && function_exists('file_entity_download_uri'))) {
    $url_type = $this
      ->getDefaultUrlType();
  }
  switch ($url_type) {
    case LINKIT_FILE_URL_TYPE_DIRECT:

      // Check if this is a local file.
      $wrapper = file_stream_wrapper_get_instance_by_uri($entity->uri);
      if ($wrapper instanceof DrupalPublicStreamWrapper) {

        // Create a relative URL to the public file.
        // See https://www.drupal.org/node/837794.
        $path = $wrapper
          ->getDirectoryPath() . '/' . file_uri_target($entity->uri);
      }
      elseif ($wrapper instanceof DrupalPrivateStreamWrapper) {

        // Create a relative URL to the private file.
        // See DrupalPrivateStreamWrapper::getExternalUrl().
        $path = 'system/files/' . str_replace('\\', '/', file_uri_target($entity->uri));
      }
      else {
        $path = file_create_url($entity->uri);
      }

      // Process the uri with the insert plugin.
      return linkit_get_insert_plugin_processed_path($this->profile, $path, array(
        'language' => (object) array(
          'language' => FALSE,
        ),
      ));
    case LINKIT_FILE_URL_TYPE_DOWNLOAD:
      $uri = file_entity_download_uri($entity);

      // Hack for LINKIT_URL_METHOD_RAW, which won't include the options that
      // we pass to linkit_get_insert_plugin_processed_path().
      if (isset($uri['options']['query']['token']) && $this->profile->data['insert_plugin']['url_method'] == LINKIT_URL_METHOD_RAW) {
        return $uri['path'] . '?token=' . rawurlencode($uri['options']['query']['token']);
      }

      // Process the uri with the insert plugin.
      return linkit_get_insert_plugin_processed_path($this->profile, $uri['path'], $uri['options']);
    case LINKIT_FILE_URL_TYPE_ENTITY:

      // Pass back to the parent if the user wants entity urls.
      return parent::createPath($entity);
  }
}