You are here

shrinktheweb.module in ShrinkTheWeb 8

Same filename and directory in other branches
  1. 6 shrinktheweb.module
  2. 7 shrinktheweb.module

File

shrinktheweb.module
View source
<?php

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Component\Utility\Html;
use Drupal\Core\Render\Element;
use Drupal\Core\Template\Attribute;

/**
 * Implementation of hook_help()
 */
function shrinktheweb_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.shrinktheweb':
      return '<p>' . t('You can find the latest help information for the ShrinkTheWeb module at ') . '<a href="http://drupal.org/node/1067900" target="_blank">http://drupal.org/node/1067900</a></p>';
  }
}

/**
 * Generates or returns the path/source of the thumbnail
 *
 * @param string $url The URL of the web page
 * @param array $options Overwrite the options set in admin config
 * @access public
 *
 * @return mixed Thumbnail path/source or FALSE
 */
function shrinktheweb_get_thumbnail($url, $options = array()) {
  module_load_include('inc', 'shrinktheweb', 'shrinktheweb.api');
  $sImageURL = shrinktheweb_getThumbnailURL($url, $options);
  return $sImageURL;
}

/**
 * Implementation of hook_theme()
 */
function shrinktheweb_theme($existing, $type, $theme, $path) {
  return array(
    'shrinktheweb_image' => array(
      'variables' => array(
        'url' => NULL,
        'custom_width' => NULL,
        'full_length' => NULL,
        'max_height' => NULL,
        'native_resolution' => NULL,
        'widescreen_resolution_y' => NULL,
        'delay' => NULL,
        'quality' => NULL,
      ),
    ),
    'shrinktheweb_image_link' => array(
      'variables' => array(
        'url' => NULL,
        'custom_width' => NULL,
        'full_length' => NULL,
        'max_height' => NULL,
        'native_resolution' => NULL,
        'widescreen_resolution_y' => NULL,
        'delay' => NULL,
        'quality' => NULL,
      ),
    ),
    // -- Link module ----------------------------
    'shrinktheweb_formatter_shrinktheweb_link_default' => array(
      'variables' => array(
        'url' => NULL,
        'title' => NULL,
        'custom_width' => NULL,
        'full_length' => NULL,
        'max_height' => NULL,
        'native_resolution' => NULL,
        'widescreen_resolution_y' => NULL,
        'delay' => NULL,
        'quality' => NULL,
      ),
    ),
    'shrinktheweb_formatter_shrinktheweb_link_plain' => array(
      'variables' => array(
        'url' => NULL,
        'custom_width' => NULL,
        'full_length' => NULL,
        'max_height' => NULL,
        'native_resolution' => NULL,
        'widescreen_resolution_y' => NULL,
        'delay' => NULL,
        'quality' => NULL,
      ),
    ),
    'shrinktheweb_formatter_shrinktheweb_link_url' => array(
      'variables' => array(
        'url' => NULL,
        'custom_width' => NULL,
        'full_length' => NULL,
        'max_height' => NULL,
        'native_resolution' => NULL,
        'widescreen_resolution_y' => NULL,
        'delay' => NULL,
        'quality' => NULL,
      ),
    ),
    'shrinktheweb_formatter_shrinktheweb_link_short' => array(
      'variables' => array(
        'url' => NULL,
        'custom_width' => NULL,
        'full_length' => NULL,
        'max_height' => NULL,
        'native_resolution' => NULL,
        'widescreen_resolution_y' => NULL,
        'delay' => NULL,
        'quality' => NULL,
      ),
    ),
    'shrinktheweb_formatter_shrinktheweb_link_separate' => array(
      'variables' => array(
        'url' => NULL,
        'title' => NULL,
        'custom_width' => NULL,
        'full_length' => NULL,
        'max_height' => NULL,
        'native_resolution' => NULL,
        'widescreen_resolution_y' => NULL,
        'delay' => NULL,
        'quality' => NULL,
      ),
    ),
    'shrinktheweb_modules_fieldset' => array(
      'render element' => 'form',
    ),
  );
}

/**
 * Displays the thumbnail, also used for '[ShrinkTheWeb] No link text'
 *
 * @param array $variables
 */
function template_preprocess_shrinktheweb_image(&$variables) {
  $options = _shrinktheweb_custom_settings($variables);
  if ($src = shrinktheweb_get_thumbnail($variables['url'], $options)) {
    $variables['image_path'] = $src;
  }
}

/**
 * Displays the thumbnail as a link.
 *
 * @param array $variables
 */
function template_preprocess_shrinktheweb_image_link(&$variables) {
  $options = _shrinktheweb_custom_settings($variables);
  if ($src = shrinktheweb_get_thumbnail($variables['url'], $options)) {
    $variables['image_path'] = $src;
  }
  $variables['url'] = Url::fromUri($variables['url']);
}

