? TODO.txt
? api.txt
? base-link.patch
? book.txt
? doc
? generic-link.patch
? rowclasses_1.patch
? test.php
Index: handlers/views_handler_field.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/handlers/views_handler_field.inc,v
retrieving revision 1.5
diff -u -p -r1.5 views_handler_field.inc
--- handlers/views_handler_field.inc	16 Oct 2008 19:38:52 -0000	1.5
+++ handlers/views_handler_field.inc	28 Jan 2009 02:42:38 -0000
@@ -38,6 +38,16 @@ class views_handler_field extends views_
     }
   }
 
+  /**
+   * Determine if this field can allow advanced rendering.
+   *
+   * Fields can set this to FALSE if they do not wish to allow
+   * token based rewriting or link-making.
+   */
+  function allow_advanced_render() {
+    return TRUE;
+  }
+
   function init(&$view, $options) {
     parent::init($view, $options);
 
@@ -132,6 +142,16 @@ class views_handler_field extends views_
     $options = parent::option_definition();
 
     $options['label'] = array('default' => $this->definition['title'], 'translatable' => TRUE);
+    $options['alter'] = array(
+      'contains' => array(
+        'alter_text' => array('default' => FALSE),
+        'text' => array('default' => '', 'translatable' => TRUE),
+        'make_link' => array('default' => FALSE),
+        'path' => array('default' => '', 'translatable' => TRUE),
+        'prefix' => array('default' => '', 'translatable' => TRUE),
+        'suffix' => array('default' => '', 'translatable' => TRUE),
+      ),
+    );
 
     return $options;
   }
@@ -153,6 +173,109 @@ class views_handler_field extends views_
       '#default_value' => $this->options['exclude'],
       '#description' => t('Check this box to not display this field, but still load it in the view.  Use this option to not show a grouping field in each record, or when doing advanced theming.'),
     );
+
+    if ($this->allow_advanced_render()) {
+      $form['alter']['#tree'] = TRUE;
+      $form['alter']['alter_text'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Rewrite the output of this field'),
+        '#description' => t('If checked, you can alter the output of this field by specifying a string of text with replacement tokens that can use any existing field output.'),
+        '#default_value' => $this->options['alter']['alter_text'],
+      );
+
+      $form['alter']['text'] = array(
+        '#title' => t('Link text'),
+        '#type' => 'textfield',
+        '#default_value' => $this->options['alter']['text'],
+        '#description' => t('The text to display for this link. You may include HTML. You may also enter the view arguments, in the format <em>%1</em>, or database field names, in the format <em>[field_name]</em>. For a list of valid tokens, see the <em>Replacement patterns</em> listed below.'),
+        '#process' => array('views_process_dependency'),
+        '#dependency' => array(
+          'edit-options-alter-alter-text' => array(1)
+        ),
+      );
+
+      $form['alter']['make_link'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Output this field as a link'),
+        '#description' => t('If checked, this field will be made into a link. The destination must be given below.'),
+        '#default_value' => $this->options['alter']['make_link'],
+      );
+      $form['alter']['path'] = array(
+        '#title' => t('Link path'),
+        '#type' => 'textfield',
+        '#default_value' => $this->options['alter']['path'],
+        '#description' => t('The Drupal path or absolute URL for this link. You may enter the view arguments, in the format <em>%1</em>, or field IDs, in the format <em>[field_name]</em>. For a list of valid tokens, see the <em>Replacement patterns</em> listed below.'),
+        '#process' => array('views_process_dependency'),
+        '#dependency' => array(
+          'edit-options-alter-make-link' => array(1)
+        ),
+      );
+      $form['alter']['prefix'] = array(
+        '#title' => t('Prefix text'),
+        '#type' => 'textfield',
+        '#default_value' => $this->options['alter']['prefix'],
+        '#description' => t('Any text to display before this link. You may include HTML.'),
+        '#process' => array('views_process_dependency'),
+        '#dependency' => array(
+          'edit-options-alter-make-link' => array(1)
+        ),
+      );
+      $form['alter']['suffix'] = array(
+        '#title' => t('Suffix text'),
+        '#type' => 'textfield',
+        '#default_value' => $this->options['alter']['suffix'],
+        '#description' => t('Any text to display after this link. You may include HTML.'),
+        '#process' => array('views_process_dependency'),
+        '#dependency' => array(
+          'edit-options-alter-make-link' => array(1)
+        ),
+      );
+
+      // Get a list of the available fields and arguments for token replacement.
+      $options = array();
+      foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
+        $options[t('Fields')]["[$field]"] = $handler->ui_name();
+        // We only use fields up to (and including) this one.
+        if ($field == $this->options['id']) {
+          break;
+        }
+      }
+      $count = 0; // This lets us prepare the key as we want it printed.
+      foreach ($this->view->display_handler->get_handlers('argument') as $arg => $handler) {
+        $options[t('Arguments')]['%' . ++$count] = $handler->ui_name();
+      }
+
+      // Default text.
+      $output = t('<p>You must add some additional fields to this display before using this field. These fields may be marked as <em>Exclude from display</em> if you prefer. Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields.</p>');
+      // We have some options, so make a list.
+      if (!empty($options)) {
+        $output = t('<p>The following substitution patterns are available for this display. Use the pattern shown on the left to display the value indicated on the right. Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields.</p>');
+        foreach (array_keys($options) as $type) {
+          if (!empty($options[$type])) {
+            $items = array();
+            $title = t(ucwords($type));
+            foreach ($options[$type] as $key => $value) {
+              $items[] = $key .' == '. $value;
+            }
+            $output .= theme('item_list', $items, $title);
+          }
+        }
+      }
+      // This construct uses 'hidden' and not markup because process doesn't
+      // run. It also has an extra div because the dependency wants to hide
+      // the parent in situations like this, so we need a second div to
+      // make this work.
+      $form['alter']['help'] = array(
+        '#type' => 'hidden',
+        '#id' => 'views-tokens-help',
+        '#prefix' => '<div><fieldset id="views-tokens-help"><legend>' . t('Replacement patterns') . '</legend>' . $output . '</fieldset></div>',
+        '#process' => array('views_process_dependency'),
+        '#dependency' => array(
+          'edit-options-alter-make-link' => array(1),
+          'edit-options-alter-alter-text' => array(1),
+        ),
+      );
+    }
   }
 
   /**
@@ -185,6 +308,109 @@ class views_handler_field extends views_
   }
 
   /**
+   * Render a field using advanced settings.
+   *
+   * This renders a field normally, then decides if render-as-link and
+   * text-replacement rendering is necessary.
+   */
+  function advanced_render($values) {
+    $this->last_render = $value = $this->render($values);
+
+    $tokens = NULL;
+    if (!empty($this->options['alter']['alter_text']) && !empty($this->options['alter']['text'])) {
+      $tokens = $this->get_render_tokens();
+      $value = $this->render_altered($tokens);
+    }
+
+    if (!empty($this->options['alter']['make_link']) && !empty($this->options['alter']['text'])) {
+      if (!isset($tokens)) {
+       $tokens = $this->get_render_tokens();
+      }
+      $value = $this->render_as_link($value, $tokens);
+    }
+
+    // This happens here so that render_link can get the unaltered value of
+    // this field as a token rather than the altered value.
+    $this->last_render = $value;
+
+    return $this->last_render;
+  }
+
+  /**
+   * Render this field as altered text, from a fieldset set by the user.
+   */
+  function render_altered($tokens) {
+    // Filter this right away as our substitutions are already sanitized.
+    $value = filter_xss_admin($this->options['alter']['text']);
+    $value = strtr($value, $tokens);
+
+    return $value;
+  }
+
+  /**
+   * Render this field as a link, with the info from a fieldset set by
+   * the user.
+   */
+  function render_as_link($text, $tokens) {
+    $path = filter_xss_admin($this->options['alter']['path']);
+    $path = strtr($path, $tokens);
+
+    $value = '';
+
+    if (!empty($this->options['alter']['prefix'])) {
+      $value .= filter_xss_admin($this->options['alter']['prefix']);
+    }
+
+    // @todo do we need options for 'query' and 'fragment' here? Can we
+    // successfully parse those from the given path instead?
+    $value .= l($text, $path, array('html' => TRUE));
+
+    if (!empty($this->options['alter']['suffix'])) {
+      $value .= filter_xss_admin($this->options['alter']['suffix']);
+    }
+
+    return $value;
+  }
+
+  /**
+   * Get the 'render' tokens to use for advanced rendering.
+   *
+   * This runs through all of the fields and arguments that
+   * are available and gets their values. This will then be
+   * used in one giant str_replace().
+   */
+  function get_render_tokens() {
+    $tokens = array();
+    if (!empty($this->view->build_info['substitutions'])) {
+      $tokens = $this->view->build_info['substitutions'];
+    }
+    $count = 0;
+    foreach ($this->view->display_handler->get_handlers('argument') as $arg => $handler) {
+      $token = '%' . ++$count;
+      if (!isset($tokens[$token])) {
+        $tokens[$token] = '';
+      }
+    }
+
+    // Now add replacements for our fields.
+    $options = array();
+    foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
+      if (isset($handler->last_render)) {
+        $tokens["[$field]"] = $handler->last_render;
+      }
+      else {
+        $tokens["[$field]"] = '';
+      }
+      // We only use fields up to (and including) this one.
+      if ($field == $this->options['id']) {
+        break;
+      }
+    }
+
+    return $tokens;
+  }
+
+  /**
    * Call out to the theme() function, which probably just calls render() but
    * allows sites to override output fairly easily.
    */
