<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
 <title>Drupal High</title>
 <link>http://drupalhigh.onsugar.com</link>
 <description>Simple musings of a Drupal developer.</description>
 <language>en</language>
 <atom:link href="http://drupalhigh.onsugar.com/tag/drupal5/rss" rel="self" type="application/rss+xml" />
<item>
 <title>update_sql is not my friend</title>
 <link>http://drupalhigh.onsugar.com/2672756</link>
 <description>&lt;a href=&quot;http://drupalhigh.onsugar.com/2672756&quot;&gt;&lt;/a&gt;&lt;p&gt;Oh &lt;a href=&quot;http://api.drupal.org/api/function/update_sql/5&quot;&gt;update_sql()&lt;/a&gt;, second most unloved of the drupal database functions (&lt;a href=&quot;http://drupal.org/node/299176&quot;&gt;db_rewrite_sql()&lt;/a&gt; is worse)! If you&#039;ve never used it, it is basically a wrapper for db_query() that you can use in hook_update_N() hooks in a module .install file. You may never need to write one of these hooks unless you maintain a module; or like me you prefer to make database changes via code when I&#039;m pushing some new features out to dev, stage, and live servers.&lt;/p&gt;
&lt;p&gt;The Drupal 6 and 7 versions of the documentation for this function do mention that &quot;%-substitution parameters are not supported.&quot; Definitely also true for the Drupal 5 version I&#039;m still working with. So you can&#039;t do variable substitution like you do with db_query() and most of the time thats not a big deal.&lt;/p&gt;
&lt;p&gt;There is one &lt;b&gt;very&lt;/b&gt; important time though.&lt;/p&gt;
&lt;p&gt;When using db_query(), you call it with a syntax like db_query($sql, $variable1, $variable2); Now, you know about database prefixes, and always sing curly braces around your table names in FROMs and JOINs, and that lets Drupal work on multisite installs very well, or talk to more than one DB at a time. So the $sql part of that function call &lt;b&gt;always&lt;/b&gt; gets passed through a strtr(), or in English, PHP&#039;s string translate function that converts every instace of &#039;{&#039; to the right prefix (generally just deletes it). You have no choice about this, and no way to prevent it from getting every single { and }!&lt;/p&gt;
&lt;p&gt;So far so good, because you only use curly braces around the table names, and that works out just fine, right?&lt;/p&gt;
&lt;p&gt;Now enter update_sql(). This function doesn&#039;t support variable substitution, so your function calls look are just update_sql($sql).&lt;/p&gt;
&lt;p&gt;Like update_sql(&quot;UPDATE {node} SET title = &#039;something else&#039; WHERE nid = 10&quot;); - that is, everything has to be passed as part of a single SQL statement (the $sql part above). The SQL statement that always gets run through strtr() looking for &#039;{&#039; and &#039;}&#039;. Which, if you are trying to update a serialized array, or a block&#039;s visibility code with PHP, or just want to use a { in a node title, it will be deleted. Every time, without exception.&lt;/p&gt;
&lt;p&gt;So if you&#039;re using update_sql() and any part of the data contains a &#039;{&#039; or &#039;}&#039; that you need to keep intact - rewrite it as a db_query() with variable substitution. The strtr() will operate on the $sql part, and the $values get substituted in later and stay intact.&lt;/p&gt;</description>
 <comments>http://drupalhigh.onsugar.com/2672756#comment</comments>
 <category domain="http://www.teamsugar.com/category/drupal">drupal</category>
 <category domain="http://www.teamsugar.com/tag/SQL">SQL</category>
 <category domain="http://www.teamsugar.com/tag/drupal5">drupal5</category>
 <category domain="http://www.teamsugar.com/tag/strtr()">strtr()</category>
 <category domain="http://www.teamsugar.com/tag/curly braces">curly braces</category>
 <category domain="http://www.teamsugar.com/tag/escaping">escaping</category>
 <pubDate>Wed, 07 Jan 2009 22:51:53 -0800</pubDate>
 <dc:creator>krs</dc:creator>
 <guid>http://drupalhigh.onsugar.com/2672756</guid>
</item>
<item>
 <title>How to hook_views_tables_alter()</title>
 <link>http://drupalhigh.onsugar.com/2669697</link>
 <description>&lt;a href=&quot;http://drupalhigh.onsugar.com/2669697&quot;&gt;&lt;/a&gt;&lt;p&gt;First of all, this only applies to Views1 &amp;amp; Drupal 5 - there&#039;s probably a similar function in views2 however.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;The setup&lt;/b&gt;: A timestamp ($node-&amp;gt;created) was being selected in a View. The only options for formatting it are as Short, Long, Medium, Time Ago, or Custom. With custom you can enter any string that can be interpreted by php&#039;s &lt;a href=&quot;http://drupalhigh.onsugar.com/php.net/date&quot;&gt;date()&lt;/a&gt; function, and have that format apply.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;The problem&lt;/b&gt;: The site that used a different format as its standard for displaying date and time, let&#039;s say &quot;D F j, Y \a\t g:i A&quot;. We had created a function in template.php called site_format_date() that accepted a timestamp, and returned the date nicely formatted that way. All for reuseability! Now all the dates in the code (and template files, and theme function overrides) we write can call that function. And when Mrs. Editor decides that she really would like it to be something completely different, we can change it in one place and be done!&lt;/p&gt;
&lt;p&gt;&lt;b&gt;The table problem&lt;/b&gt;: What a pain to have to go to each View that uses a time or date, and select the &quot;custom&quot; format, and enter into the textbox the format we want to use. Now when someone wants to change the site-wide format, we&#039;ll have to go back to each view and update all the format fields! Argh!&lt;/p&gt;
&lt;p&gt;So what we&#039;re trying to do, is somehow add our own custom format as one of the options for displaying a timestamp, in addition to the basic ones Drupal and Views provides us. Unfortunately, those choices are created by a call to &lt;a title=&quot;Common Handlers for Views fields&quot; href=&quot;http://drupal.org/node/99565#common-handlers&quot;&gt;views_handler_field_dates()&lt;/a&gt;, which just returns a fixed array of those 5 choices, and there&#039;s no way to get your own in there.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://drupal.org/node/142383&quot;&gt;&lt;b&gt;hook_views_tables_alter()&lt;/b&gt;&lt;/a&gt; to the rescue! This hook lets you modify how the Views module understands the tables in the Drupal database. Most of the core tables are added by .inc files in the views module, and many contrib modules have their own views.inc file. Using this hook you have the opportunity to change anything you like about how views works with modules, and do it safely without hacking any modules.&lt;/p&gt;
&lt;p&gt;The fields I wanted to affect (the created and changed timestamp) are &lt;b&gt;node&lt;/b&gt; fields. You can see them defined in path/to/views/modules/&lt;a href=&quot;http://drupal.org/cvs?file=/modules/views/modules/views_node.inc&quot;&gt;views_node.inc&lt;/a&gt;. I&#039;m trying affect how the &lt;b&gt;fields&lt;/b&gt; are &lt;b&gt;handled&lt;/b&gt; (as opposed to filters, arguments, sorts, or anything else). By checking the .inc file I can see the keys I want to affect are simply called &#039;&lt;b&gt;created&lt;/b&gt;&#039; and &#039;&lt;b&gt;changed&lt;/b&gt;&#039;. Putting that all together, here is my complete hook function :&lt;/p&gt;
&lt;p style=&quot;background-color: #99ccff;&quot;&gt;function hook_views_tables_alter(&amp;amp;$tabledata) {&lt;br /&gt; $tabledata[&#039;node&#039;][&#039;fields&#039;][&#039;created&#039;][&#039;handler&#039;][&#039;my_custom_format_date&#039;] = t(&#039;As My Custom Date&#039;); &lt;br /&gt; $tabledata[&#039;node&#039;][&#039;fields&#039;][&#039;changed&#039;][&#039;handler&#039;][&#039;my_custom_format_date&#039;] = t(&#039;As My Custom Date&#039;); &lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;Now when I can choose &quot;As My Custom Date&quot; to format the timestamps! Views will automatically call my_custom_format_date() for me, and I just pass the data right along to our sitewide date formatter.&lt;/p&gt;
&lt;p style=&quot;background-color: #99ccff;&quot;&gt;function my_custom_format_date($fieldinfo, $fielddata, $value, $data) {&lt;br /&gt; return $value ? site_format_date($value) : theme(&#039;views_nodate&#039;);&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;If figuring out the syntax of the array to modify was a little confusing (views syntax? confusing? never!) try this little snippet first:&lt;/p&gt;
&lt;p style=&quot;background-color: #99ccff;&quot;&gt;function hook_views_tables_alter(&amp;amp;$tabledata) {&lt;br /&gt; drupal_set_message(print_r($tabledata, TRUE));&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;and you&#039;ll get a very long printout of how every table interacts with the Views module. With some searching and little luck, you should be able to find the section you want to modify. Lots of great &lt;a href=&quot;http://drupal.org/node/109604&quot;&gt;views1 and views2 documentation&lt;/a&gt; is available, but it gets a little hairy! Unfortunately there&#039;s not a lot of documentation that I could find on this very powerful hook! I discovered it while poking through the views.module code and found 1 Drupal issue at &lt;a href=&quot;http://drupal.org/node/142383&quot; title=&quot;http://drupal.org/node/142383&quot;&gt;http://drupal.org/node/142383&lt;/a&gt;. (This was the issue that actually enabled the hook to work properly as of about July 2007).&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Remind me to add some pictures to this post!&lt;/p&gt;</description>
 <comments>http://drupalhigh.onsugar.com/2669697#comment</comments>
 <category domain="http://www.teamsugar.com/tag/date">date</category>
 <category domain="http://www.teamsugar.com/category/drupal">drupal</category>
 <category domain="http://www.teamsugar.com/tag/tables">tables</category>
 <category domain="http://www.teamsugar.com/tag/drupal5">drupal5</category>
 <category domain="http://www.teamsugar.com/tag/views1">views1</category>
 <category domain="http://www.teamsugar.com/tag/timestamp">timestamp</category>
 <category domain="http://www.teamsugar.com/tag/format_date">format_date</category>
 <category domain="http://www.teamsugar.com/tag/views module hooks">views module hooks</category>
 <pubDate>Wed, 07 Jan 2009 00:50:21 -0800</pubDate>
 <dc:creator>krs</dc:creator>
 <guid>http://drupalhigh.onsugar.com/2669697</guid>
</item>
<item>
 <title>Imagecache and You (err, your picture)</title>
 <link>http://drupalhigh.onsugar.com/2669631</link>
 <description>&lt;a href=&quot;http://drupalhigh.onsugar.com/2669631&quot;&gt;&lt;/a&gt;&lt;p&gt;When you using imagecache on the $user-&amp;gt;picture, many people have noticed 1 small problem. If someone uploads a new picture, everything appears to work fine, but the user photo stays the same!&lt;/p&gt;
&lt;p&gt;What&#039;s going on? When a user uploads a picture, it gets renamed to a specific filename - doesn&#039;t matter the name of the file you uploaded. Imagecache then makes a copy of that file to display. (Technically this doesn&#039;t happen until the first time the image is viewed with a particular preset). When the new user image is uploaded, it gets the very same name as the previous one, again regardless of the actual filename that was uploaded. Now imagecache will think it already has a cached version of that image, because it just checks the filename and sees that it exists!&lt;/p&gt;
&lt;p&gt;So I was all about to post on how to fix that, basically by linking to &lt;a title=&quot;Lullabots!&quot; href=&quot;http://www.lullabot.com/articles/imagecache_example_user_profile_pictures&quot;&gt;Nate Haug&#039;s excellent post&lt;/a&gt; on how to do just that - but the Imagecache module maintainers have just made &lt;a href=&quot;http://drupal.org/node/354862&quot;&gt;a new release&lt;/a&gt; (for Drupal 5) that includes &lt;a href=&quot;http://drupal.org/node/279861&quot;&gt;a fix for the issue&lt;/a&gt;! So the new solution is - download the latest version of the Imagecache module!&lt;/p&gt;</description>
 <comments>http://drupalhigh.onsugar.com/2669631#comment</comments>
 <category domain="http://www.teamsugar.com/category/drupal">drupal</category>
 <category domain="http://www.teamsugar.com/tag/drupal5">drupal5</category>
 <category domain="http://www.teamsugar.com/tag/imagecache">imagecache</category>
 <category domain="http://www.teamsugar.com/tag/user picture">user picture</category>
 <category domain="http://www.teamsugar.com/tag/flush">flush</category>
 <pubDate>Tue, 06 Jan 2009 23:21:42 -0800</pubDate>
 <dc:creator>krs</dc:creator>
 <guid>http://drupalhigh.onsugar.com/2669631</guid>
</item>
<item>
 <title>Ampersands and CCK</title>
 <link>http://drupalhigh.onsugar.com/2669617</link>
 <description>&lt;a href=&quot;http://drupalhigh.onsugar.com/2669617&quot;&gt;&lt;/a&gt;&lt;p&gt;When using a CCK text field that is set to display as a select list, using the &quot;Allowed Values&quot; textarea to specify each of the choices works 99% of the time. But when trying to use an option that included an ampersand, like &quot;Drupal &amp;amp; Drupal&quot;, it keeps getting escaped to &amp;amp;amp; -  stop it CCK!&lt;/p&gt;
&lt;p&gt;What&#039;s going on? Each line of the textarea is eventually passed through &lt;a title=&quot;api.drupal.org : filter_xss()&quot; href=&quot;http://api.drupal.org/api/function/filter_xss/5&quot;&gt;filter_xss()&lt;/a&gt;, which tries it&#039;s best to remove naughty characters like &amp;amp;amp;, becauseof its &lt;a title=&quot;List of XML and HTML character entity references&quot; href=&quot;http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references&quot;&gt;special meaning in HTML&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;How to avoid it? Use the section &quot;PHP Code&quot; and write your values out as an array. &lt;b&gt;You must specify a key and value pair for each option&lt;/b&gt;, CCK will not automatically handle just having the value. Don&#039;t forget to return the array. PHP open and closing tags arent needed. CCK trusts the values found in this block, and pass them through any kind of text filtering, and your ampsersands will sail through undamaged.&lt;/p&gt;
&lt;p&gt;So&lt;/p&gt;
&lt;p style=&quot;background-color: #99ccff;&quot;&gt;Drupal &amp;amp; Drupal&lt;br /&gt; Hans &amp;amp; Franz&lt;br /&gt; Martin &amp;amp; Lewis&lt;/p&gt;
&lt;p&gt;become&lt;/p&gt;
&lt;p style=&quot;background-color: #99ccff;&quot;&gt;return array(&lt;br /&gt;&#039;Drupal &amp;amp; Drupal&#039; =&amp;gt; &#039;Drupal &amp;amp; Drupal&#039;,&lt;br /&gt;&#039;Hans &amp;amp; Franz&#039; =&amp;gt; &#039;Hans &amp;amp; Franz&#039;,&lt;br /&gt;&#039;Martin &amp;amp; Lewis&#039; =&amp;gt; &#039;Martin &amp;amp; Lewis&#039;,&lt;br /&gt;);&lt;/p&gt;</description>
 <comments>http://drupalhigh.onsugar.com/2669617#comment</comments>
 <category domain="http://www.teamsugar.com/category/drupal">drupal</category>
 <category domain="http://www.teamsugar.com/tag/drupal5">drupal5</category>
 <category domain="http://www.teamsugar.com/tag/filtering">filtering</category>
 <category domain="http://www.teamsugar.com/tag/xss">xss</category>
 <pubDate>Tue, 06 Jan 2009 22:58:15 -0800</pubDate>
 <dc:creator>krs</dc:creator>
 <guid>http://drupalhigh.onsugar.com/2669617</guid>
</item>
</channel>
</rss>
