Commit 19b80962 authored by osman's avatar osman

Issue #2986470: Images not showing in RSS feeds

parent c27fb006
......@@ -37,9 +37,10 @@ function lazy_configuration_form($form, $form_state) {
$form['settings']['lazy_filter_image_fields'] = array(
'#type' => 'checkbox',
'#title' => t('Enable on image fields attached to fieldable entities. For example, content-types, blocks.'),
'#description' => t('Default is %val', array('%val' => ($defaults['lazy_filter_loadInvisible']) ? 'checked' : 'unchecked')),
'#description' => t('<div style="color: red;">This setting is disabled and will be removed on next release.<br><br>Now lazy-loading can be enabled for each field separataly via display settings.</div>'),
'#default_value' => variable_get('lazy_filter_image_fields', $defaults['lazy_filter_image_fields']),
'#return_value' => TRUE,
'#disabled' => TRUE,
);
$form['settings']['lazy_filter_placeholderSrc'] = array(
......@@ -47,6 +48,21 @@ function lazy_configuration_form($form, $form_state) {
'#title' => t('Placeholder image URL'),
'#description' => t('Default is %val', array('%val' => $defaults['lazy_filter_placeholderSrc'])),
'#default_value' => variable_get('lazy_filter_placeholderSrc', $defaults['lazy_filter_placeholderSrc']),
'#size' => 100,
);
$form['paths'] = array(
'#type' => 'fieldset',
'#title' => t('Disabled pages'),
'#description' => t('<p>There could be pages Lazy-loading should be disabled; set them here.</p>'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['paths']['lazy_disabled_paths'] = array(
'#type' => 'textarea',
'#title' => t('Lazy-loading is disabled for both <em>image fields</em> and <em>inline images/iframes</em> in content on following pages'),
'#default_value' => variable_get('lazy_disabled_paths', $defaults['lazy_disabled_paths']),
'#description' => t('Specify pages by using their paths. Enter one path per line.'),
);
$form['blazy'] = array(
......@@ -162,6 +178,9 @@ function lazy_configuration_form_submit($form, &$form_state) {
if (in_array($key, $bool_value)) {
$value = boolval($value);
}
if ('string' === gettype($value)) {
$value = trim($value);
}
variable_set($key, $value);
}
drupal_set_message(t('The configuration options have been saved.'));
......
......@@ -22,6 +22,7 @@ function lazy_install() {
'lazy_filter_validateDelay' => 25,
'lazy_filter_placeholderSrc' => 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==',
'lazy_filter_image_fields' => FALSE,
'lazy_disabled_paths' => 'rss.xml',
);
variable_set('lazy_filter_defaults', $defaults);
......@@ -61,6 +62,7 @@ function lazy_uninstall() {
variable_del('lazy_filter_placeholderSrc');
variable_del('lazy_filter_image_fields');
variable_del('lazy_library_installed');
variable_del('lazy_disabled_paths');
$module = 'lazy';
$name = 'lazy_filter';
......@@ -138,3 +140,39 @@ function lazy_update_7101() {
$blazy = libraries_detect('blazy');
variable_set('lazy_library_installed', $blazy['installed']);
}
/**
* New configuration option to set which pages should disable Lazy-loading.
*/
function lazy_update_7102() {
$defaults = variable_get('lazy_filter_defaults');
$defaults['lazy_disabled_paths'] = 'rss.xml';
variable_set('lazy_filter_defaults', $defaults);
variable_set('lazy_disabled_paths', $defaults['lazy_disabled_paths']);
}
/**
* Image fields now use display formatter to enable lazy-loading per field.
*/
function lazy_update_7103() {
$image_fields = variable_get('lazy_filter_image_fields');
if ($image_fields) {
$fields = array();
$field_map = field_info_field_map();
foreach ($field_map as $field_name => $field) {
if ($field['type'] === 'image') {
foreach ($field['bundles'] as $entity_type => $bundle) {
$field_instance = field_info_instance($entity_type, $field_name, $bundle[0]);
foreach ($field_instance['display'] as $display_name => $display) {
if ($field_instance['display'][$display_name]['type'] === 'image') {
$field_instance['display'][$display_name]['type'] = 'lazy';
$field_instance['display'][$display_name]['module'] = 'lazy';
field_update_instance($field_instance);
}
}
}
}
}
}
variable_del('lazy_filter_image_fields');
}
......@@ -68,8 +68,7 @@ function lazy_help($path, $arg) {
*/
function lazy_filter_info() {
$filters['lazy_filter'] = array(
'title' => t('Lazy-load'),
'description' => t('Lazy-load inline-images and/or iframes'),
'title' => t('Lazy-load images and IFRAMEs via bLazy'),
'default settings' => _filter_lazy_defaults(),
'process callback' => '_filter_lazy_process',
'cache' => TRUE,
......@@ -110,8 +109,9 @@ function _filter_lazy_process($text, $filter) {
$opt_src = (variable_get('lazy_filter_src') !== 'src') ? variable_get('lazy_filter_src') : 'data-filterlazy-src';
$opt_placeholderSrc = variable_get('lazy_filter_placeholderSrc');
$library_installed = variable_get('lazy_library_installed');
$disabled_paths = preg_split("/\s+/", variable_get('lazy_disabled_paths'));
if ($library_installed) {
if ($library_installed && !in_array(drupal_get_path_alias(), $disabled_paths)) {
$html_dom = filter_dom_load($text);
foreach ($opt_tags as $tag) {
......@@ -272,25 +272,200 @@ function lazy_library_get_version($library, $options) {
}
/**
* Implements hook_theme_registry_alter().
* Implements hook_field_formatter_info().
*/
function lazy_theme_registry_alter(&$theme_registry) {
if (variable_get('lazy_library_installed') && variable_get('lazy_filter_image_fields') && isset($theme_registry['image'])) {
$theme_registry['image']['function'] = 'theme_lazy_image';
function lazy_field_formatter_info() {
$formatters = array(
'lazy' => array(
'label' => t('Lazy-load image'),
'field types' => array(
'image',
),
'settings' => array(
'image_style' => '',
'image_link' => '',
),
),
);
return $formatters;
}
/**
* Implements hook_field_formatter_settings_form().
*/
function lazy_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$image_styles = image_style_options(FALSE, PASS_THROUGH);
$element['image_style'] = array(
'#title' => t('Image style'),
'#type' => 'select',
'#default_value' => $settings['image_style'],
'#empty_option' => t('None (original image)'),
'#options' => $image_styles,
);
$link_types = array(
'content' => t('Content'),
'file' => t('File'),
);
$element['image_link'] = array(
'#title' => t('Link image to'),
'#type' => 'select',
'#default_value' => $settings['image_link'],
'#empty_option' => t('Nothing'),
'#options' => $link_types,
);
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function lazy_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = array();
$image_styles = image_style_options(FALSE, PASS_THROUGH);
// Unset possible 'No defined styles' option.
unset($image_styles['']);
// Styles could be lost because of enabled/disabled modules that defines
// their styles in code.
if (isset($image_styles[$settings['image_style']])) {
$summary[] = t('Image style: @style', array(
'@style' => $image_styles[$settings['image_style']],
));
}
else {
$summary[] = t('Original image');
}
$link_types = array(
'content' => t('Linked to content'),
'file' => t('Linked to file'),
);
// Display this setting only if image is linked.
if (isset($link_types[$settings['image_link']])) {
$summary[] = $link_types[$settings['image_link']];
}
return implode('<br />', $summary);
}
/**
* Overrides theme_image().
* Implements hook_field_formatter_view().
*/
function lazy_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
// Check if the formatter involves a link.
if ($display['settings']['image_link'] == 'content') {
$uri = entity_uri($entity_type, $entity);
}
elseif ($display['settings']['image_link'] == 'file') {
$link_file = TRUE;
}
// Check if the formatter is set to lazy-load.
// if ($display['type'] === 'lazy') {}
foreach ($items as $delta => $item) {
if (isset($link_file)) {
$uri = array(
'path' => file_create_url($item['uri']),
'options' => array(),
);
}
$element[$delta] = array(
'#theme' => 'lazy_image_formatter',
'#item' => $item,
'#image_style' => $display['settings']['image_style'],
'#path' => isset($uri) ? $uri : '',
);
}
return $element;
}
/**
* Implements hook_theme().
*/
function lazy_theme() {
return array(
'lazy_image' => array(
'variables' => array(
'style_name' => NULL,
'path' => NULL,
'width' => NULL,
'height' => NULL,
'alt' => '',
'title' => NULL,
'attributes' => array(),
),
),
'lazy_image_formatter' => array(
'variables' => array(
'item' => NULL,
'path' => NULL,
'image_style' => NULL,
),
),
);
}
/**
* Returns HTML for an image.
*
* @param $variables
* An associative array containing:
* - path: Either the path of the image file (relative to base_path()) or a
* full URL.
* - width: The width of the image (if known).
* - height: The height of the image (if known).
* - alt: The alternative text for text-based browsers. HTML 4 and XHTML 1.0
* always require an alt attribute. The HTML 5 draft allows the alt
* attribute to be omitted in some cases. Therefore, this variable defaults
* to an empty string, but can be set to NULL for the attribute to be
* omitted. Usually, neither omission nor an empty string satisfies
* accessibility requirements, so it is strongly encouraged for code
* calling theme('image') to pass a meaningful value for this variable.
* - http://www.w3.org/TR/REC-html40/struct/objects.html#h-13.8
* - http://www.w3.org/TR/xhtml1/dtds.html
* - http://dev.w3.org/html5/spec/Overview.html#alt
* - title: The title text is displayed when the image is hovered in some
* popular browsers.
* - attributes: Associative array of attributes to be placed in the img tag.
*/
function theme_lazy_image($variables) {
$opt_skipClass = variable_get('lazy_filter_skipClass');
$opt_selector = ltrim(variable_get('lazy_filter_selector'), '.');
$opt_src = (variable_get('lazy_filter_src') !== 'src') ? variable_get('lazy_filter_src') : 'data-filterlazy-src';
$opt_placeholderSrc = variable_get('lazy_filter_placeholderSrc');
$disabled_paths = preg_split("/\s+/", variable_get('lazy_disabled_paths'));
if ($variables['style_name']) {
// Determine the dimensions of the styled image.
$dimensions = array(
'width' => $variables['width'],
'height' => $variables['height'],
);
image_style_transform_dimensions($variables['style_name'], $dimensions);
$variables['width'] = $dimensions['width'];
$variables['height'] = $dimensions['height'];
// Determine the URL for the styled image.
$variables['path'] = image_style_url($variables['style_name'], $variables['path']);
}
// Skip Blazy rendering if variables contain `.no-b-lazy` CSS class name.
if (!variable_get('lazy_library_installed') || (!empty($variables['attributes']['class']) && in_array($opt_skipClass, $variables['attributes']['class']))) {
// Skip Blazy rendering if...
//
// library is not available OR
// viewing a page listed in disabled pages OR
// element has the skip-class added.
if (
!variable_get('lazy_library_installed') ||
in_array(drupal_get_path_alias(), $disabled_paths) || (
!empty($variables['attributes']['class'])
&& in_array($opt_skipClass, $variables['attributes']['class'])
)
) {
return theme('image', $variables);
}
else {
......@@ -312,3 +487,54 @@ function theme_lazy_image($variables) {
return '<img' . drupal_attributes($attributes) . ' />';
}
}
/**
* Returns HTML for an image field formatter.
*
* @param $variables
* An associative array containing:
* - item: Associative array of image data, which may include "uri", "alt",
* "width", "height", "title" and "attributes".
* - image_style: An optional image style.
* - path: An array containing the link 'path' and link 'options'.
*
* @ingroup themeable
*/
function theme_lazy_image_formatter($variables) {
$item = $variables['item'];
$image = array(
'path' => $item['uri'],
);
if (array_key_exists('alt', $item)) {
$image['alt'] = $item['alt'];
}
if (isset($item['attributes'])) {
$image['attributes'] = $item['attributes'];
}
if (isset($item['width']) && isset($item['height'])) {
$image['width'] = $item['width'];
$image['height'] = $item['height'];
}
// Do not output an empty 'title' attribute.
if (isset($item['title']) && drupal_strlen($item['title']) > 0) {
$image['title'] = $item['title'];
}
if ($variables['image_style']) {
$image['style_name'] = $variables['image_style'];
}
$output = theme('lazy_image', $image);
// The link path and link options are both optional, but for the options to be
// processed, the link path must at least be an empty string.
if (isset($variables['path']['path'])) {
$path = $variables['path']['path'];
$options = isset($variables['path']['options']) ? $variables['path']['options'] : array();
// When displaying an image inside a link, the html option must be TRUE.
$options['html'] = TRUE;
$output = l($output, $path, $options);
}
return $output;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment