? ._399648-cck-arg_4.patch ? 399648-cck-arg_4.patch ? views_attach.patch ? views_attach_cck.patch Index: README.txt =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/views_attach/README.txt,v retrieving revision 1.1 diff -u -p -r1.1 README.txt --- README.txt 13 Jan 2009 08:07:10 -0000 1.1 +++ README.txt 28 May 2009 14:52:56 -0000 @@ -21,6 +21,7 @@ REQUIREMENTS - Drupal 6 - Views 2 +- Token AUTHOR AND CREDIT @@ -29,3 +30,6 @@ Jeff Eaton "eaton" (http://drupal.org/us 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 =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/views_attach/views_attach.info,v retrieving revision 1.1.2.1 diff -u -p -r1.1.2.1 views_attach.info --- views_attach.info 20 Jan 2009 21:19:48 -0000 1.1.2.1 +++ views_attach.info 28 May 2009 14:52:56 -0000 @@ -1,6 +1,7 @@ ; $Id: views_attach.info,v 1.1.2.1 2009/01/20 21:19:48 crell Exp $ name = Views attach -description = Provides new Views display types that can be attached to nodes or users. +description = Provides new Views display types that can be attached to nodes or users. core = 6.x dependencies[] = views +dependencies[] = token package = Views Index: views_attach.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/views_attach/views_attach.module,v retrieving revision 1.1.2.8 diff -u -p -r1.1.2.8 views_attach.module --- views_attach.module 27 May 2009 18:28:03 -0000 1.1.2.8 +++ views_attach.module 28 May 2009 14:52:57 -0000 @@ -50,8 +50,7 @@ function views_attach_user($op, &$edit, 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', @@ -81,6 +80,7 @@ function views_attach_nodeapi(&$node, $o $mode = $node->build_mode; if ($mode === NODE_BUILD_NORMAL) { $mode = $teaser ? 'teaser' : 'full'; + $mode = $teaser ? 'edit' : 'full'; } } else { @@ -91,8 +91,7 @@ function views_attach_nodeapi(&$node, $o 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)) { $node->content[$view->name . '_' . $info['display']] = array( '#weight' => module_exists('content') ? content_extra_field_weight($node->type, $view->name . '_' . $info['display']) : 10, @@ -105,6 +104,59 @@ function views_attach_nodeapi(&$node, $o } /** + * Get view arguments array from string that contains tokens + * + * @param $user_input + * The token string or array 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); +} + +/** + * Trim the array values + */ +function trim_value(&$value) { + $value = trim($value); +} + +/** + * 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; +} + +/** * Get a list of views and displays attached to the specified category. * * This function will cache its results into the views cache, so it gets @@ -208,9 +260,33 @@ function views_attach_build_modes() { } else { $modes = array( - 'full' => 'Full', + 'full' => 'Full node', 'teaser' => 'Teaser', ); } + $modes['edit'] = 'Edit page'; return $modes; +} + +/** +* Implementation of hook_form_alter(). +*/ +function views_attach_form_alter(&$form, $form_state, $form_id) { + if($form['#id'] == 'node-form') { + $views = views_attach_get_node_views($form['type']['#value'], 'edit'); + 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); + if (!empty($result)) { + $form[$view->name . '_' . $info['display']] = array( + '#title' => $view->display_handler->get_option('show_title') ? $view->get_title() : '', + '#type' => 'item', + '#weight' => module_exists('content') ? content_extra_field_weight($form['type']['#value'], $view->name . '_' . $info['display']) : 10, + '#value' => $result, + ); + } + } + } } \ No newline at end of file Index: views_attach_plugin_display_node_content.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/views_attach/views_attach_plugin_display_node_content.inc,v retrieving revision 1.1.2.7 diff -u -p -r1.1.2.7 views_attach_plugin_display_node_content.inc --- views_attach_plugin_display_node_content.inc 22 Feb 2009 23:05:36 -0000 1.1.2.7 +++ views_attach_plugin_display_node_content.inc 28 May 2009 14:52:57 -0000 @@ -10,7 +10,7 @@ class views_attach_plugin_display_node_c $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 @@ class views_attach_plugin_display_node_c $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 @@ class views_attach_plugin_display_node_c 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,6 +147,33 @@ class views_attach_plugin_display_node_c 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); + $token_values = views_attach_get_arguments_from_token_string($token_string, $token_type, $node); + $new_args = array(); + // We have to be careful to only replace arguments that have tokens. + foreach ($token_values as $key => $value) { + $new_args[$key] = $value; + } + $this->view->args = $new_args; + } /** * The display block handler returns the structure necessary for a block. Index: views_attach_plugin_display_profile.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/views_attach/views_attach_plugin_display_profile.inc,v retrieving revision 1.1.2.1 diff -u -p -r1.1.2.1 views_attach_plugin_display_profile.inc --- views_attach_plugin_display_profile.inc 13 Feb 2009 17:14:26 -0000 1.1.2.1 +++ views_attach_plugin_display_profile.inc 28 May 2009 14:52:57 -0000 @@ -10,7 +10,7 @@ class views_attach_plugin_display_profil $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 @@ class views_attach_plugin_display_profil '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 @@ class views_attach_plugin_display_profil 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,37 @@ class views_attach_plugin_display_profil } /** + * 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; + } + + if (is_array($args)) { + $user = user_load($args[0]); + } + else { + $user = user_load(); + } + + // Now do the token replacement. + $token_type = views_attach_get_token_type($this->view->base_table); + $token_values = views_attach_get_arguments_from_token_string($token_string, $token_type, $user); + $new_args = array(); + // We have to be careful to only replace arguments that have tokens. + foreach ($token_values as $key => $value) { + $new_args[$key] = $value; + } + + $this->view->args = $new_args; + } + + /** * The display block handler returns the structure necessary for a block. */ function execute() {