You are here

token_embed_views.module in Token Embed Views 7

Same filename and directory in other branches
  1. 8 token_embed_views.module

Module to embed views using tokens

File

token_embed_views.module
View source
<?php

/**
 * @file
 * Module to embed views using tokens
 */

/**
 * Implements hook_token_info().
 */
function token_embed_views_token_info() {
  $info['types']['views'] = array(
    'name' => t('Views'),
    'description' => 'Tokens to embed views.',
  );
  $info['tokens']['views']['embed'] = array(
    'name' => t('Embed views'),
    'description' => t('Embed views using tokens. The following values may be appended to the token: view-name:display-id:arg1/arg2/arg3'),
    'dynamic' => TRUE,
  );
  return $info;
}

/**
 * Implements hook_tokens().
 */
function token_embed_views_tokens($type, $tokens, array $data = array(), array $options = array()) {
  if ($type == 'views') {
    $replacements = array();
    foreach ($tokens as $token => $key) {

      // Extract info.
      $args = explode(':', $token);

      // Remove the first element out as its not a required parameter for the views.
      $token_name = array_shift($args);

      // Fetch the view with all args passed to it.
      $replacements[$key] = call_user_func_array('views_embed_view', $args);
    }
    return $replacements;
  }
}

Functions