You are here

class LinkitSearchPluginFile in Linkit 7.3

Reprecents a Linkit file search plugin.

Hierarchy

Expanded class hierarchy of LinkitSearchPluginFile

File

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

View source
class LinkitSearchPluginFile extends LinkitSearchPluginEntity {

  /**
   * Overrides LinkitSearchPlugin::ui_title().
   */
  function ui_title() {
    return t('Managed files');
  }

  /**
   * Overrides LinkitSearchPlugin::ui_description().
   */
  function ui_description() {
    return t('Extend Linkit with file support (Managed files).');
  }

  /**
   * Overrides LinkitSearchPluginEntity::createDescription().
   *
   * If the file is an image, a small thumbnail can be added to the description.
   * Also, image dimensions can be shown.
   */
  function createDescription($data) {
    $description_array = array();

    // Get image info.
    $imageinfo = image_get_info($data->uri);

    // Add small thumbnail to the description.
    if ($this->conf['image_extra_info']['thumbnail']) {
      $image = $imageinfo ? theme_image_style(array(
        'width' => $imageinfo['width'],
        'height' => $imageinfo['height'],
        'style_name' => 'linkit_thumb',
        'path' => $data->uri,
      )) : '';
    }

    // Add image dimensions to the description.
    if ($this->conf['image_extra_info']['dimensions'] && !empty($imageinfo)) {
      $description_array[] = $imageinfo['width'] . 'x' . $imageinfo['height'] . 'px';
    }
    $description_array[] = parent::createDescription($data);

    // Add tiel files scheme to the description.
    if ($this->conf['show_scheme']) {
      $description_array[] = file_uri_scheme($data->uri) . '://';
    }
    $description = (isset($image) ? $image : '') . implode('<br />', $description_array);
    return $description;
  }

  /**
   * Overrides LinkitSearchPluginEntity::createGroup().
   */
  function createGroup($entity) {

    // The the standard group name.
    $group = parent::createGroup($entity);

    // Add the scheme.
    if ($this->conf['group_by_scheme']) {

      // Get all stream wrappers.
      $stream_wrapper = file_get_stream_wrappers();
      $group .= ' - ' . $stream_wrapper[file_uri_scheme($entity->uri)]['name'];
    }
    return $group;
  }

