Index: 399648-cck-arg.patch =================================================================== Index: views_attach_plugin_display_node_content.inc =================================================================== --- views_attach_plugin_display_node_content.inc (.../all/modules/views_attach) (revision 17) +++ views_attach_plugin_display_node_content.inc (.../default/modules/views_attach) (working copy) @@ -10,7 +10,7 @@ $options['types'] = array('default' => array()); $options['modes'] = array('default' => array('full')); - $options['default_argument'] = array('default' => 'nid'); + $options['default_argument'] = array('default' => ''); $options['show_title'] = 0; return $options; @@ -57,11 +57,12 @@ $weight = 10; } + //TODO: rename "argument" to "arguments string"? $default_argument = $this->get_option('default_argument'); $options['default_argument'] = array( 'category' => 'node_content', 'title' => t('Default argument'), - 'value' => empty($default_argument) ? t('None') : $default_argument, + 'value' => empty($default_argument) ? t('None') : check_plain($default_argument), ); $options['show_title'] = array( @@ -104,11 +105,16 @@ case 'default_argument': $form['#title'] .= t('Default argument'); $form['default_argument'] = array( - '#type' => 'checkbox', - '#title' => t('Provide the current node id as a default argument.'), - '#default_value' => $this->get_option('default_argument') === 'nid', - '#return_value' => 'nid', + '#type' => 'textfield', + '#default_value' => $this->get_option('default_argument'), + '#description' => t('Separate arguments with "/".'), ); + // Add the token help to the form. + if ($token_type = views_attach_get_token_type($form_state['view']->base_table)) { + $form['help'] = array( + '#value' => t('You may use token replacement to provide arguments based on the node. Token replacements require that NID is the first argument of the URL for the view.') . theme('token_help', $token_type), + ); + } break; case 'show_title': @@ -141,7 +147,33 @@ break; } } + /** + * We have to run token replacement before the arguments are used. + */ + function pre_execute() { + // Call the parent setup function so we do not lose data. + parent::pre_execute(); + $args = $this->view->args; + $token_string = $this->view->display_handler->get_option('default_argument'); + if (empty($args) || empty($token_string)) { + return; + } + $node_types = $this->view->display_handler->get_option('types'); + $node = node_load($args[0]); + if (!in_array($node->type, $node_types)) { + return; + } + // Now do the token replacement. + $token_type = views_attach_get_token_type($this->view->base_table); + $new_args = views_attach_get_arguments_from_token_string($token_string, $token_type, $node); + // We have to be carfeul to only replace arguments that have tokens. + foreach ($new_args as $key => $value) { + $args[$key] = $value; + } + $this->view->args = $args; + } + /** * The display block handler returns the structure necessary for a block. */ Index: views_attach_plugin_display_profile.inc =================================================================== --- views_attach_plugin_display_profile.inc (.../all/modules/views_attach) (revision 17) +++ views_attach_plugin_display_profile.inc (.../default/modules/views_attach) (working copy) @@ -10,7 +10,7 @@ $options['weight'] = array('default' => 10); $options['category'] = array('default' => NULL); - $options['default_argument'] = array('default' => 'uid'); + $options['default_argument'] = array('default' => ''); return $options; } @@ -50,10 +50,11 @@ 'value' => $weight, ); + $default_argument = $this->get_option('default_argument'); $options['default_argument'] = array( 'category' => 'profile', 'title' => t('Default argument'), - 'value' => $this->get_option('default_argument') === 'uid' ? t('Yes') : t('No'), + 'value' => empty($default_argument) ? t('None') : check_plain($default_argument), ); } @@ -84,11 +85,16 @@ case 'default_argument': $form['#title'] .= t('Default argument'); $form['default_argument'] = array( - '#type' => 'checkbox', - '#title' => t("Provide the current user id as a default argument."), - '#default_value' => $this->get_option('default_argument') === 'uid', - '#return_value' => 'uid', + '#type' => 'textfield', + '#default_value' => $this->get_option('default_argument'), + '#description' => t('Separate arguments with "/".'), ); + // Add the token help to the form. + if ($token_type = views_attach_get_token_type($form_state['view']->base_table)) { + $form['help'] = array( + '#value' => '

