Category Archives: PHP

Display Audio File Description in node.tpl.php (Drupal Media module)

The audio field widget that is included in the Media module is great! You add a File field to your content type with the Media Browser widget. This allows you to create nodes and upload audio files and they display with a lovely little audio player without any effort on your part.

Here is what the audio field looks like in the node edit form.

audio-description

Here is what it displays like.

player

This is all wonderful, but I recently had a request to allow for multiple Audio files being attached to a single node. This was a quick setting change, of course, but I realized when you have multiple audio players on screen, you likely want a label or description to be displayed for each.

As you can see above, there is a description field, but it is only displayed if you opt to display the audio file as download link and you use a token.

I wanted it to display above each player. Here’s how I did it.

I created a theme template override for the content type (“concert”). I named the file:

node--concert.tpl.php

…and saved it to my sites/all/themes/mytheme/templates/nodes directory.

Here is my code. Yours will vary as you may be doing special things with other fields in this template as well.

Once you save this file, Flush all caches. You will now see the description field above each audio player.

How to Create a Slogan Randomizer Using PHP

One of my all-time favorite Saturday Night Live skits was “Stuart Smalley’s Daily Affirmations.

I’ve decided to have a little fun with PHP by adding random Stuart Smalley quotes to the gench site I’m building. I only want one user to see these quotes (my client who is also my husband) and it’s really just a silly joke between us. As silly as this example is, this technique can be used for serious applications as well. Many is the client who wants rotating slogans in their branding zone.

Continue reading

Drupal Commerce: Indicating a “Sold Out” Product

For the music label site I’m creating, the client wants to prevent people from ordering items no longer in stock, but he wants to continue displaying them as they represent his discography.

For a high-volume Drupal Commerce site, you’re going to want to write or use a custom module for inventory control (like Commerce Stock), but that seemed like overkill for this project.

I decided to deploy a solution that doesn’t require any additional modules – just a little jQuery and the addition of a status field to my product variation type.

Continue reading

Creating ‘Previous’ & ‘Next’ Buttons For Drupal Commerce Product Nodes

The gench site I’m building has an inventory of a few dozen albums for sale (mostly CDs).

Once I created all the products and styled the product display, I was pretty happy with my online store except that it lacked functionality for flipping through the products. I want the gench store to feel a bit like flipping through the LP bin at a used record store.

I briefly considered creating a paginated (or paged) view, with just one product per page. In some situations, this may have worked fine, but it would have broken my lovely content-aware view blocks which display songs, reviews and artists for the currently displayed album. Maybe there is a way to do this, but I couldn’t figure it out (at least not quickly).

Plus, it seemed like this wouldn’t be that difficult to accomplish with some PHP and jQuery.

This is what I created…

Screen Shot 2013-09-12 at 1.44.31 PM

Here’s how I did it…

Continue reading

How To Override a Contributed Module’s Function

I wanted to change the message of the add-to-cart confirmation popup message on the gench site.

I’m using the Commerce Kickstart distribution of Drupal. Utilizing the Theme Devel module, I can see that a contributed module named commerce_add_to_cart_confirmation is delivering the message text I want to change.

Thanks to my newly minted DrupalizeMe knowledge, I know I can override a contributed module’s function(s) from the theme layer. So I find the function responsible for the message in profiles/commerce_kickstart/modules/contrib/commerce_add_to_cart_confirmation/commerce_add_to_cart_confirmation.rules.inc. I copy & paste the function into my theme’s template.php file. I preface the name of the pasted function with the name of my theme followed by an underscore (genchstyle_). I change the message text, save my new function, clear my cache and load the confirmation message: no change. Why? Continue reading

Using PHP to Control Layout of a Drupal Webform

A webform’s layout can be controlled by PHP.

Say I want to take this webform…

Webform - before

Webform – before: the student’s name is on 3 separate lines

…and put the 3 Student’s name fields (Student’s first name, Student’s middle name & Student’s last name) on the same line together like this…

Webform – after

Webform – after: the student’s name is now on 1 line

Continue reading