How to obscure email and other text using signwriter and a cck formatter

Posted by: 
Dominique De Cooman

Here is an example on how to obscure email addresse (or other tekst) using http://drupal.org/project/signwriter in a formatter. You can use it on the cck field overview as a formatter (or in display suite).

<?php
//cck formatters
/**
 * Implementation of hook_field_formatter_info(),.
 */
function your_module_field_formatter_info() {
  return array(
    
'text_as_image' => array(
      
'label' => t('Text as image'),
      
'field types' => array('email''text'),
      
'multiple values' => CONTENT_HANDLE_MODULE,
    ),
  );
}

/**
 * Implementation of hook_theme().
 */
function your_module_theme() {
  
$theme_functions['your_module_formatter_text_as_image'] = array (
    
'arguments' => array('element' => NULL),
  );

  return 
$theme_functions;
}

/**
 * Themes an image from text
 */
function theme_your_module_formatter_text_as_image($element) {
  
// Set up signwriter profile    
  
$profile->fontfile drupal_get_path('module''signwriter'). '/Arial.TTF';//See this issue why its like this (<a href="http://drupal.org/node/606670">http://drupal.org/node/606670</a>)
  
$profile->fontsize 10;
  
$profile->foreground '000000';
  
$profile->background 'ffffff';
  
$profile->maxwidth 600;
  
$profile->transparent true;
  
$profile->imagetype 'png';
  
$profile->disable_span true;
  
  
$string_to_obscure $element[0]['#item']['safe']; 
    
  
$textsignwriter_title_convert($string_to_obscure$profile);
  
//Some extra logic if it is an email    
  
$output preg_replace('/alt="[0-9a-zA-Z!@#\$%^&\*~\-\.\+\_\/=\?\|\{\}}]+"/','alt="Email Address"',$text);
    
  return 
$output;
}
?>

The code in the formatter is taken from this example http://dominiquedecooman.com/bookmark/obscure-email-adresses-drupal-usin...

Comments

How to obscure email and other text using signwriter and a cck formatter

To use signwritter on sites with lots of nodes, meaning lots of images check out this issue http://drupal.org/node/880006

How to obscure email and other text using signwriter and a cck formatter

If the element has no data, this wil create an empty image.
This means the label for this field will always be shown.
You should check for data before you create the image

How to obscure email and other text using signwriter and a cck formatter

That indeed correct. A check should be performed. Above function should be.

<?php

/**
 * Themes an image from text
 */
function theme_your_module_formatter_text_as_image($element) {
  
//Validation
  
$string_to_obscure $element[0]['#item']['safe'];
  if (!
$string_to_obscure) {
    return;
  }

  
// Set up signwriter profile    
  
$profile->fontfile drupal_get_path('module''signwriter'). '/Arial.TTF';//See this issue why its like this (<a href="<a href="http://drupal.org/node/606670"">http://drupal.org/node/606670"</a> title="<a href="http://drupal.org/node/606670">http://drupal.org/node/606670</a>">http://drupal.org/node/606670">http://drupal.org/node/606670</a></a>)
  
$profile->fontsize 10;
  
$profile->foreground '000000';
  
$profile->background 'ffffff';
  
$profile->maxwidth 600;
  
$profile->transparent true;
  
$profile->imagetype 'png';
  
$profile->disable_span true
    
  
$textsignwriter_title_convert($string_to_obscure$profile);
  
//Some extra logic if it is an email    
  
$output preg_replace('/alt="[0-9a-zA-Z!@#\$%^&\*~\-\.\+\_\/=\?\|\{\}}]+"/','alt="Email Address"',$text);
    
  return 
$output;
}
?>

Add new comment

Who are you?


By knowing who you are you will be directed to a page where we have displayed all our drupal services relevant to your professional identity.