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;
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>';
}
}
function shrinktheweb_get_thumbnail($url, $options = array()) {
module_load_include('inc', 'shrinktheweb', 'shrinktheweb.api');
$sImageURL = shrinktheweb_getThumbnailURL($url, $options);
return $sImageURL;
}
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,
),
),
'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',
),
);
}
function template_preprocess_shrinktheweb_image(&$variables) {
$options = _shrinktheweb_custom_settings($variables);
if ($src = shrinktheweb_get_thumbnail($variables['url'], $options)) {
$variables['image_path'] = $src;
}
}
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']);
}
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;
}
}
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']);
}
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;
}
}
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;
}
}
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;
}
}
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;
}
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),
);
}
}
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';
}
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) {
if (strpos($value['type'], 'shrinktheweb') !== FALSE && !in_array($key, $shrinktheweb_fields)) {
$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();
}
if (strpos($value['type'], 'shrinktheweb') === FALSE && in_array($key, $shrinktheweb_fields)) {
\Drupal::database()
->delete('shrinktheweb_fields')
->condition('stw_field_name', $key, '=')
->execute();
}
}
}
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;
}
}
}
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();
}
}
}
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();
}
}
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¤cy_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';
}
function template_preprocess_shrinktheweb_modules_fieldset(&$variables) {
$form = $variables['form'];
$variables['modules'] = [];
foreach (Element::children($form) as $key) {
$module = $form[$key];
unset($module['enable']['#title']);
$module['#requires'] = array_filter($module['#requires']);
$module['#required_by'] = array_filter($module['#required_by']);
$module['checkbox'] = $module['enable'];
$id = Html::getUniqueId('module-' . $key);
$module['id'] = $id;
$module['enable_id'] = $module['enable']['#id'];
$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;
}
}
function _shrinktheweb_link_check($var) {
return $var && $var[0] != '#';
}