0

I have trouble converting my custom modules in D7. Found a how-to here http://drupal.org/node/224333 but not so clear to me. This how my code in D6.

/**
 * Implementation of hook_filter().
 */
function custom_inline_filter($op, $delta = 0, $format = -1, $text = '') {

  switch ($op) {
    case 'list':
      return array(0 => t('Custom Inline Filter'));

    case 'description':
      return t('Substitutes @inline tags with the uploaded inline images of the node.', array('@inline' => '<!-- INLINE -->'));

    case 'settings':
      break;

    case 'no cache':
      return FALSE;

    case 'prepare':    
      return $text;

    case 'process':
      return custom_inline_substitute($text);
      break;

    default:
      return $text;
  }
}


/**
 * Substitute inline image placeholder comments with inline image divs.
 * This function is usually called by hook_filter.process but also by template.php (used for RSS).
 *
 * @param $content
 * @param $node_id
 * @param $rss
 */
function custom_inline_substitute($content, $node_id = NULL, $rss = FALSE) {

  // Get nid from argument

  if ($node_id) {
    $nid = $node_id;
  }
  // Get nid when a node is displayed

  elseif (arg(0) == 'node' && is_numeric(arg(1))) {
    $nid = arg(1);
  }
  else {
    return $content;
  }

  // Instantiate inline image class

  $inlineImg = new CustomInlineImage($nid, $rss);
  return $inlineImg->substitute($content);
}


/**
 * Implementation of hook_filter_tips().
 */
function custom_inline_filter_tips($delta, $format, $long = FALSE) {

  if ($long) {
    return t('You may link to uploaded images within the current node using the wysiwyg editor toolbar icons.
      The Custom Inline Filter is to replace added image placeholders with uploaded inline images.'
    );
  }
  else {
    return t('Use the wysiwyg editor toolbar icons to display uploaded images inline.');
  }
}
1
  • I am relatively new to Drupal and still learning API which a bit new to me.
    – user8012
    Jul 4, 2012 at 9:16

3 Answers 3

3

The code you should use in Drupal 7 is similar to the following one.

function custom_inline_filter_info() {
  $filters['custom_inline_filter'] = array(
    'title' => t('Custom Inline Filter'), 
    'description' => t('Substitutes @inline tags with the uploaded inline images of the node.', array('@inline' => '<!-- INLINE -->')),
    'cache' => TRUE, 
    'process callback' => 'custom_inline_substitute', 
    'tips callback' => 'custom_inline_filter_tips',
  );

  return $filters;
}

function custom_inline_substitute($text, $filter, $format, $langcode, $cache, $cache_id) {
  // …
}

function custom_inline_filter_tips($filter, $format, $long) {
  if ($long) {
    return t('You may link to uploaded images within the current node using the wysiwyg editor toolbar icons.
      The Custom Inline Filter is to replace added image placeholders with uploaded inline images.'
    );
  }
  else {
    return t('Use the wysiwyg editor toolbar icons to display uploaded images inline.');
  }
}

The documentation for the input filter implementations is provided in more than one page:

For the parameters passed to the callbacks defined from an input filter module, see hook_filter_FILTER_settings(), hook_filter_FILTER_prepare(), hook_filter_FILTER_process(), and hook_filter_FILTER_tips().
In particular, it is relevant to know that:

  • $filter is a filter object, not a string that identifies the input filter; it contains information about the input filter, but what an input filter implementation normally uses is $filter->settings, which is an array of settings defined from the input filter.
  • $format is a format object, described in filter_format_save(); it doesn't contain information that an input filter normally uses.

To notice that only the first one is actually a hook; the others are callbacks. The documentation shows them as hooks because a limit of the module used to add documentation pages on Drupal.org API: A documentation page is added only if the function is contained in a module (a Drupal core module, or a third-party module), or it is a hook described in a .api.php file.

2
  • After clearing the cache the page generated a fatal error. Recoverable fatal error: Object of class stdClass could not be converted to string in DatabaseStatementBase->execute()
    – user8012
    Jul 4, 2012 at 10:11
  • Your code is trying to use an object as if it were a string. Verify you are not using any string operator with an object, or that you are passing an object to a function that is expecting a string.
    – apaderno
    Jul 4, 2012 at 11:55
1

hook_filter() has been replaced with hook_filter_info(); the docs page has all the information you need on how to build a valid filter info array for Drupal 7.

hook_filter_tips() was removed in favour of the tips callback property of the array you return from hook_filter_info(). The basic example from the docs page should help it make sense, but there's a more advanced example on the same page if this is too simplistic:

$filters['filter_autop'] = array(
  'title' => t('Convert line breaks'), 
  'description' => t('Converts line breaks into HTML (i.e. &lt;br&gt; and &lt;p&gt;) tags.'), 
  'process callback' => '_filter_autop', 
  'tips callback' => '_filter_autop_tips',
);
0
1

You can compare the D6 and D7 version of the node linke module. It's a filter module that has been ported from D6 to D7.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.