drupal

Browser Tabs #0

0

I regularly have way too many tabs open. Rather than bookmark them or log them in delicious, I’m going to start listing them here. It will act like a snapshot of what’s going on in my brain…? I think that’s interesting. Maybe you will too.

  1. http://www.beeratjoes.com/index.php/beer-dinners/spent-grain-beer-bread/
  2. http://www.realbeer.com/discussions/showthread.php?t=11228
  3. http://www.instructables.com/id/Etched-Copper-Board-Valentines-Day-art-wLEDs/#step0
  4. http://www.harkavagrant.com/index.php?id=152
  5. http://www.amazon.com/gp/search/ref=pe_77460_18793460_pe_09/?ie=UTF8&rh=n:1055398,n:!13900811,n:!1063496,n:331401011,n:284507,p_6:ATVPDKIKX0DER,n:13162311,n:13217501,n:13217701&page=1&bbn=331401011
  6. http://www.jagjaguwar.com/home.php
  7. http://chicago2011.drupal.org/tickets-and-registration
  8. http://bamfestpdx.com/schedule.html
  9. http://www.goldmine-elec-products.com/prodinfo.asp?number=C6900
  10. http://www.workbenchdesign.net/walkaround/walkaround.html
  11. http://www.workbenchdesign.net/
  12. http://www.skycraftsurplus.com/toolstoolkits.aspx
  13. http://www.closegrain.com/2010/08/portable-workbench.html
  14. http://www.instructables.com/id/Stop-using-Ferric-Chloride-etchant!–A-better-etc/?ALLSTEPS
  15. http://www.redwallprints.com/Services.php

301 Redirects in Drupal Pages

0

I recently ran into a problem with a Drupal site I designed and maintain. Here’s a quick explanation of the problem, and then the solution.

The front page has a Views Slideshow Block that grabs node data from custom CCK types. These types contain an image to be displayed in the slideshow (but not the node itself) and a checkbox for enabling or disabling the node from appearing in the Slideshow Block. The slideshow images by default link to the nodes from which they were created.

The problem is linking to a page rendered by Views. Since I can’t add a field for the image or checkbox in a view page (not that I know of, let me know if I’m wrong on this) I had to figure out a different way. And I did, but not without further glitches.

I added the image and checkbox fields to my Page content type and created a node with the appropriate image. It appeared in the slideshow and linked to the page. I then tried several methods to redirect the alias to the correct page. First, the Path Redirect Module. Fail. Then, cpanel’s .htaccess redirect configuration tool. Fail. Then manual .htaccess configuration. Fail.

I don’t really know why the .htaccess failed – I have suspicions it has to do simply with Apache not updating, the fact that I have the Global Redirect Module installed, the clean url directives that already exist in the .htaccess file, or some similar url rewriting conflict.

In a pinch, I added this snippet of code to the body of the page I created:

<?php
 header("HTTP/1.1 301 Moved Permanently");
 header("Location: http://www.mysite.com/aliasname");
 exit();
 ?>

This delicious morsel did the trick, but not without a headache first. You’ll have to have access to posting PHP code, and post it as source, even if you select the PHP option.

A Quick Note on Image Visibility in Drupal’s Views_Gallery Module

0

For anyone whose views_gallery module seems to be installed and configured correctly:

Don’t forget to enable anonymous user permissions. This goes for most uploaded content, including attachments.

Inserting a span element into an anchor via Drupal’s $primary_links

0

I previously wrote about how to insert a span into a link, but after testing this method quickly failed when coupled with the Views module. When a Page view is created and assigned to a node url, everything is fine and dandy. The issues arise when you want a link to use an alias. Luckily, I’ve devised a really simple work around:

use the same code to generate your menu:

&lt;?php if (!empty($primary_links)): ?&gt;
&lt;?php print theme('links', $primary_links, array('id' =&gt; 'nav')); ?&gt;
&lt;?php endif; ?&gt;

this calls the links() function inside of includes/theme.inc, which triggers the l() function in the includes/common.inc to write the primary links menu. We can’t override the l() entirely because other code uses it, so we’ll copy the code from theme.inc and override the l() function with a site template specific one. Here’s my code:

theme.inc

function mytheme_links($links, $attributes = array('class' =&gt; 'links')) {
 global $language;
 $output = '';
 $options = array(
 'class' =&gt; '',
 'html' =&gt; FALSE,
 );

 if (count($links) &gt; 0) {
 $output = '&lt;ul' . drupal_attributes($attributes) .'&gt;';

 $num_links = count($links);
 $i = 1;

 foreach ($links as $key =&gt; $link) {
 $class = $key;

 $links['attributes']['title'] = $link['title'];
 // Add first, last and active classes to the list of links to help out themers.
 if ($i == 1) {
 $class .= ' first';
 }
 if ($i == $num_links) {
 $class .= ' signup last';
 $links['attributes']['class'] .= $class;
 }
 if (isset($link['href']) &amp;&amp; ($link['href'] == $_GET['q'] || ($link['href'] == '&lt;front&gt;' &amp;&amp; drupal_is_front_page()))
 &amp;&amp; (empty($link['language']) || $link['language']-&gt;language == $language-&gt;language)) {
 $class .= ' active';
 }
 $output .= '&lt;li' . drupal_attributes(array('class' =&gt; $class)) .'&gt;';

 if (isset($link['href'])) {
 // Pass in $link as $options, they share the same keys.

//here's my call to the overridden function
 $output .= mytheme_l($link['title'], $link['href'], $links);
 }
 else if (!empty($link['title'])) {
 // Some links are actually not links, but we wrap these in &lt;span&gt; for adding title and class attributes
 if (empty($link['html'])) {
 $link['title'] = check_plain($link['title']);
 }
 $span_attributes = '';
 if (isset($link['attributes'])) {
 $span_attributes = drupal_attributes($link['attributes']);
 }
 $output .= '&lt;span'. $span_attributes .'&gt;'. $link['title'] .'&lt;/span&gt;';
 }

 $i++;
 $output .= "&lt;/li&gt;\n";
 }

 $output .= '&lt;/ul&gt;';
 }

 return $output;
}

and now the l() function in common.inc gets changed to mytheme_l():

function mytheme_l($text, $path, $options = array()) {
 global $language;

 // Merge in defaults.
 $options += array(
 'attributes' =&gt; array(),
 'html' =&gt; FALSE,
 );

 // Append active class.
 if (($path == $_GET['q'] || ($path == '&lt;front&gt;' &amp;&amp; drupal_is_front_page())) &amp;&amp;
 (empty($options['language']) || $options['language']-&gt;language == $language-&gt;language)) {
 if (isset($options['attributes']['class'])) {
 $options['attributes']['class'] .= ' active';
 }
 else {
 $options['attributes']['class'] = 'active';
 }
 }

 // Remove all HTML and PHP tags from a tooltip. For best performance, we act only
 // if a quick strpos() pre-check gave a suspicion (because strip_tags() is expensive).
 if (isset($options['attributes']['title']) &amp;&amp; strpos($options['attributes']['title'], '&lt;') !== FALSE) {
 $options['attributes']['title'] = strip_tags($options['attributes']['title']);
 }
 // Inject a span inside the anchor tag for the purposes of this theme - NOTE - May have to remove check_plain() around
 // the $text variable
 return '&lt;a href="'. check_url(url($path, $options)) .'"'. drupal_attributes($options['attributes']) .'&gt;&lt;span&gt;'. ($options['html'] ? $text : check_plain($text)) .'&lt;/span&gt;&lt;/a&gt;';
}

You can see that all I did was include the span element inside the anchor tag using simple php string concatenation. Until I can find a better solution, this does the trick. Don’t ask me about benchmarking, though…

Returning an Alias from primary_links’s links['href'] – or – How I became a Drupal Wizard

0

I’m building a custom template in Drupal for the farm, and needed a way to spit out a custom implementation of primary_links navigation unordered list. I needed something like this:

