David Slack - Web developer

Helpful Code

I'll add some code snippets here that could help with a drupal website.

PHP

Add a class to each page

NB: This is for your custom theme, DO NOT add to one of the Drupal themes eg Garland. If you want to add a class to each page add this to the <body> tag of page.tpl.php:

{syntaxhighlighter brush: php;fontsize: 100; first-line: 1; }<?php // Add a class depending on the page if (module_exists('path')) { $alias = drupal_get_path_alias($_GET['q']); $class = explode('/', $alias); print 'class="page page-'.$class[0].'" '; } ?>{/syntaxhighlighter}

Add a class to Teaser lists

In the node.tpl.php file in your theme change

{syntaxhighlighter brush: php;fontsize: 100; first-line: 1; }<div id="node-<?php print $node->nid; ?>" class="node<?php if ($sticky) { print ' sticky'; } ?><?php if (!$status) { print ' node-unpublished'; } ?>">{/syntaxhighlighter}

to

{syntaxhighlighter brush: php;fontsize: 100; first-line: 1; }<div id="node-<?php print $node->nid; ?>" class="<?php print ($teaser ? 'node-teaser' : 'node'); ?><?php if ($sticky&&$page == 0) { print " sticky"; } ?><?php if (!$status) { print " node-unpublished"; } ?> clear-block <?php if ($page == 0) { ?>node-in-list<?php }; ?>">{/syntaxhighlighter}

Show Vars

NB: This is for your custom theme, DO NOT add to one of the Drupal themes eg Garland. Show all the vars in the main array, but only for the user 1 (super user). Just drop the following into your node.tpl.php

{syntaxhighlighter brush: php;fontsize: 100; first-line: 1; }<?php global $user; if($user->uid == 1) { print '<pre>'; print_r(get_defined_vars()); print '</pre>'; } ?>{/syntaxhighlighter}

Create teaser

NB: This is for your custom theme, DO NOT add to one of the Drupal themes eg Garland. If you want to shorten a string and add an elipse to the end use this php function in your templeate.tpl.php and call it on anything

{syntaxhighlighter brush: php;fontsize: 100; first-line: 1; }function myTruncate($string, $limit, $break=".", $pad="...") { if(strlen($string) <= $limit) return $string; if(false !== ($breakpoint = strpos($string, $break, $limit))) { if($breakpoint < strlen($string) - 1) { $string = substr($string, 0, $breakpoint) . $pad; } } return $string; }{/syntaxhighlighter}

Thanks to CCS Tricks for this one

Tags

Latest content

There are lots of developers out there with varying degrees of experience and expertise, some mid level, some junior and some senior. Some...
First thoughts are Symfony is ace!Only been using Symfony for 1 project but the amount of coding it has saved has been worth the time spent...
The Lost and Found website was built by myself (David Slack) and designed by Arm & Eye.The site was created to promote a bar and...

Social networks

Contact me here or catch me on one of
the social networks below


What I'm listening to

Off-site News

My blog

Wednesday, 2 August, 2017 - 06:56
There are lots of developers out there with varying degrees of experience and expertise, some mid level, some junior and some senior. Some developers...
Tuesday, 5 March, 2013 - 09:49
First thoughts are Symfony is ace!Only been using Symfony for 1 project but the amount of coding it has saved has been worth the time spent learning...
Monday, 3 December, 2012 - 10:07
As a web developer and web designer I need to take into account everything on a site from SEO, marketing, usability across devices, the look and feel...

Try this on your mobile

Use your QR reader to read this

You should be able to use this right from the screen

Latest tweets

Calendar

M T W T F S S
 
 
1
 
2
 
3
 
4
 
5
 
6
 
7
 
8
 
9
 
10
 
11
 
12
 
13
 
14
 
15
 
16
 
17
 
18
 
19
 
20
 
21
 
22
 
23
 
24
 
25
 
26
 
27
 
28
 
29
 
30
 
31
 
 
 

You are here

Back to top

Copyright David Slack - Web developer