cookie_content_blocker_media.module in Cookie Content Blocker 7
Same filename and directory in other branches
Contains the main module code for Cookie content blocker - Media.
File
modules/cookie_content_blocker_media/cookie_content_blocker_media.moduleView source
<?php
/**
* @file
* Contains the main module code for Cookie content blocker - Media.
*/
/**
* Implements hook_help().
*/
function cookie_content_blocker_media_help($path, array $arg) {
if ($path !== 'admin/help#cookie_content_blocker_media') {
return '';
}
$output = file_get_contents(drupal_get_path('module', 'cookie_content_blocker_media') . '/README.md');
return module_exists('markdown') ? filter_xss_admin(module_invoke('markdown', 'filter', 'process', 0, -1, $output)) : '<pre>' . check_plain($output) . '</pre>';
}
/**
* Implements hook_menu().
*/
function cookie_content_blocker_media_menu() {
$items = array();
$items['admin/config/system/cookie_content_blocker/media'] = array(
'title' => 'Media',
'description' => 'Manage settings related to Cookie content blocker - Media.',
'access arguments' => array(
'administer cookie content blocker',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'cookie_content_blocker_media_settings_form',
),
'type' => MENU_LOCAL_TASK,
'file' => 'cookie_content_blocker_media.admin.inc',
);
return $items;
}
/**
* Implements hook_image_default_styles().
*/
function cookie_content_blocker_media_image_default_styles() {
return array(
'blocked_media_teaser' => array(
'label' => 'Blocked media teaser (480x480)',
'effects' => array(
array(
'name' => 'image_scale',
'data' => array(
'width' => 480,
'height' => 480,
'upscale' => 1,
),
'weight' => 0,
),
),
),
);
}
/**
* Implements hook_file_view().
*/
function cookie_content_blocker_media_file_view($file, $view_mode, $langcode) {
try {
$provider = cookie_content_blocker_media_get_provider($file);
} catch (MediaInternetNoHandlerException $e) {
return;
}
if (empty($provider) || !cookie_content_blocker_media_provider_is_blocked($provider)) {
return;
}
$build =& $file->content;
if (empty($build['#pre_render'])) {
$build['#pre_render'] = array();
}
$build['#pre_render'][] = 'cookie_content_blocker_element_pre_render';
$build['#cookie_content_blocker'] = array(
'blocked_message' => cookie_content_blocker_media_provider_blocked_message($provider),
);
$show_preview = cookie_content_blocker_media_provider_variable_get($provider, 'show_preview');
if ($show_preview) {
$build['#cookie_content_blocker']['preview'] = cookie_content_blocker_media_get_preview($file);
}
}
/**
* Get the media internet provider info for a file.
*
* @param object $file
* The file.
*
* @return array
* The provider info or an empty array if not found.
*
* @throws \MediaInternetNoHandlerException
*/
function cookie_content_blocker_media_get_provider($file) {
$file_url = file_create_url($file->uri);
if (empty($file_url)) {
return array();
}
$provider = media_internet_get_provider($file_url);
$class_name = get_class($provider);
$provider_info = array_filter(media_internet_get_providers(), function ($class) use ($class_name) {
return $class === $class_name;
}, ARRAY_FILTER_USE_KEY);
$provider_info = reset($provider_info);
return $provider_info ?: array();
}
/**
* Get the (local) preview for a blocked media file.
*
* @param object $file
* The file object to retrieve a thumbnail for.
*
* @return array
* The preview render array.
*/
function cookie_content_blocker_media_get_preview($file) {
$wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
if (empty($wrapper) || !is_callable(array(
$wrapper,
'getLocalThumbnailPath',
))) {
return array();
}
try {
$provider = cookie_content_blocker_media_get_provider($file);
} catch (MediaInternetNoHandlerException $e) {
return array();
}
if (empty($provider)) {
return array();
}
return array(
'#theme' => 'image_style',
'#style_name' => cookie_content_blocker_media_provider_variable_get($provider, 'preview_style', 'blocked_media_teaser'),
'#path' => $wrapper
->getLocalThumbnailPath(),
'#alt' => $file->override['attributes']['alt'] ?? $file->filename,
);
}
/**
* Check if media for a given provider should be blocked.
*
* @param array $provider
* The media provider info.
*
* @return bool
* Whether media for the given provider should be blocked.
*/
function cookie_content_blocker_media_provider_is_blocked(array $provider) {
$is_blocked = cookie_content_blocker_media_provider_variable_get($provider, 'blocked');
return !empty($is_blocked);
}
/**
* Get the value for a media provider variable.
*
* @param array $provider
* The media provider info.
* @param string $variable
* A unique name for the variable.
* @param mixed $default
* The default value to use if this variable has never been set.
*
* @return mixed
* The variable value.
*/
function cookie_content_blocker_media_provider_variable_get(array $provider, $variable, $default = NULL) {
$name = _cookie_content_blocker_media_provider_variable_name($provider, $variable);
return variable_get($name, $default);
}
/**
* Get the blocked message that should be shown for the given provider.
*
* Falls back to the default message if no specific message is defined for
* the given provider.
*
* @param array $provider
* The media provider info.
*
* @return string
* The blocked message that should be used for the given provider.
*/
function cookie_content_blocker_media_provider_blocked_message(array $provider) {
$blocked_message = cookie_content_blocker_media_provider_variable_get($provider, 'blocked_message');
return !empty($blocked_message) ? $blocked_message : _cookie_content_blocker_blocked_message();
}
/**
* Get the appropriate variable name for a media provider.
*
* @param array $provider
* The media provider info.
* @param string $variable
* A unique name for the variable.
*
* @return string
* The composed variable name.
*/
function _cookie_content_blocker_media_provider_variable_name(array $provider, $variable) {
return sprintf('cookie_content_blocker_media_provider_%s_%s', preg_replace('@[^a-z0-9_]+@', '_', strtolower($provider['title'])), $variable);
}
Functions
Name | Description |
---|---|
cookie_content_blocker_media_file_view | Implements hook_file_view(). |
cookie_content_blocker_media_get_preview | Get the (local) preview for a blocked media file. |
cookie_content_blocker_media_get_provider | Get the media internet provider info for a file. |
cookie_content_blocker_media_help | Implements hook_help(). |
cookie_content_blocker_media_image_default_styles | Implements hook_image_default_styles(). |
cookie_content_blocker_media_menu | Implements hook_menu(). |
cookie_content_blocker_media_provider_blocked_message | Get the blocked message that should be shown for the given provider. |
cookie_content_blocker_media_provider_is_blocked | Check if media for a given provider should be blocked. |
cookie_content_blocker_media_provider_variable_get | Get the value for a media provider variable. |
_cookie_content_blocker_media_provider_variable_name | Get the appropriate variable name for a media provider. |