Index: includes/view.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/includes/view.inc,v
retrieving revision 1.148
diff -u -p -r1.148 view.inc
--- includes/view.inc	27 Jan 2009 21:32:46 -0000	1.148
+++ includes/view.inc	28 Jan 2009 02:42:41 -0000
@@ -504,6 +504,9 @@ class view extends views_db_object {
       $this->build_info['title'] = str_replace(array_keys($substitutions), $substitutions, $title);
     }
 
+    // Store the arguments for later use.
+    $this->build_info['substitutions'] = $substitutions;
+
     return $status;
   }
 
@@ -793,6 +796,15 @@ class view extends views_db_object {
   }
 
   /**
+   * Render a specific field via the field ID and the row #.
+   */
+  function render_field($field, $row) {
+    if (isset($this->field[$field]) && isset($this->result[$row])) {
+      return $this->field[$field]->advanced_render($this->result[$row]);
+    }
+  }
+
+  /**
    * Execute the given display, with the given arguments.
    * To be called externally by whatever mechanism invokes the view,
    * such as a page callback, hook_block, etc.
Index: modules/node.views.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/modules/node.views.inc,v
retrieving revision 1.91
diff -u -p -r1.91 node.views.inc
--- modules/node.views.inc	2 Dec 2008 22:17:41 -0000	1.91
+++ modules/node.views.inc	28 Jan 2009 02:42:41 -0000
@@ -90,6 +90,7 @@ function node_views_data() {
       'group' => t('Node'), // The group it appears in on the UI. Could be left out.
       'handler' => 'views_handler_field_node',
       'click sortable' => TRUE,
+      'allow advanced render' => TRUE,
      ),
     'sort' => array(
       'handler' => 'views_handler_sort',
Index: theme/theme.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/theme/theme.inc,v
retrieving revision 1.68
diff -u -p -r1.68 theme.inc
--- theme/theme.inc	28 Jan 2009 00:43:43 -0000	1.68
+++ theme/theme.inc	28 Jan 2009 02:42:44 -0000
@@ -201,7 +201,7 @@ function template_preprocess_views_view_
  * this: @code { $row->{$field->field_alias} @endcode
  */
 function theme_views_view_field($view, $field, $row) {
-  return $field->render($row);
+  return $field->advanced_render($row);
 }
 
 /**
@@ -209,10 +209,10 @@ function theme_views_view_field($view, $
  *
  * This preprocess function isn't normally run, as a function is used by
  * default, for performance. However, by creating a template, this
- * preprocess should get pickedup.
+ * preprocess should get picked up.
  */
 function template_preprocess_views_view_field(&$vars) {
-  $vars['output'] = $vars['field']->render($vars['row']);
+  $vars['output'] = $vars['field']->advanced_render($vars['row']);
 }
 
 /**