/**
 * Displays the thumbnail as '[ShrinkTheWeb] Title as link'.
 *
 * @param array $variables
 */
function template_preprocess_shrinktheweb_formatter_shrinktheweb_link_default(&$variables) {
  $options = _shrinktheweb_custom_settings($variables);
  $variables['title'] = empty($variables['title']) ? '' : \Drupal\Component\Utility\SafeMarkup::checkPlain($variables['title']);
  $variables['link'] = Link::fromTextAndUrl($variables['title'], Url::fromUri($variables['url']));
  if ($src = shrinktheweb_get_thumbnail($variables['url'], $options)) {
    $variables['image_path'] = $src;
  }
}

/**
 * Displays the thumbnail as '[ShrinkTheWeb] URL as plain text'.
 *
 * @param array $variables
 */
function template_preprocess_shrinktheweb_formatter_shrinktheweb_link_plain(&$variables) {
  $options = _shrinktheweb_custom_settings($variables);
  if ($src = shrinktheweb_get_thumbnail($variables['url'], $options)) {
    $variables['image_path'] = $src;
  }
  $variables['url'] = Url::fromUri($variables['url']);
}

/**
 * Displays the thumbnail as '[ShrinkTheWeb] Separate title and URL'.
 *
 * @param array $variables
 */
function template_preprocess_shrinktheweb_formatter_shrinktheweb_link_separate(&$variables) {
  $options = _shrinktheweb_custom_settings($variables);
  $variables['title'] = empty($variables['title']) ? '' : \Drupal\Component\Utility\SafeMarkup::checkPlain($variables['title']);
  $variables['link'] = Link::fromTextAndUrl($variables['url'], Url::fromUri($variables['url']));
  if ($src = shrinktheweb_get_thumbnail($variables['url'], $options)) {
    $variables['image_path'] = $src;
  }
}

/**
 * Displays the thumbnail as '[ShrinkTheWeb] Short as link with title "Link"'.
 *
 * @param array $variables
 */
function template_preprocess_shrinktheweb_formatter_shrinktheweb_link_short(&$variables) {
  $options = _shrinktheweb_custom_settings($variables);
  $variables['link'] = Link::fromTextAndUrl(t('Link'), Url::fromUri($variables['url']));
  if ($src = shrinktheweb_get_thumbnail($variables['url'], $options)) {
    $variables['image_path'] = $src;
  }
}

/**
 * Displays the thumbnail as '[ShrinkTheWeb] URL, as link'.
 *
 * @param array $variables
 */
function template_preprocess_shrinktheweb_formatter_shrinktheweb_link_url(&$variables) {
  $options = _shrinktheweb_custom_settings($variables);
  $variables['link'] = Link::fromTextAndUrl($variables['url'], Url::fromUri($variables['url']));
  if ($src = shrinktheweb_get_thumbnail($variables['url'], $options)) {
    $variables['image_path'] = $src;
  }
}

/**
 * Generate custom request options for field formatters.
 * @param $_variables
 * @return mixed
 */
function _shrinktheweb_custom_settings($_variables) {
  $_options = array();
  if (!is_null($_variables['custom_width'])) {
    $_options['SizeCustom'] = $_variables['custom_width'];
  }
  if (!is_null($_variables['full_length'])) {
    $_options['FullSizeCapture'] = $_variables['full_length'];
  }
  if (!is_null($_variables['max_height'])) {
    $_options['MaxHeight'] = $_variables['max_height'];
  }
  if (!is_null($_variables['native_resolution'])) {
    $_options['NativeResolution'] = $_variables['native_resolution'];
  }
  if (!is_null($_variables['widescreen_resolution_y'])) {
    $_options['WidescreenY'] = $_variables['widescreen_resolution_y'];
  }
  if (!is_null($_variables['delay'])) {
    $_options['Delay'] = $_variables['delay'];
  }
  if (!is_null($_variables['quality'])) {
    $_options['Quality'] = $_variables['quality'];
  }
  return $_options;
}

/**
 * Implementation of hook_file_download().
 */
function shrinktheweb_file_download($uri) {
  if (preg_match('/^' . file_default_scheme() . ':\\/\\/' . \Drupal::config('shrinktheweb.settings')
    ->get('shrinktheweb_thumbs_folder') . '(.*?)\\.jpg$/si', $uri)) {
    return array(
      'Content-Type' => \Drupal::service('file.mime_type.guesser')
        ->guess($uri),
    );
  }
}

/**
 * Implements hook_form_alter().
 */
function shrinktheweb_form_entity_view_display_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $form['actions']['submit']['#submit'][] = '_shrinktheweb_submit_handler';
}