<ul id="nav">
  <li class="first"><a href="link-goes-here" title="wow, how descriptive"><span>link 1 copy</span></a></li>
  <li><a href="link-goes-here2" title="round 2, FIGHT!"><span>link 2 copy</span></a></li>
</ul>

The spans inside the anchor tag are valid xhtml strict, and necessary for the nested/floating backgrounds. The l() function in drupal does a fine job of spitting out valid code, but I also haven’t found anything to delimit the angle brackets in the span element.
Here’s how I solved the problem.

First, I added a preprocess function in template.php and copied this chunk of code from root/includes/theme.inc:

function theme_links($links, $attributes = array('class' => 'links')) {
 global $language;
 $output = '';

 if (count($links) > 0) {
 $output = '<ul'. drupal_attributes($attributes) .'>';

 $num_links = count($links);
 $i = 1;

 foreach ($links as $key => $link) {
 $class = $key;

 // Add first, last and active classes to the list of links to help out themers.
 if ($i == 1) {
 $class .= ' first';
 }
 if ($i == $num_links) {
 $class .= ' last';
 }
 if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))
 && (empty($link['language']) || $link['language']->language == $language->language)) {
 $class .= ' active';
 }
 $output .= '<li'. drupal_attributes(array('class' => $class)) .'>';

 if (isset($link['href'])) {
 // Pass in $link as $options, they share the same keys.
 $output .= l($link['title'], $link['href'], $link);
 }
 else if (!empty($link['title'])) {
 // Some links are actually not links, but we wrap these in <span> for adding title and class attributes
 if (empty($link['html'])) {
 $link['title'] = check_plain($link['title']);
 }
 $span_attributes = '';
 if (isset($link['attributes'])) {
 $span_attributes = drupal_attributes($link['attributes']);
 }
 $output .= '<span'. $span_attributes .'>'. $link['title'] .'</span>';
 }

 $i++;
 $output .= "</li>\n";
 }

 $output .= '</ul>';
 }

 return $output;
}

I then modified the code as follows:

function stoneyacresfarm_links($links, $attributes = array('class' => 'links')) {
 global $language;
 $output = '';

 if (count($links) > 0) {
 $output = '<ul'. drupal_attributes($attributes) .'>';

 $num_links = count($links);
 $i = 1;

 foreach ($links as $key => $link) {
 $class = $key;

 // Add first, last and active classes to the list of links to help out themers.
 if ($i == 1) {
 $class .= ' first';
 }
 if ($i == $num_links) {
 $class .= ' last';
 }
 if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))
 && (empty($link['language']) || $link['language']->language == $language->language)) {
 $class .= ' active';
 }
 $output .= '<li'. drupal_attributes(array('class' => $class)) .'>';

 if (isset($link['href'])) {
 // Pass in $link as $options, they share the same keys.
 // here are the modifications
 $temphref = drupal_lookup_path('alias', $link['href'], '');
 $output .= '<a href="' . $temphref . '" title="' . $link['title'] . '"> <span>' . $link['title'] . '</span></a>';
 }
 else if (!empty($link['title'])) {
 // Some links are actually not links, but we wrap these in <span> for adding title and class attributes
 if (empty($link['html'])) {
 $link['title'] = check_plain($link['title']);
 }
 $span_attributes = '';
 if (isset($link['attributes'])) {
 $span_attributes = drupal_attributes($link['attributes']);
 }
 $output .= '<span'. $span_attributes .'>'. $link['title'] .'</span>';
 }

 $i++;
 $output .= "</li>\n";
 }

 $output .= '</ul>';
 }

 return $output;
}

Lines 29 & 30 are the brilliant bits. I scoured api.drupal.org till I found theĀ  drupal_lookup_path function. Follow the link for the arguments that can be passed in. I specified that I wanted an alias, gave it the path ( links['href'] ), and passed in a blank character for language.

This is all rendered out in my page-front.tpl.php and page.tpl.php files in the standard fashion:

<?php if (!empty($primary_links)): ?>
  <?php print theme('links', $primary_links, array('id' => 'nav')); ?>
<?php endif; ?>

I hope this helps!

Go to Top