drupal tokens

satya - Friday, July 15, 2011 5:00:52 PM

what are drupal tokens?

what are drupal tokens?

Search for: what are drupal tokens?

satya - Friday, July 15, 2011 5:01:59 PM

They are

They specially formated strings in content. ex

[sometoken:someargs]

Then these tokens will be expanded. on the serverside.

satya - Friday, July 15, 2011 5:04:29 PM

Steps to allow these


have a filter_info hook
provide a filter process method
in that method call token_replace
implement token_info hook
return appropriate text from token_info

satya - Friday, July 15, 2011 5:04:50 PM

filter_info token_replace token_info

filter_info token_replace token_info

Search for: filter_info token_replace token_info

satya - Friday, July 15, 2011 5:05:27 PM

A drupal view output as a token

A drupal view output as a token

Search for: A drupal view output as a token

satya - Saturday, July 16, 2011 9:00:09 AM

api: hook_token_info

api: hook_token_info

satya - Saturday, July 16, 2011 9:58:56 AM

what is the use of registering tokens in hook_token_info in drupal?

what is the use of registering tokens in hook_token_info in drupal?

Search for: what is the use of registering tokens in hook_token_info in drupal?

what is the point? In the structure of [tokentype:token] the token can be anything including keys or names to any database content. It can be even a URI. In that case this token is an indefinite or open "set". So what is the benefit of registering. Is it just for documentation?

satya - Saturday, July 16, 2011 10:01:24 AM

what is the point of registering data needed in hook_token_info in drupal?

what is the point of registering data needed in hook_token_info in drupal?

Search for: what is the point of registering data needed in hook_token_info in drupal?

This parameter data needed is expected to provide the key name of the associative array that will be passed for this token type. However in the code we are expected to key the array ourselves. So why say it before? Is it just documentation? Is it enforced? will it throw an error if the data needed is specified but not available in the associative array?

satya - Saturday, July 16, 2011 12:38:54 PM

drupal blocks as tokens

drupal blocks as tokens

Search for: drupal blocks as tokens

satya - Saturday, July 16, 2011 12:43:38 PM

blog:How to render a block in the middle of a node.

blog:How to render a block in the middle of a node.

satya - Saturday, July 16, 2011 12:44:06 PM

drupal module_invoke

drupal module_invoke

Search for: drupal module_invoke

satya - Saturday, July 16, 2011 12:47:28 PM

link views module

link views module

satya - Saturday, July 16, 2011 1:08:06 PM

use php to insert views into a php template

use php to insert views into a php template

satya - Saturday, July 16, 2011 1:12:44 PM

possible answers


views_embed_view
view->execute_display
views_get_view

satya - Saturday, July 16, 2011 1:13:09 PM

drupal how to execute a view

drupal how to execute a view

Search for: drupal how to execute a view

satya - Saturday, July 16, 2011 2:03:14 PM

this is a good link to embed a view anywhere

this is a good link to embed a view anywhere

satya - Saturday, July 16, 2011 2:03:58 PM

To see the machnine name of a view


sturcture
views
Hover the mouse on each view

satya - Saturday, July 16, 2011 2:04:22 PM

drupal tokens for views

drupal tokens for views

Search for: drupal tokens for views

satya - Saturday, July 16, 2011 2:08:56 PM

The tokens module??

The tokens module??

Search for: The tokens module??

you can also find here all the modules that use tokens.

satya - Tuesday, July 19, 2011 1:32:46 PM

hook_filter_info api

hook_filter_info api

Start here to write your own token.

satya - Tuesday, July 19, 2011 1:50:26 PM

drupal text format and filters

drupal text format and filters

Search for: drupal text format and filters

satya - Tuesday, July 19, 2011 1:51:52 PM

Specifying the allowed formats for user input

Specifying the allowed formats for user input

satya - Tuesday, July 19, 2011 1:57:24 PM

Here is how you add a new text format

Here is how you add a new text format