  /**
   * Overrides LinkitSearchPluginEntity::createPath().
   *
   * If 'Direct download' is enabled, make the link point to the file entity
   * download endpoint.
   */
  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);
    }
  }

  /**
   * Overrides LinkitSearchPluginEntity::getQueryInstance().
   */
  function getQueryInstance() {

    // Call the parent getQueryInstance method.
    parent::getQueryInstance();

    // Only search for permanent files.
    $this->query
      ->propertyCondition('status', FILE_STATUS_PERMANENT);
  }

  /**
   * Overrides LinkitSearchPluginEntity::buildSettingsForm().
   */
  function buildSettingsForm() {
    $form = parent::buildSettingsForm();
    $form['entity:file']['show_scheme'] = array(
      '#title' => t('Show file scheme'),
      '#type' => 'checkbox',
      '#default_value' => isset($this->conf['show_scheme']) ? $this->conf['show_scheme'] : '',
    );
    $form['entity:file']['group_by_scheme'] = array(
      '#title' => t('Group files by scheme'),
      '#type' => 'checkbox',
      '#default_value' => isset($this->conf['group_by_scheme']) ? $this->conf['group_by_scheme'] : '',
    );
    $form['entity:file']['url_type'] = array(
      '#title' => t('URL type'),
      '#type' => 'radios',
      '#options' => array(
        LINKIT_FILE_URL_TYPE_DIRECT => t('Direct file link'),
        LINKIT_FILE_URL_TYPE_DOWNLOAD => t('Download file link'),
        LINKIT_FILE_URL_TYPE_ENTITY => t('Entity view page'),
      ),
      '#default_value' => 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 (!(module_exists('file_entity') && function_exists('file_entity_download_uri'))) {
      unset($form['entity:file']['url_type']['#options'][LINKIT_FILE_URL_TYPE_DOWNLOAD]);
    }
    $image_extra_info_options = array(
      'thumbnail' => t('Show thumbnails <em>(using the image style !linkit_thumb_link)</em>', array(
        '!linkit_thumb_link' => l(t('linkit_thumb'), 'admin/config/media/image-styles/edit/linkit_thumb'),
      )),
      'dimensions' => t('Show pixel dimensions'),
    );
    $form['entity:file']['image_extra_info'] = array(
      '#title' => t('Images'),
      '#type' => 'checkboxes',
      '#options' => $image_extra_info_options,
      '#default_value' => isset($this->conf['image_extra_info']) ? $this->conf['image_extra_info'] : array(
        'thumbnail',
        'dimensions',
      ),
    );
    return $form;
  }

  /**
   * Gets the default URL type to use if no URL type has been explicitly set.
   *
   * @return string The URL type
   */
  protected function getDefaultUrlType() {
    $info = entity_get_info('file');
    $callback = $info['uri callback'];
    if ($callback == 'entity_metadata_uri_file') {

      // The Drupal core file URI callback would be used if we were to use the
      // "Entity view page" URL mode, which generates absolute URLs to files.
      // The "Direct link" URL mode is preferable in this case as it generates
      // relative links.
      return LINKIT_FILE_URL_TYPE_DIRECT;
    }
    else {

      // We use a custom URI callback such as in Media 1.x or File Entity 2.x.
      // The "Entity view page" URL mode is preferable in this case.
      return LINKIT_FILE_URL_TYPE_ENTITY;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LinkitSearchPlugin::$plugin protected property The plugin definition for this instance.
LinkitSearchPlugin::$profile protected property The profile instance for this instance.
LinkitSearchPlugin::broken public function Determine if the handler is considered 'broken', meaning it's a a placeholder used when a handler can't be found. 1
LinkitSearchPlugin::factory public static function Implements LinkitSearchPluginInterface::factory(). Overrides LinkitSearchPluginInterface::factory
LinkitSearchPluginEntity::$conf property Plugin specific settings.
LinkitSearchPluginEntity::$entity_field_label property The name of the property that contains the entity label.
LinkitSearchPluginEntity::$entity_info property The entity info array of an entity type.
LinkitSearchPluginEntity::$entity_key_bundle property The name of the property of the bundle object that contains the name of the bundle object.
LinkitSearchPluginEntity::$query property Entity field query instance.
LinkitSearchPluginEntity::createLabel function Create a label of an entity.
LinkitSearchPluginEntity::createRowClass function Create a row class to append to the search result row. 1
LinkitSearchPluginEntity::fetchResults public function Implements LinkitSearchPluginInterface::fetchResults(). Overrides LinkitSearchPluginInterface::fetchResults 1
LinkitSearchPluginEntity::__construct function Overrides LinkitSearchPlugin::__construct(). Overrides LinkitSearchPlugin::__construct 2
LinkitSearchPluginFile::buildSettingsForm function Overrides LinkitSearchPluginEntity::buildSettingsForm(). Overrides LinkitSearchPluginEntity::buildSettingsForm
LinkitSearchPluginFile::createDescription function Overrides LinkitSearchPluginEntity::createDescription(). Overrides LinkitSearchPluginEntity::createDescription
LinkitSearchPluginFile::createGroup function Overrides LinkitSearchPluginEntity::createGroup(). Overrides LinkitSearchPluginEntity::createGroup
LinkitSearchPluginFile::createPath function Overrides LinkitSearchPluginEntity::createPath(). Overrides LinkitSearchPluginEntity::createPath
LinkitSearchPluginFile::getDefaultUrlType protected function Gets the default URL type to use if no URL type has been explicitly set.
LinkitSearchPluginFile::getQueryInstance function Overrides LinkitSearchPluginEntity::getQueryInstance(). Overrides LinkitSearchPluginEntity::getQueryInstance
LinkitSearchPluginFile::ui_description function Overrides LinkitSearchPlugin::ui_description(). Overrides LinkitSearchPlugin::ui_description
LinkitSearchPluginFile::ui_title function Overrides LinkitSearchPlugin::ui_title(). Overrides LinkitSearchPlugin::ui_title