A silly trip through the innards of views theming: output plain link with no HTML

By Benjamin Melançon
on 12 Mar
0 comments

Key words and phrases

theme div class field-item avoid skip solution views do not group multiple values

Tags

Description

Spoiler: if you want a plain value of a field to be output plain without surrounding <div class="field-item"> tags, make sure to select "Do not group multiple values".

Now for the masochists, let's follow how I found this out the really, really, really hard way!

Amazing what's available to a simple view in Drupal 5.

A views-list-agaric_view_name.tpl.php with the following sole contents:

<?php
print '<pre>';
print_r(get_defined_vars());
print
'</pre>';
?>

Outputs the below. Remember that what you print is already all wrapped in <li> tags.

The easy access to available fields is at the bottom.

Array
(
    [file] => sites/lc.openzuka.com/themes/sky/views-list-doctor_resources.tpl.php
    [variables] => Array
        (
            [view] => stdClass Object
                (
                    [vid] => 12
                    [name] => doctor_resources
                    [description] => The view that shows doctor resource links.
                    [access] => Array
                        (
                        )

                    [page] => 1
                    [page_title] => Doctor's Resource Links
                    [page_header] => 
                    [page_header_format] => 1
                    [page_empty] => 
                    [page_empty_format] => 1
                    [page_footer] => 
                    [page_footer_format] => 1
                    [page_type] => list
                    [use_pager] => 1
                    [nodes_per_page] => 50
                    [url] => view/links
                    [menu] => 0
                    [menu_tab] => 0
                    [menu_tab_weight] => 0
                    [menu_title] => 
                    [menu_tab_default] => 0
                    [menu_tab_default_parent_type] => tab
                    [menu_parent_title] => 
                    [menu_parent_tab_weight] => 0
                    [block] => 0
                    [block_title] => 
                    [block_use_page_header] => 0
                    [block_header] => 
                    [block_header_format] => 1
                    [block_use_page_footer] => 0
                    [block_footer] => 
                    [block_footer_format] => 1
                    [block_use_page_empty] => 0
                    [block_empty] => 
                    [block_empty_format] => 1
                    [block_type] => node
                    [nodes_per_block] => 0
                    [block_more] => 0
                    [breadcrumb_no_home] => 0
                    [changed] => 1204895886
                    [view_args_php] => 
                    [is_cacheable] => 1
                    [sort] => Array
                        (
                            [0] => Array
                                (
                                    [vid] => 12
                                    [position] => 0
                                    [field] => node.created
                                    [sortorder] => DESC
                                    [options] => normal
                                    [tablename] => 
                                    [id] => node.created
                                )

                        )

                    [argument] => Array
                        (
                        )

                    [field] => Array
                        (
                            [0] => Array
                                (
                                    [vid] => 12
                                    [tablename] => node
                                    [field] => title
                                    [label] => 
                                    [handler] => views_handler_field_nodelink
                                    [sortable] => 0
                                    [defaultsort] => 0
                                    [options] => nolink
                                    [position] => 0
                                    [fullname] => node.title
                                    [id] => node.title
                                    [queryname] => node_title
                                )

                            [1] => Array
                                (
                                    [vid] => 12
                                    [tablename] => node_data_field_resource_link
                                    [field] => field_resource_link_url
                                    [label] => 
                                    [handler] => content_views_field_handler_group
                                    [sortable] => 0
                                    [defaultsort] => 
                                    [options] => plain
                                    [position] => 1
                                    [fullname] => node_data_field_resource_link.field_resource_link_url
                                    [id] => node_data_field_resource_link.field_resource_link_url
                                    [queryname] => node_data_field_resource_link_field_resource_link_url
                                )

                            [2] => Array
                                (
                                    [vid] => 12
                                    [tablename] => node_data_field_description
                                    [field] => field_description_value
                                    [label] => 
                                    [handler] => content_views_field_handler_group
                                    [sortable] => 0
                                    [defaultsort] => 0
                                    [options] => default
                                    [position] => 2
                                    [fullname] => node_data_field_description.field_description_value
                                    [id] => node_data_field_description.field_description_value
                                    [queryname] => node_data_field_description_field_description_value
                                )

                        )

                    [filter] => Array
                        (
                            [0] => Array
                                (
                                    [vid] => 12
                                    [tablename] => 
                                    [field] => node.type
                                    [value] => Array
                                        (
                                            [0] => link_audio
                                            [1] => link_video
                                        )

                                    [operator] => OR
                                    [options] => 
                                    [position] => 0
                                    [id] => node.type
                                )

                        )

                    [exposed_filter] => Array
                        (
                        )

                    [build_type] => page
                    [type] => list
                    [pager_limit] => 50
                    [current_page] => 0
                    [offset] => 0
                    [total_rows] => 17
                    [num_rows] => 17
                    [real_url] => view/links
                )

            [view_type] => page
            [node] => stdClass Object
                (
                    [nid] => 705
                    [node_created_created] => 1204309651
                    [node_title] => Special PLWC Podcast: 2007 Breast Cancer Symposium Highlights, with Eric Winer, MD
                    [node_changed] => 1204309651
                    [node_data_field_description_field_description_value] => (8.71 MB/9:31)
                )

            [count] => 0
            [stripe] => odd
            [title] => Special PLWC Podcast: 2007 Breast Cancer Symposium Highlights, with Eric Winer, MD
            [title_label] => 
            [field_resource_link_url] => 
http://www.plwc.org/PLWC/Library/Podcasts/2007_Breast_Cancer_Symposium.m...

            [field_resource_link_url_label] => 
            [field_description_value] => (8.71 MB/9:31)
            [field_description_value_label] => 
            [zebra] => odd
            [id] => 1
            [directory] => sites/lc.openzuka.com/themes/sky
            [is_front] => 
        )

    [view] => stdClass Object
        (
            [vid] => 12
            [name] => doctor_resources
            [description] => The view that shows doctor resource links.
            [access] => Array
                (
                )

            [page] => 1
            [page_title] => Doctor's Resource Links
            [page_header] => 
            [page_header_format] => 1
            [page_empty] => 
            [page_empty_format] => 1
            [page_footer] => 
            [page_footer_format] => 1
            [page_type] => list
            [use_pager] => 1
            [nodes_per_page] => 50
            [url] => view/links
            [menu] => 0
            [menu_tab] => 0
            [menu_tab_weight] => 0
            [menu_title] => 
            [menu_tab_default] => 0
            [menu_tab_default_parent_type] => tab
            [menu_parent_title] => 
            [menu_parent_tab_weight] => 0
            [block] => 0
            [block_title] => 
            [block_use_page_header] => 0
            [block_header] => 
            [block_header_format] => 1
            [block_use_page_footer] => 0
            [block_footer] => 
            [block_footer_format] => 1
            [block_use_page_empty] => 0
            [block_empty] => 
            [block_empty_format] => 1
            [block_type] => node
            [nodes_per_block] => 0
            [block_more] => 0
            [breadcrumb_no_home] => 0
            [changed] => 1204895886
            [view_args_php] => 
            [is_cacheable] => 1
            [sort] => Array
                (
                    [0] => Array
                        (
                            [vid] => 12
                            [position] => 0
                            [field] => node.created
                            [sortorder] => DESC
                            [options] => normal
                            [tablename] => 
                            [id] => node.created
                        )

                )

            [argument] => Array
                (
                )

            [field] => Array
                (
                    [0] => Array
                        (
                            [vid] => 12
                            [tablename] => node
                            [field] => title
                            [label] => 
                            [handler] => views_handler_field_nodelink
                            [sortable] => 0
                            [defaultsort] => 0
                            [options] => nolink
                            [position] => 0
                            [fullname] => node.title
                            [id] => node.title
                            [queryname] => node_title
                        )

                    [1] => Array
                        (
                            [vid] => 12
                            [tablename] => node_data_field_resource_link
                            [field] => field_resource_link_url
                            [label] => 
                            [handler] => content_views_field_handler_group
                            [sortable] => 0
                            [defaultsort] => 
                            [options] => plain
                            [position] => 1
                            [fullname] => node_data_field_resource_link.field_resource_link_url
                            [id] => node_data_field_resource_link.field_resource_link_url
                            [queryname] => node_data_field_resource_link_field_resource_link_url
                        )

                    [2] => Array
                        (
                            [vid] => 12
                            [tablename] => node_data_field_description
                            [field] => field_description_value
                            [label] => 
                            [handler] => content_views_field_handler_group
                            [sortable] => 0
                            [defaultsort] => 0
                            [options] => default
                            [position] => 2
                            [fullname] => node_data_field_description.field_description_value
                            [id] => node_data_field_description.field_description_value
                            [queryname] => node_data_field_description_field_description_value
                        )

                )

            [filter] => Array
                (
                    [0] => Array
                        (
                            [vid] => 12
                            [tablename] => 
                            [field] => node.type
                            [value] => Array
                                (
                                    [0] => link_audio
                                    [1] => link_video
                                )

                            [operator] => OR
                            [options] => 
                            [position] => 0
                            [id] => node.type
                        )

                )

            [exposed_filter] => Array
                (
                )

            [build_type] => page
            [type] => list
            [pager_limit] => 50
            [current_page] => 0
            [offset] => 0
            [total_rows] => 17
            [num_rows] => 17
            [real_url] => view/links
        )

    [view_type] => page
    [node] => stdClass Object
        (
            [nid] => 705
            [node_created_created] => 1204309651
            [node_title] => Special PLWC Podcast: 2007 Breast Cancer Symposium Highlights, with Eric Winer, MD
            [node_changed] => 1204309651
            [node_data_field_description_field_description_value] => (8.71 MB/9:31)
        )

    [count] => 0
    [stripe] => odd
    [title] => Special PLWC Podcast: 2007 Breast Cancer Symposium Highlights, with Eric Winer, MD
    [title_label] => 
    [field_resource_link_url] => 
http://www.plwc.org/PLWC/Library/Podcasts/2007_Breast_Cancer_Symposium.m...

    [field_resource_link_url_label] => 
    [field_description_value] => (8.71 MB/9:31)
    [field_description_value_label] => 
    [zebra] => odd
    [id] => 1
    [directory] => sites/lc.openzuka.com/themes/sky
    [is_front] => 
)