/**
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 */
function _shrinktheweb_submit_handler($form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $fields = $form_state
    ->getValues();
  $shrinktheweb_fields = \Drupal::database()
    ->select('shrinktheweb_fields', 't')
    ->fields('t', array(
    'stw_field_name',
  ))
    ->condition('stw_entity_bundle', $form['#bundle'])
    ->execute()
    ->fetchCol();
  foreach ($fields['fields'] as $key => $value) {

    // Stw formatter was set for new field.
    if (strpos($value['type'], 'shrinktheweb') !== FALSE && !in_array($key, $shrinktheweb_fields)) {

      // Save new field id in database.
      $db_fields = array(
        'stw_field_name' => $key,
        'stw_entity_bundle' => $form['#bundle'],
      );
      \Drupal::database()
        ->merge('shrinktheweb_fields')
        ->key(array(
        'stw_field_name' => $key,
      ))
        ->fields($db_fields)
        ->updateFields($db_fields)
        ->execute();
    }

    // Stw formatter was unset for old field.
    if (strpos($value['type'], 'shrinktheweb') === FALSE && in_array($key, $shrinktheweb_fields)) {

      // Remove old field from database.
      \Drupal::database()
        ->delete('shrinktheweb_fields')
        ->condition('stw_field_name', $key, '=')
        ->execute();
    }
  }
}

/**
 * Implements hook_entity_view_alter().
 */
function shrinktheweb_entity_view_alter(array &$build, Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display) {
  $shrinktheweb_fields = \Drupal::database()
    ->select('shrinktheweb_fields', 't')
    ->distinct()
    ->fields('t', array(
    'stw_entity_bundle',
  ))
    ->condition('stw_entity_bundle', $entity
    ->bundle())
    ->execute()
    ->fetchCol();
  if (!empty($shrinktheweb_fields)) {
    $cache_days = \Drupal::config('shrinktheweb.settings')
      ->get('shrinktheweb_cache_days');
    switch (TRUE) {
      case $cache_days == '-1':
        $build['#cache']['max-age'] = 0;
        break;
      case $cache_days == '0':
        $build['#cache']['max-age'] = \Drupal\Core\Cache\Cache::PERMANENT;
        break;
      case $cache_days > '0':
        $build['#cache']['max-age'] = $cache_days * 86400;
        break;
    }
  }
}

/**
 * Implements hook_entity_bundle_delete().
 */
function shrinktheweb_entity_bundle_delete($entity_type_id, $bundle) {
  if ($entity_type_id == 'node') {
    $shrinktheweb_fields = \Drupal::database()
      ->select('shrinktheweb_fields', 't')
      ->distinct()
      ->fields('t', array(
      'stw_entity_bundle',
    ))
      ->condition('stw_entity_bundle', $bundle)
      ->execute()
      ->fetchCol();
    if (in_array($bundle, $shrinktheweb_fields)) {
      \Drupal::database()
        ->delete('shrinktheweb_fields')
        ->condition('stw_entity_bundle', $bundle, '=')
        ->execute();
    }
  }
}

/**
 * Implements hook_field_purge_field().
 */
function shrinktheweb_field_purge_field(\Drupal\field\Entity\FieldConfig $field) {
  $shrinktheweb_fields = \Drupal::database()
    ->select('shrinktheweb_fields', 't')
    ->fields('t', array(
    'stw_field_name',
  ))
    ->condition('stw_field_name', $field
    ->getName())
    ->execute()
    ->fetchCol();
  if (in_array($field
    ->getName(), $shrinktheweb_fields)) {
    \Drupal::database()
      ->delete('shrinktheweb_fields')
      ->condition('stw_field_name', $field
      ->getName(), '=')
      ->execute();
  }
}

/**
 * Implements hook_form_alter().
 */
function shrinktheweb_form_system_modules_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $form['modules']['shrinktheweb']['shrinktheweb']['links']['donate'] = array(
    '#url' => Url::fromUri('https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=ZBNAT7HJACUAG&lc=US&item_name=ShrinkTheWeb&no_note=0&cn=Add%20special%20instructions%20to%20the%20seller%3a&no_shipping=1&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted'),
    '#options' => array(
      'attributes' => array(
        'class' => array(
          0 => 'module-link',
          1 => 'module-link-donate',
        ),
        'title' => t('Donate'),
        'target' => '_blank',
      ),
    ),
    '#title' => t('Donate'),
    '#type' => 'link',
  );
  $form['modules']['shrinktheweb']['#theme'] = 'shrinktheweb_modules_fieldset';
}

