Url Aliases for Date Ranges OR creating your own custom tokens

Events happening in the community are now at Drupal community events on www.drupal.org.
kurtronaldmueller@gmail.com's picture

Hey guys,
Sorry for inundating your inboxes - I've searched google and the drupal site was unable to come up with an answer.

I've downloaded the Token & PathAuto module and am currently attempting to customize the path pattern for a content type. This content type has a date range that I'd like to use when creating the path pattern... a single date has replacement pattern options that allows the user me to get the Year, Month, and Day so teh path pattern looks like something like "/2012/05/04/content-name".

However, no such options exist for date ranges - I'm contemplating attempting to write a little custom code to create a token that gets the first date in the date range and return the year, another token that gets the month, etc. If you any of you has a simple solution, that you could let me in on, that would be awesome.

Thanks again and again, my apologies for sending a lot of emails.

Kurt

Comments

How I solved this problem (sorta)

kurtronaldmueller@gmail.com's picture

Hey guys,
So I solved my problem - sort of. IMO, it's messy and there's probably a better way of doing it, but it works. Hopefully some of you codemonkeys can point out any glaring flaws/security issues contained in this post, including whether or not to post snippets of php code. Listed below are the steps I took in solving this problem:

  1. I initially attempted to use the 'entity tokens' module which gave me the option to retrieve the first date in a date range. Unfortunately, attempting to use entity tokens in my replacement paths killed my site. I looked at the issue queue for the module and saw that other users were experiencing the same problem.
  2. After doing some digging (i.e. googling around for an hour), I ran into the 'custom tokens' module which, obviously, allows you to create custom tokens. I even found an example of a custom token, located here: http://drupal.org/node/1416570.
  3. Using NaviCat for Mac, I opened up my site's SQLite DB, '.ht.sqlite', and navigated to the the 'field_data' that contained the performance start and end dates.
  4. Each record has, I believe, a unique entity_id, and has two date fields, the start date and end date
  5. I then went to admin/structure/token-custom and added a custom token and inputted the following code: (I would appreciate informing me if posting this php code is a security risk).
  6. <?php
     
    global $performance_date_year;
     
    $entity_id = arg(1);
     
    $yearSQL = "SELECT field_performance_date_value FROM field_data_field_performance_date WHERE entity_id=:entity_id";
     
    $yearResult = db_query($yearSQL, array(':entity_id' => $entity_id));

      foreach(
    $yearResult as $yearRow) {
       
    $performance_date_year = $yearRow->field_performance_date_value;
      }

      return
    $performance_date_year;
    ?>
  7. After inserting this token in the replacement path for my node type, I updated the url aliases and then went to the node view page. This is the result I got: http://i.imgur.com/XT5Dzh.jpg
  8. As you can see, the day in the url is one greater than the day in the node. This is the same for all the nodes of this content type. I don't know why this is my result. Additionally, I honestly don't know how to interpret the time value in the url.
  9. From there, I altered the result to return the year by using php's 'substr' function:
  10. <?php
    $performance_date_year
    = substr($performance_date_year, 0, 4);
    ?>
  11. For the day, I used php's 'substr' function to get the month and then used php's 'intval' and 'strval' function to first convert the substring to an integer, subtract it by one, and then convert it back to a string and returned it.
  12. <?php
      $performance_date_day
    = substr($performance_date_day, 8, 2);
     
    $performance_date_day = intval($performance_date_day)-1;
     
    $performance_date_day = strval($performance_date_day);
    ?>

Anyway, my apologies for the long post. I created this post for several reasons - to help out other Drupal site builders who are experiencing this problem and to get feedback from the coders and site builders of San Diego's Drupal community.

Thanks guys,
Kurt

Timezones

christophweber's picture

For your day offset issue I'd look into UTF vs. PST time zones. The time value in the URL seems to be 3:00 am of the next day, i.e. 7h offset from your PDT event.

Can't really comment on the rest.

--
Christoph Weber

San Diego DUG

Group categories

Event Classifications

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: