0

I would like to have specific text automatically (or manually) insert a tooltip.

For example, let's say I want every instance on the website of:

CMS

And have the tooltip say:

"Content Management System"

For drupal to automatically add this would be nice...but not asking for that much ;)

Can I add some kind of dynamic token?

The html can be static since the CSS is bootstrap CDN:

<p>Drupal is the best <strong><span data-placement="top" data-  toggle="tooltip" title="Content Management System" type="button">CMS</span></strong> in the world.</p>
1
0

You could write custom javascript that wraps all strings you want to replace with the <span data-toogle="tooltip"></span>

here's a tutorial

// Pretending we have more than one paragraph to look through
jQuery('p').each(function(){
    traverseChildNodes(this);
});

function traverseChildNodes(node) {

    var next;

    if (node.nodeType === 1) {

        // (Element node)

        if (node = node.firstChild) {
            do {
                // Recursively call traverseChildNodes
                // on each child node
                next = node.nextSibling;
                traverseChildNodes(node);
            } while(node = next);
        }

    } else if (node.nodeType === 3) {

        // (Text node)

        if (/CMS/.test(node.data)) {
            // Do something interesting here
            alert('FOUND A MATCH!');
        }

    }

}
0

In a custom module:

function MODULE_filter_info() {
  $filters = array();
  $filters['inline_terms'] = array(
    'title' => t('Term definitions inline'),
    'process callback' => '_MODULE_inline_terms_filter',

  );
  return $filters;
}

function _MODULE_inline_terms_filter($text, $filter, $format, $langcode, $cache, $cache_id) {
  switch ($filter->name) {
    case 'inline_terms':
      $glossary = **... get your glossary array list from somewhere**

      foreach($glossary as $key => $item) {
        $text = str_replace($key, '<span data-placement="top" data-toggle="tooltip" title="' . $item . '" type="button">' . $key . '</span>);
      }
      break;    
  }

  return $text;
}

If the requirement is whole word replacements only, then you can look at preg_replace, as in this post:

str_replace: Match whole word only

The benefits of using a text filter is that the content is left untouched in the database. Only when the content is rendered with a cache miss is the filter run over the text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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