/**
 * Prepares variables for the module details templates.
 *
 * Default template: shrinktheweb-modules-fieldset.html.twig.
 *
 * @param $variables
 *   An associative array containing:
 *   - form: A render element representing the form. The main form element
 *     represents a package, and child elements of the form are individual
 *     projects. Each project (or module) is an associative array containing the
 *     following elements:
 *     - name: The name of the module.
 *     - enable: A checkbox for enabling the module.
 *     - description: A description of the module.
 *     - version: The version of the module.
 *     - links: Administration links provided by the module.
 *     - #requires: A list of modules that the project requires.
 *     - #required_by: A list of modules that require the project.
 *     - #attributes: A list of attributes for the module wrapper.
 *
 * @see \Drupal\system\Form\ModulesListForm
 */
function template_preprocess_shrinktheweb_modules_fieldset(&$variables) {
  $form = $variables['form'];
  $variables['modules'] = [];

  // Iterate through all the modules, which are children of this element.
  foreach (Element::children($form) as $key) {

    // Stick the key into $module for easier access.
    $module = $form[$key];
    unset($module['enable']['#title']);
    $module['#requires'] = array_filter($module['#requires']);
    $module['#required_by'] = array_filter($module['#required_by']);

    // Add the checkbox to allow installing new modules and to show the
    // installation status of the module.
    $module['checkbox'] = $module['enable'];

    // Add the module label and expand/collapse functionality.
    $id = Html::getUniqueId('module-' . $key);
    $module['id'] = $id;
    $module['enable_id'] = $module['enable']['#id'];

    // @todo Remove early rendering and use safe_join in the Twig template once
    //   https://www.drupal.org/node/2579091 is fixed.
    $renderer = \Drupal::service('renderer');
    $machine_name_render = [
      '#prefix' => '<span dir="ltr" class="table-filter-text-source">',
      '#plain_text' => $key,
      '#suffix' => '</span>',
    ];
    $module['machine_name'] = $renderer
      ->render($machine_name_render);
    if (!empty($module['#requires'])) {
      $requires = [
        '#theme' => 'item_list',
        '#items' => $module['#requires'],
        '#context' => [
          'list_style' => 'comma-list',
        ],
      ];
      $module['requires'] = $renderer
        ->render($requires);
    }
    if (!empty($module['#required_by'])) {
      $required_by = [
        '#theme' => 'item_list',
        '#items' => $module['#required_by'],
        '#context' => [
          'list_style' => 'comma-list',
        ],
      ];
      $module['required_by'] = $renderer
        ->render($required_by);
    }
    if (!empty($module['version'])) {
      $module['version'] = $renderer
        ->render($module['version']);
    }
    $module['attributes'] = new Attribute($module['#attributes']);
    $module['link_types'] = array_filter(array_keys($module['links']), '_shrinktheweb_link_check');
    $variables['modules'][] = $module;
  }
}

/**
 * Custom callback for array_filter.
 */
function _shrinktheweb_link_check($var) {
  return $var && $var[0] != '#';
}

Functions

Namesort descending Description
shrinktheweb_entity_bundle_delete Implements hook_entity_bundle_delete().
shrinktheweb_entity_view_alter Implements hook_entity_view_alter().
shrinktheweb_field_purge_field Implements hook_field_purge_field().
shrinktheweb_file_download Implementation of hook_file_download().
shrinktheweb_form_entity_view_display_edit_form_alter Implements hook_form_alter().
shrinktheweb_form_system_modules_alter Implements hook_form_alter().
shrinktheweb_get_thumbnail Generates or returns the path/source of the thumbnail
shrinktheweb_help Implementation of hook_help()
shrinktheweb_theme Implementation of hook_theme()
template_preprocess_shrinktheweb_formatter_shrinktheweb_link_default Displays the thumbnail as '[ShrinkTheWeb] Title as link'.
template_preprocess_shrinktheweb_formatter_shrinktheweb_link_plain Displays the thumbnail as '[ShrinkTheWeb] URL as plain text'.
template_preprocess_shrinktheweb_formatter_shrinktheweb_link_separate Displays the thumbnail as '[ShrinkTheWeb] Separate title and URL'.
template_preprocess_shrinktheweb_formatter_shrinktheweb_link_short Displays the thumbnail as '[ShrinkTheWeb] Short as link with title "Link"'.
template_preprocess_shrinktheweb_formatter_shrinktheweb_link_url Displays the thumbnail as '[ShrinkTheWeb] URL, as link'.
template_preprocess_shrinktheweb_image Displays the thumbnail, also used for '[ShrinkTheWeb] No link text'
template_preprocess_shrinktheweb_image_link Displays the thumbnail as a link.
template_preprocess_shrinktheweb_modules_fieldset Prepares variables for the module details templates.
_shrinktheweb_custom_settings Generate custom request options for field formatters.
_shrinktheweb_link_check Custom callback for array_filter.
_shrinktheweb_submit_handler