You are here

image_browser.module in Image Entity Browser 8

Extends Drupal file entities to be fieldable and viewable.

File

image_browser.module
View source
<?php

/**
 * @file
 * Extends Drupal file entities to be fieldable and viewable.
 */
use Drupal\file\Entity\File;
function image_browser_theme() {
  return array(
    'image_browser' => array(
      'render element' => 'element',
    ),
  );
}

/**
 * Implements hook template_preprocess_theme().
 */
function template_preprocess_image_browser(&$variables) {
  $element = $variables['element'];
  if (!isset($element['#value']) && isset($element['#default_value'])) {
    $element['#value'] = $element['#default_value'];
  }

  // Remove name attribute if empty, for W3C compliance.
  if (isset($variables['attributes']['name']) && empty((string) $variables['attributes']['name'])) {
    unset($variables['attributes']['name']);
  }
  if (isset($element['#value'])) {
    $fid = str_replace('file:', '', $element['#value']);
    if ($file = File::load($fid)) {
      if ($file
        ->getMimeType() == 'image/svg+xml') {
        $variables['preview'] = array(
          '#markup' => '<img src="' . file_create_url($file
            ->getFileUri()) . '" style="max-width: 100px"/>',
        );
      }
      else {
        $variables['preview'] = array(
          '#theme' => 'image_style',
          '#style_name' => 'image_browser_thumbnail',
          '#uri' => $file
            ->getFileUri(),
        );
      }
    }
  }
  $variables['children'] = $element['#children'];
  $variables['html_id'] = \Drupal\Component\Utility\Html::getUniqueId('dexp-image-browser');
}

/**
 * Implemment hook_form_alter().
 */
function image_browser_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
  if ($form['#id'] == 'views-exposed-form-dexp-image-browser-image-browser') {
    $form['actions']['#type'] = 'container';
    $form['actions']['#id'] = 'search-actions';
  }
}

Functions

Namesort descending Description
image_browser_form_views_exposed_form_alter Implemment hook_form_alter().
image_browser_theme
template_preprocess_image_browser Implements hook template_preprocess_theme().