'. t('You may use token replacement to provide arguments based on the user. Token replacements require that UID is the first argument of the URL for the view.') .'

'. theme('token_help', $token_type), + ); + } break; } } @@ -110,6 +116,27 @@ } /** + * We have to run token replacement before the arguments are used. + */ + function pre_execute() { + // Call the parent setup function so we do not lose data. + parent::pre_execute(); + $args = $this->view->args; + $token_string = $this->view->display_handler->get_option('default_argument'); + if (empty($args) || empty($token_string)) { + return; + } + + $user = user_load($args[0]); + + // Now do the token replacement. + $token_type = views_attach_get_token_type($this->view->base_table); + $new_args = views_attach_get_arguments_from_token_string($token_string, $token_type, $user); + + $this->view->args = $new_args; + } + + /** * The display block handler returns the structure necessary for a block. */ function execute() { Index: views_attach.module =================================================================== --- views_attach.module (.../all/modules/views_attach) (revision 17) +++ views_attach.module (.../default/modules/views_attach) (working copy) @@ -50,8 +50,7 @@ foreach ($views as $info) { $view = views_get_view($info['name']); $view->set_display($info['display']); - $args = $view->display_handler->get_option('default_argument') === 'uid' ? array($account->uid) : array(); - $result = $view->execute_display($info['display'], $args); + $result = $view->execute_display($info['display'], $account->uid); if (!empty($result)) { $account->content[$view->name . '_' . $info['display']] = array( '#type' => 'user_profile_category', @@ -91,8 +90,7 @@ foreach ($views as $info) { $view = views_get_view($info['name']); $view->set_display($info['display']); - $args = $view->display_handler->get_option('default_argument') === 'nid' ? array($node->nid) : array(); - $result = $view->execute_display($info['display'], $args); + $result = $view->execute_display($info['display'], $node->nid); if (!empty($result)) { // This is not really a form item, but by using item here we get // all of the built in theming of #title and don't need to introduce @@ -220,4 +218,50 @@ ); } return $modes; -} \ No newline at end of file +} + +/** + * Get view arguments array from string that contains tokens + * + * @param $string + * The token string defined by the view. + * @param $type + * The token type. + * @param $object + * The object being used for replacement data (typically a node). + * @return + * An array of argument values. + * + * @todo: security? + */ +function views_attach_get_arguments_from_token_string($string, $type, $object) { + $args = trim($string); + + if (empty($args)) { + return array(); + } + + $args = token_replace($args, $type, $object); + return explode('/', $args); +} + +/** + * Get the proper node type data based on the base table. + * + * @param $base_table + * A string representing the base table of the view. + * @return + * A string indicating the token type for this table. + */ +function views_attach_get_token_type($base_table) { + // TODO: add other object types? + switch ($base_table) { + case 'users': + $token_type = 'user'; + break; + default: + $token_type = 'node'; + break; + } + return $token_type; +} Index: README.txt =================================================================== --- README.txt (.../all/modules/views_attach) (revision 17) +++ README.txt (.../default/modules/views_attach) (working copy) @@ -21,6 +21,7 @@ - Drupal 6 - Views 2 +- Token AUTHOR AND CREDIT @@ -29,3 +30,6 @@ Maintainer: Larry Garfield "Crell" (http://drupal.org/user/26398) + +Token integration: +Alexander Makarov "Sam Dark" (http://drupal.org/user/281132) \ No newline at end of file Index: views_attach.info =================================================================== --- views_attach.info (.../all/modules/views_attach) (revision 17) +++ views_attach.info (.../default/modules/views_attach) (working copy) @@ -3,6 +3,7 @@ description = Provides new Views display types that can be attached to nodes or users. core = 6.x dependencies[] = views +dependencies[] = token package = Views ; Information added by drupal.org packaging script on 2009-02-23