Commit message:
themed link view, ugh ugh ugh ugh it's wrapping the plain text link in divs-- OH THE IDIOCY

<div class="<?php print $stripe; ?>">
<?php
if (user_access('administer nodes')) {
  print
'<small>[' . l('edit', 'node/'. $node->nid .'/edit') . ']</small> ';
}
print
l($title, $field_resource_link_url);
if (
$field_description_value) {
  print
' &mdash; ';
  print
$field_description_value;
}
// testing:
print $variables['field_resource_link_url'];
?>

  • [edit] Special PLWC Podcast: 2007 Breast Cancer Symposium Highlights, with Eric Winer, MD — (8.71 MB/9:31)
    http://www.plwc.org/PLWC/Library/Podcasts/2007_Breast_Cancer_Symposium.mp3
  • An issue filed:

    http://drupal.org/node/233304

    Related, might as well patch all at once:

    http://drupal.org/node/141571
    http://drupal.org/node/209685

    It's not Link's fault:

    quicksketch - March 12, 2008 - 12:38

    CCK is the one adding that wrapped DIV around the result. I think it's put in there when the default code output by views theming wizard calls views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);. Prior to that the field is properly just the URL. I don't think there's anything at the link.module level that can be done.

    <?php
     
    if ($formatter == 'plain') {
    drupal_set_message($item['url']);
    drupal_set_message('<pre>'.print_r($item,TRUE).'</pre>');
        return
    check_plain($item['url']);
      }
    ?>

    Pure and innocent:

                   <div class="messages status">
    <ul>

      <li>http://www.plwc.org/PLWC/Library/Podcasts/2007_Breast_Cancer_Symposium.mp3</li>
      <li><pre>Array
    (
        [url] => http://www.plwc.org/PLWC/Library/Podcasts/2007_Breast_Cancer_Symposium.m...    [title] =>
        [attributes] => N;
    )

    My apologies to Link, this makes it easy to fix on theme layer...

    some drupal_set_message output to see what's happening:

    <?php
    function views_theme_field() {
     
    $args = func_get_args();
     
    $function = array_shift($args);
     
    $field_name = array_shift($args);
     
    $view = array_pop($args);
    drupal_set_message('function: '. $function);
    drupal_set_message('<br />field name: '. $field_name);
    drupal_set_message('<pre>'.print_r($view,TRUE).'</pre>');

      if (!(
    $func = theme_get_function($function . '_' . $view->name . '_' . $field_name))) {
        if (!(
    $func = theme_get_function($function . '_' . $field_name))) {
         
    $func = theme_get_function($function);
        }
      }

      if (
    $func) {
        return
    call_user_func_array($func, $args);
      }
    }
    ?>

    # function: views_handle_field
    #

    field name: node_data_field_resource_link_field_resource_link_url
    #

    stdClass Object
    (
        [vid] => 12
        [name] => doctor_resources
        [description] => The view that shows doctor resource links.
        [access] => Array
            (
            )

        [page] => 1
        [page_title] => Doctor's Resource Links
        [page_header] =>
        [page_header_format] => 1
        [page_empty] =>
        [page_empty_format] => 1
        [page_footer] =>
        [page_footer_format] => 1
        [page_type] => list
        [use_pager] => 1
        [nodes_per_page] => 50
        [url] => view/links
        [menu] => 0
        [menu_tab] => 0
        [menu_tab_weight] => 0
        [menu_title] =>
        [menu_tab_default] => 0
        [menu_tab_default_parent_type] => tab
        [menu_parent_title] =>
        [menu_parent_tab_weight] => 0
        [block] => 0
        [block_title] =>
        [block_use_page_header] => 0
        [block_header] =>
        [block_header_format] => 1
        [block_use_page_footer] => 0
        [block_footer] =>
        [block_footer_format] => 1
        [block_use_page_empty] => 0
        [block_empty] =>
        [block_empty_format] => 1
        [block_type] => node
        [nodes_per_block] => 0
        [block_more] => 0
        [breadcrumb_no_home] => 0
        [changed] => 1205335882
        [view_args_php] =>
        [is_cacheable] => 1
        [sort] => Array
            (
                [0] => Array
                    (
                        [vid] => 12
                        [position] => 0
                        [field] => node.created
                        [sortorder] => DESC
                        [options] => normal
                        [tablename] =>
                        [id] => node.created
                    )

            )

        [argument] => Array
            (
            )

        [field] => Array
            (
                [0] => Array
                    (
                        [vid] => 12
                        [tablename] => node
                        [field] => title
                        [label] =>
                        [handler] => views_handler_field_nodelink
                        [sortable] => 0
                        [defaultsort] => 0
                        [options] => nolink
                        [position] => 0
                        [fullname] => node.title
                        [id] => node.title
                        [queryname] => node_title
                    )

                [1] => Array
                    (
                        [vid] => 12
                        [tablename] => node_data_field_resource_link
                        [field] => field_resource_link_url
                        [label] =>
                        [handler] => content_views_field_handler_group
                        [sortable] => 0
                        [defaultsort] =>
                        [options] => plain
                        [position] => 1
                        [fullname] => node_data_field_resource_link.field_resource_link_url
                        [id] => node_data_field_resource_link.field_resource_link_url
                        [queryname] => node_data_field_resource_link_field_resource_link_url
                    )

                [2] => Array
                    (
                        [vid] => 12
                        [tablename] => node_data_field_description
                        [field] => field_description_value
                        [label] =>
                        [handler] => content_views_field_handler_group
                        [sortable] => 0
                        [defaultsort] => 0
                        [options] => default
                        [position] => 2
                        [fullname] => node_data_field_description.field_description_value
                        [id] => node_data_field_description.field_description_value
                        [queryname] => node_data_field_description_field_description_value
                    )

                [3] => Array
                    (
                        [vid] => 12
                        [tablename] => node
                        [field] => type
                        [label] =>
                        [handler] =>
                        [sortable] => 0
                        [defaultsort] => 0
                        [options] =>
                        [position] => 3
                        [fullname] => node.type
                        [id] => node.type
                        [queryname] => node_type
                    )

            )

        [filter] => Array
            (
                [0] => Array
                    (
                        [vid] => 12
                        [tablename] =>
                        [field] => node.type
                        [value] => Array
                            (
                                [0] => link_audio
                                [1] => link_video
                            )

                        [operator] => OR
                        [options] =>
                        [position] => 0
                        [id] => node.type
                    )

            )

        [exposed_filter] => Array
            (
            )

        [build_type] => page
        [type] => list
        [pager_limit] => 50
        [current_page] => 0
        [offset] => 0
        [total_rows] => 17
        [num_rows] => 17
        [real_url] => view/links
    )

    So way up there you can see it provided two functions to try in the views handler-- the first was very general (views_handle_field) and the second very specific (node_data_field_resource_link_field_resource_link_url)

    Following the general first:

    function theme_views_handle_field($fields, $field, $data) {

    }

    <?php
    /**
    * Themeable function to handle displaying a specific field.
    */
    function theme_views_handle_field($fields, $field, $data) {
     
    $info = $fields[$field['fullname']];
    drupal_set_message($field['handler']);
    ?>

    ...

    Output for a bunch of views:

    views_handler_field_nodelink
    content_views_field_handler_group
    content_views_field_handler_group
    views_handler_field_nodelink
    content_views_field_handler_group
    content_views_field_handler_group
    views_handler_field_nodelink
    content_views_field_handler_group
    content_views_field_handler_group
    views_handler_field_nodelink
    content_views_field_handler_group
    content_views_field_handler_group
    views_handler_field_nodelink
    content_views_field_handler_group
    content_views_field_handler_group
    views_handler_field_nodelink
    content_views_field_handler_group
    content_views_field_handler_group
    views_handler_field_nodelink
    content_views_field_handler_group
    content_views_field_handler_group
    views_handler_field_nodelink
    content_views_field_handler_group
    content_views_field_handler_group
    views_handler_field_nodelink
    content_views_field_handler_group
    content_views_field_handler_group
    views_handler_field_nodelink
    content_views_field_handler_group
    content_views_field_handler_group
    views_handler_field_nodelink
    content_views_field_handler_group
    content_views_field_handler_group
    views_handler_field_nodelink
    content_views_field_handler_group
    content_views_field_handler_group
    views_handler_field_nodelink
    content_views_field_handler_group
    content_views_field_handler_group
    views_handler_field_nodelink
    content_views_field_handler_group
    content_views_field_handler_group
    views_handler_field_nodelink
    content_views_field_handler_group
    content_views_field_handler_group
    views_handler_field_nodelink
    content_views_field_handler_group
    content_views_field_handler_group
    views_handler_field_nodelink
    content_views_field_handler_group
    content_views_field_handler_group
    content_views_field_handler_ungroup
    content_views_field_handler_ungroup
    content_views_field_handler_ungroup
    content_views_field_handler_ungroup
    content_views_field_handler_ungroup
    content_views_field_handler_group
    content_views_field_handler_group
    content_views_field_handler_group
    content_views_field_handler_group
    content_views_field_handler_group
    content_views_field_handler_group
    content_views_field_handler_group
    content_views_field_handler_group
    content_views_field_handler_group
    content_views_field_handler_group
    

    Culprit:

    <?php
    function theme_content_view_multiple_field($items, $field, $data) {
      foreach (
    $items as $item) {
       
    $output .= '<div class="field-item">'. $item .'</div>';
      }
      return
    $output;
    }
    ?>

    And, behold, simply selecting "Do not group multiple values" for that field display in the edit view form solved all problems in the world.

    Note:

    <?php
    function phptemplate_node_data_field_resource_link_field_resource_link_url($fields, $field, $data) {
      return
    check_plain($data->$field['queryname']);
     
    drupal_set_message('<pre>'.print_r($data,TRUE).'</pre>');
    }
    ?>

    Did not get called, and I expected that it would. So I learned very little in this particular trip through the views theming rabbit hole.

     

    Post new comment
    The content of this field is kept private and will not be shown publicly.
    • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <small> <pre> <strike> <sub> <sup> <kbd> <s>
    • Lines and paragraphs break automatically.
    • Web page addresses and e-mail addresses turn into links automatically.
    • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

    More information about formatting options

    CAPTCHA
    This question is for testing whether you are a human visitor and to prevent automated spam submissions.
    Image CAPTCHA
    Copy the characters (respecting upper/lower case) from the image.