Wysiwyg button with form

Print

At my work i recently had the opportunity to build a wysiwyg plugin in drupal for formatting a token we had created for a customer. This precented a number of challenges which I though others might also have, so I will try to share them here.

This will be a four step tutorial for drupal 7

Step 1 - building the module

First step is to create our module so as always we need an info file:

name = Ting token
description = Provides tokens for inserting ting objects in text fields.
core = 7.x
package = Ding!
files[] = ting_token.module
files[] = ting_token.token.inc

I will presume that you are familier with the setup of an info file so I won't go into details here. Now I will go into the token file for this module.

For these tokens we wanted a format which would insert a view_mode and one or more entities to be rendered. First we need a token file with the file name hook_token_info

  1. function example_token_info() {
  2.   $type = array(
  3.     'name' => t('Example entity'),
  4.     'description' => t('Display an entity.'),
  5.   );
  6.  
  7.   // Core tokens for nodes.
  8.   $consent['example'] = array(
  9.     'name' => t("Example entity"),
  10.     'description' => t("Show an entity."),
  11.   );
  12.  
  13.   return array(
  14.     'types' => array('example' => $type),
  15.     'tokens' => array('example' => $consent),
  16.   );
  17. }

Next up we need to handle the tokens once inserted by defining hook_tokens

  1. function example_tokens($type, $tokens, array $data = array(), array $options = array()) {
  2.   $url_options = array('absolute' => TRUE);
  3.   if (isset($options['language'])) {
  4.     $url_options['language'] = $options['language'];
  5.     $language_code = $options['language']->language;
  6.   }
  7.   else {
  8.     $language_code = NULL;
  9.   }
  10.   $sanitize = !empty($options['sanitize']);
  11.   $replacements = array();
  12.   if ($type == 'example') {
  13.     foreach ($tokens as $name => $original) {
  14.       $args = explode(':', $name);
  15.       $view_mode = array_shift($args);
  16.       $eid = implode(':', $args);
  17.       if(strpos($eid, ',') !== FALSE) {
  18.         $eids = explode(',', rawurldecode($eid));
  19.       } else {
  20.         $eids = array(rawurldecode($eid));
  21.       }
  22.      
  23.       $entities = entity_load('example', $eids);
  24.       $output = '<div class="example-inline-list">';
  25.       foreach($entities as $id => $entity) {
  26.         $object = entity_view($entity, $view_mode);
  27.         if($view_mode == 'list_item') {
  28.           $object['#attributes']['class'][] = format_string('compact');
  29.         }
  30.         $output .= drupal_render($object);
  31.       }
  32.       $output .= '</div>';
  33.       $replacements[$original] = $output;
  34.     }
  35.   }
  36.  
  37.   return $replacements;
  38. }

For this customer our entities are ting entities so we are using specialized functions for these, but this code should work for most purposes. You might want to specialize the rendering of entities to fit your needs.

For this part of the module we actually don't need to write anything in our module file, but we will need it for the next part.

Category: 

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.