satya - Tuesday, July 19, 2011 2:00:39 PM

How are text formats and filters related in Drupal?

Here is how.

Modules will register a series of functions as filters. Then Druapl allows you (through administration) to choose a set of filters as a particular text format. You can create new text formats by grouping these filters as you wish.

Out of the box drupal may give you a set of text formats such as filtered htm, full html, plain text etc.

you can change these text format behaviors by either adding a new filter or removing an exisiting filter.

here is how you can access the admin panel where you can configure your text formats for content

satya - Tuesday, July 19, 2011 2:02:05 PM

how to get to text formats admin


login
configuration
text formats
configuratio: to add/remove filters
Add text foramt: to create a new one

here is how you can write a hook to register your own filter.

satya - Tuesday, July 19, 2011 2:07:10 PM

You will need to provide a hook_filter_info


function yourModule_filter_info() {
  $filters['yourModule_token_filter'] = array(
    'title' => t('A Token Filter'),
    'description' => t('Calls token_replace on text.'),
    'process callback'  => 'yourModule_your_process_method',
  );
  return $filters;
}

This will register a filter called

yourModule_token_fiter

with drupal. You will be able to see this name in the text formats admin of Drupal.

Here is how the function that gets called to do the filtering

satya - Tuesday, July 19, 2011 2:09:53 PM

the method to process tokens


function yourModule_your_process_method($text, $filter, $format) 
{
  return token_replace($text);
}

If your intent is to enable Drupal tokens then you will call the built in token_replace method. Otherwise you are free to do what ever is necessary to transform the incoming text.

My goal is to do the tokens. So I will always call token_replace. This method token_replace uses its own set of hooks to enable specific tokens that follow the syntax of

[tokentype:token]

I will show you this in a minute.

satya - Tuesday, July 19, 2011 2:19:28 PM

Here is how I tell token_replace that I am interested in a token called

Here is how I tell token_replace that I am interested in a token called

[insertView:viewname:displayname]

function content_tokens_token_info() 
{
  $info['types']['insertView'] = array(
    'name' => t('Insert a view'),
    'description' => t('[insertView:viewname:displayname]'),
  );
  $info['tokens']['insertView']['default'] = array(
    'name' => t('Default'),
    'description' => t("Dont know how this is used"),
  );
  return $info;
}

satya - Tuesday, July 19, 2011 2:44:15 PM

Here is how the tokens are expanded: hook_tokens


function yourModule_tokens($type, 
    $tokens, 
    array $data = array(), 
    array $options = array()) 
{
    if ($type == 'insertview')
    {
      return insertViewTokenCallback($type, $tokens);
    }
    else(...for other types of tokens)
}
    

/****************************************************
* [insertview:physical_view_name:machine_display_name]
****************************************************/
function insertViewTokenCallback($type, $tokens)
{
    //dpm($tokens);
    foreach ($tokens as $key => $value) {
        // dpm($key); //physical_view_name:machine_display_name
        // dpm($value); //[insertview:physical_view_name:machine_display_name]
        if(strpos($key, ':') > 0) 
        {
            //if you have view name and also display name 
            $viewArray = explode(':', $key);
            $view_id = $viewArray[0]; 
            $view_display_id = $viewArray[1];
            $viewstr = views_embed_view($view_id, $view_display_id);
            $replacements[$value] = $viewstr;
            return $replacements;
        } else {
            //if you have jsut view name  
            $viewstr = views_embed_view($key);
            $replacements[$value] = $viewstr;
            return $replacements;
        }//eof-if-else
      }//eof-foreach
}//eof-function

satya - Tuesday, July 19, 2011 2:48:32 PM

Key take aways

1. Have your module provide your own filter with hook_filter_info

2. Enable your text format to use this filter. or define a new one if you dont have one

3. use token_replace to process text

4. override hook_token_info to provide token types and tokens

5. override hook_tokens to enable the processing of your tokens