no_nbsp.module in No Non-breaking Space Filter 7
Same filename and directory in other branches
A filter module that deletes all non-breaking spaces.
File
no_nbsp.moduleView source
<?php
/**
* @file
* A filter module that deletes all non-breaking spaces.
*/
/**
* A filter module that deletes all non-breaking spaces.
*
* @mainpage
*
* @section project_page Project page
* - @link http://drupal.org/project/no_nbsp Drupal project "No
* (non-breaking space)" (no_nbsp) @endlink
*
* @section git_repository Git repository
* - @link http://drupalcode.org/project/no_nbsp.git Drupal git repository
* @endlink
*/
/**
* @defgroup drupal_hooks Drupal hooks
* @{
* Hooks used by the module.
*/
/**
* Implements hook_filter_info().
*/
function no_nbsp_filter_info() {
$filters['filter_no_nbsp'] = array(
'title' => t('No Non-breaking Space Filter'),
'description' => t('Delete all non-breaking space HTML entities.'),
'process callback' => '_no_nbsp_process',
'settings callback' => '_no_nbsp_settings',
'default settings' => array(
'preserve_placeholders' => FALSE,
),
'tips callback' => '_no_nbsp_tips',
'weight' => -99,
);
return $filters;
}
/**
* Implements hook_filter_FILTER_process().
*
* Process callback for hook_filter_info().
*
* @see no_nbsp_filter_info()
*/
function _no_nbsp_process($text, $filter, $format) {
return _no_nbsp_eraser($text, $filter->settings['preserve_placeholders']);
}
/**
* Implements hook_filter_FILTER_tips().
*
* Tips callback for hook_filter_info().
*
* @see no_nbsp_filter_info()
*/
function _no_nbsp_tips($filter, $format, $long = FALSE) {
$tips = array();
$tips[] = t('All non-breaking space HTML entities are replaced by blank space characters.');
if ($long) {
$tips[] = t('Multiple contiguous space characters are replaced by a single blank space character.');
}
return implode(' ', $tips);
}
/**
* Implements hook_field_formatter_info().
*/
function no_nbsp_field_formatter_info() {
return array(
'no_nbsp' => array(
'label' => t('No Non-breaking Space Filter'),
'field types' => array(
'text',
'text_long',
'text_with_summary',
),
),
);
}
/**
* Implements hook_field_formatter_view().
*/
function no_nbsp_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$settings = $display['settings'];
$element = array();
if ($display['type'] == 'no_nbsp') {
foreach ($items as $delta => $item) {
$element[$delta] = array(
'#markup' => _no_nbsp_eraser($item['value'], isset($settings['preserve_placeholders']) ? $settings['preserve_placeholders'] : FALSE),
);
}
}
return $element;
}
/**
* @} End of "defgroup drupal_hooks".
*/
/**
* Erase the string ' '.
*
* @param string $text
* A string to pass through the eraser.
* @param bool $preserve_placeholders
* Perserve non-breaking spaces, that serve as placeholders.
*
* @return string
* The erased string.
*/
function _no_nbsp_eraser($text, $preserve_placeholders = FALSE) {
if ($preserve_placeholders) {
// See https://stackoverflow.com/a/50301496.
$text = preg_replace("/([^>]) /ui", "\$1 ", $text);
}
else {
$text = preg_replace('/ /i', ' ', $text);
}
return preg_replace('/ +/i', ' ', $text);
}
/**
* Implements callback_filter_settings().
*
* Provides settings for the no_nbsp filter.
*
* @see filter_filter_info()
*/
function _no_nbsp_settings($form, &$form_state, $filter, $format, $defaults) {
$filter->settings += $defaults;
$settings['preserve_placeholders'] = array(
'#type' => 'checkbox',
'#title' => t('Preserve placeholders.'),
'#default_value' => isset($filter->settings['preserve_placeholders']) ? $filter->settings['preserve_placeholders'] : FALSE,
'#description' => t('A placeholder non-breaking space is surrounded by a HTML tag, for example <p>&nbsp;</p>.'),
);
return $settings;
}
Functions
Name | Description |
---|---|
no_nbsp_field_formatter_info | Implements hook_field_formatter_info(). |
no_nbsp_field_formatter_view | Implements hook_field_formatter_view(). |
no_nbsp_filter_info | Implements hook_filter_info(). |
_no_nbsp_eraser | Erase the string ' '. |
_no_nbsp_process | Implements hook_filter_FILTER_process(). |
_no_nbsp_settings | Implements callback_filter_settings(). |
_no_nbsp_tips | Implements hook_filter_FILTER_tips(). |