Since the token module is part of core in drupal 7, I think we should implement the tokens through the token api.

* This will make the code cleaner.
* We'll be able to use tokens from other modules when we want to.

Comments

zilverdistel’s picture

I started working on this. I implemented the needed hooks. The replacements are still left 'todo', but I got stuck on the architecture of the module.

The replacements happen in javacript, where - unfortunately - we cannot replace tokens through the token api. There should be a workaround by creating some ajax function for this, but thinking about it, I don't get why the bookmarklet is being generated by js.

This might be a whole other issue but I think this module doesn't need any js at all. Why not just save the values in the settings form? The bookmarklet can be generated in php, depending on the settings, when the form is sent to the browser. When everything is in php, we can fully leverage the benefits of the drupal api. Do you think it would be hard to change this? Or would you accept a patch that does this? If so, we should make a new issue for this.

Here are the hooks I implemented:

/**
 * Implements hook_token_info.
 */
function quickpost_bookmarklet_token_info(){
  $types = array();
  $tokens = array();

  $types['quickpost_bookmarklet'] = array(
    'name' => t('Quickpost bookmarklet'),
    'description' => t('Quickpost bookmarklet tokens.'),
  );

  $tokens['quickpost_bookmarklet']['title'] = array(
    'name' => t('Title'),
    'description' => t('Page title.'),    
  );
  $tokens['quickpost_bookmarklet']['url'] = array(
    'name' => t('Url'),
    'description' => t('Page url.'),    
  );
  $tokens['quickpost_bookmarklet']['selection'] = array(
    'name' => t('Selection'),
    'description' => t('User-selected text on the page.'),    
  );
  
  return array('types' => $types, 'tokens' => $tokens);
}

/**
 * Implements hook_tokens.
 */
function quickpost_bookmarklet_tokens($type, $tokens, array $data = array(), array $options = array()){
  $replacements = array();
  
  if ($type == 'quickpost_bookmarklet') {
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'title':
          $replacements[$original] = 'todo';
        break;
        case 'url':
          $replacements[$original] = 'todo';
        break;
        case 'selection':
          $replacements[$original] = 'todo';
        break;
      }
    }
  }

  return $replacements;
}
zilverdistel’s picture

Status: Active » Needs review
BrockBoland’s picture

I see you opened #1912830: Convert js code to php to handle the PHP rewrite of some of the JS functionality. Finishing that will be step one here, and will also make it a lot easier to implement #1465918: Mapping of the content type field in the bookmarklet configuration form.