Download & Extend

Can I make the links print out in li tags?

Project:Link
Version:6.x-2.8
Component:Code
Category:feature request
Priority:normal
Assigned:Kasper Souren
Status:active

Issue Summary

I have a section on one of my pages where my client can add links and their titles using CCK link and I printed the field out in my template like this:

<?php
print $node -> content ['field_url'] ['#children']
?>

I really want each link to print out in li tags surrounded by a

    tag. I tried putting
      and
    • around my php but that did not work. How do I do this?

      Becky

Comments

#1

Sorry I forgot to add code tags. I meant that I added <ul>tags <li> tags around my php but that didn't work.

#2

I got these results by adding a new variable to the page and then calling it in the theme.

In the template.php, add the following code inside the phptemplate_preprocess_page function:

<?php
    $vars
['theme_links_variable'] = '<ul>';
    foreach (
$vars['node']->field_my_cck_links_variable as $links) {
       
$vars['theme_links_variable'] .= '<li><a title="' . $links['display_title'] . '" href="' . $links['display_url'] . '" >' . $links['display_title'] . '</a></li>';
    }
   
$vars['links_further'] .= '</ul>';
?>

You can then call the variable from your page.tpl.php template by calling the newly created variable name.
eg.

<?php
print $theme_links_variable;
?>

You'll need to change the cck field name to the same name of your field.

#3

Wow, thanks, I will try that out.

Becky

#4

I also would like to change the output of the Link module to a list format. However, when I use the code given above, I get an error message: warning: Invalid argument supplied for foreach() in /home/blah/public_html/sites/all/themes/customzen/template.php on line 208.

Here is the code I've added to my theme's template.php

//Re-write Link module output
    $vars['theme_links_variable'] = '<ul class="anchor_menu">Link <img alt="down arrow" src="/files/images/icons/arrow-down.png" /> to: ';
    foreach ($vars['node']->field_menu_item as $links) {
        $vars['theme_links_variable'] .= '<li><a title="' . $links['display_title'] . '" href="' . $links['display_url'] . '" >' . $links['display_title'] . '</a></li>';
    }
    $vars['links_further'] .= '</ul>';

Where field_menu_item is the name of my Links field in the content type.

Not only do I get that invalid argument error, but the extra text that I've included at the beginning will show up on every page, even if it doesn't have a Link menu, however it doesn't close out the ul. And the original Link menu still shows up in the content of the page.

I've attached a screenshot if that helps.

I'm afraid I've done absolutely no theming at all, so I really don't even know where to start looking. I'm about to download every file on the server and do a Find to try to find the Link code.

AttachmentSize
linkerror.png 24.54 KB

#5

I found an answer of sorts, and wanted to add it here for the next person who is looking for a way to change the output of the Link module.

I used the Content Template module (http://drupal.org/project/contemplate) to see the code and variables that were being passed to the template in the ubiquitous $content section. Contemplate was able to show me the proper code to use, then I was able to tweak it and put it into a node-mycontenttype.tpl.php file.

Here is what I came up with, if anyone wishes to use something like it:
Replaced

  <div class="content">
    <?php print $content; ?>
  </div>
in the template with
  <div class="content">
<!-- custom anchor link code -->
<?php
if ($node->field_menu_item[0]['url'] != NULL) { ?>

<div class="field field-type-link field-field-menu-item">
  <div class="field-items">
<ul class="anchor-menu">Link  to:
    <?php
    count
($node->field_menu_item);
   
$i=0;
    foreach ((array)
$node->field_menu_item as $item) { ?>

      <li class="field-item<?php print $i ?>"><?php print $item['view']; $i++;?></li>
    <?php } ?></ul>
  </div>
</div>
<?php } //endif ?>
<?php print $node->content['body']['#value'] ?>
<!-- end custom anchor link code -->
  </div>

and then of course, some css to style the list:
/**
* Anchor menu styles
*/
ul.anchor-menu { font-weight: bold; }
ul.anchor-menu li { display: inline;
                             padding-left: .5em;
                             padding-right: .5em;
                             border-left: 1px solid black;
                             list-style-type: none;
                             }
ul.anchor-menu li.field-item0 { border: none; }

The count(array) code in there isn't needed unless you want to be able to style the first list item differently. It's not very elegant, but it works, so I'm happy.

Hope this helps someone else in the future.

#6

Version:6.x-2.5» 6.x-2.8
Component:Miscellaneous» Code
Category:support request» feature request
Assigned to:Anonymous» Kasper Souren

In the project I'm working on I need links as li items in several places, I don't like hacking around in templates.
Since it's such a basic thing it seems to make more sense to implement this inside the module. So I'm giving that a try now...