<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Passing Curiosity: Posts tagged views</title>
    <link href="https://passingcuriosity.com/tags/views/views.xml" rel="self" />
    <link href="https://passingcuriosity.com" />
    <id>https://passingcuriosity.com/tags/views/views.xml</id>
    <author>
        <name>Thomas Sutton</name>
        
        <email>me@thomas-sutton.id.au</email>
        
    </author>
    <updated>2010-07-06T00:00:00Z</updated>
    <entry>
    <title>Going Further with Litecal</title>
    <link href="https://passingcuriosity.com/2010/going-further-with-litecal/" />
    <id>https://passingcuriosity.com/2010/going-further-with-litecal/</id>
    <published>2010-07-06T00:00:00Z</published>
    <updated>2010-07-06T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>My <a href="/2010/using-litecal/">last article</a> was a quick introduction to the
<code>litecal</code> module. In this article, I’ll show how to go further with Litecal to
create more complete user interfaces. You’ll see how to use associate related
events by colour, how to add additional displays of the same data, and a few
tips on themeing Litecal.</p>
<hr />
<h3 id="using-colour-in-the-calendar">Using colour in the calendar</h3>
<p>Most of us are familiar with programs like iCal which use colour “link”
related events together on a calendar. One way to make a Litecal-based view
immediately more usable is to implement this technique. Thankfully it’s rather
easy to do thanks, once again, to <a href="http://developmentseed.org/">Development Seed</a> and their
<a href="http://code.developmentseed.org/node/176">Seed</a> module. Seed contains useful code and customisations for Litecal
and a number of other modules. The name suggests that it’s intended for
semi-internal use (maybe just Open Atrium, I’m not sure), but it’s GPL and the
awesome people at DS released it so I don’t feel too bad in making use of it.</p>
<p>Do the usual dance of downloading, unpacking and installing the module from
the link above (Seed depends on <a href="http://drupal.org/project/context">Context</a>, so install that too). Once
that’s done, you’ll need to return to the view you created in the last
article.</p>
<p>Go back and edit your calendar. Select the Calendar display, update the <em>Style
options</em> and change <em>Color by</em> to <code>field_event_location_value</code>. Once you’ve
updated (or saved) the display, Litecal will use Seed to assign a colour to
each event displayed on <em>the calendar based on the value of the location
field</em>. Events at the same location (where “same” means “has the same CRC32”)
will have the same colour! Seed only supports 16 colours but this should be
enough for most purposes and the colours themselves are easily overridden in
your theme stylesheet.</p>
<p>As great as this is (and you’ll have to agree, it is pretty awesome) there are
a few little niggles. First, when Seed is installed Litecal might get confused
and fail to render events in the <em>Live Preview</em> (this might just be Theme
Developer, though). Second, Seed doesn’t just add support for colours to
Litecal; it also overrides the user picture template and a bunch of other
stuff that you probably want left alone. Third, it’s adding Context for no
particular reason (at least in this demo).</p>
<p>I’m still a bit puzzled by the first issue, but the others are readily solved
by pulling the colouring code out of Seed (look for the theme functions with
<code>crayon</code> in their names) and whacking it in another module, but you’ll also
need to tweak the code of Litecal (look for the <code>module_exists('seed')</code>). I
hope that this colouring functionality will be merged into Litecal itself at
some point, or at least pulled out into a module that doesn’t do a bunch of
other stuff too.</p>
<h3 id="additional-displays">Additional displays</h3>
<p>A calendar by itself is great for little things – “Yes, I’m free on Friday
13th” – but might not be enough in cases where users need more complete
information at a glance. That’s why so many calendar applications also display
events in a list or table view. With a pretty feature-full calendar, this list
view is next on the list! The goal is to add a table with the most pertinent
details about each event below the calendar display.</p>
<p>Having though of just this combination of use cases, Views makes this an
absolute cinch. Go back to your view and add a new <em>Attachment</em> display.
Change the name of your new display and override the <em>Style</em>, setting it to
<em>Table</em> (sorted by the Date field).</p>
<p>Edit the <em>Attachment settings</em> like this:</p>
<ul>
<li><p><em>Inherit arguments</em> should be <em>yes</em> (so that the table displays the same
month as the calendar).</p></li>
<li><p><em>Postition</em> should be <em>After</em> (it’ll look a bit odd before the calendar).</p></li>
<li><p><em>Attach to</em> the calendar display you created in the first article.</p></li>
</ul>
<p>There are a few more tweaks you might like to make at this point:</p>
<ul>
<li><p>Add <em>Sort criteria</em> in the <em>Default</em> display so that the events are sorted
by <em>Content: Date</em> in ascending order. This might help keep things
consistent in the event that you need to display the pager.</p></li>
<li><p>Add <em>Filters</em> on <em>Date: Date (node)</em> to list only upcoming events in the
table (those where <em>To date</em> is greater than or equal to now).</p></li>
</ul>
<p>Other alternatives include:</p>
<ul>
<li>Using Panels instead of attachment Views to link the table to the calendar.
This makes using the pager slightly simpler.</li>
</ul>
<h3 id="themeing-litecal">Themeing Litecal</h3>
<p>Given that it’s called “Key”, it’d be nice if the table display was actually a
key for the calendar display. You can add a column with the same colour as is
used in the calendar quite with a simple change to the view and a less simple
change to your theme.</p>
<p>If you looked at the colouring code in Seed and the way it’s invoke from
Litecal, you probably noticed that it’s fairly simple: get the “key” value,
feed it to the colour picker code (<code>theme('crayon', $key)</code>) and use the result
to add a class to the element.</p>
<p>If you followed the instructions above, your calendar entries are colour coded
according to the location field. To add the correct colour to the table,
you’ll need to add a new column containing this value and then use the theme
layer to turn this into a coloured splotch.</p>
<p>Edit your “Key” display and add a new <em>Content: Location</em> field. Re-order the
fields and move this new column to the top of the list as you want the
colour-coding to be the first item in each row. Save that and then load the
view in your browser. Using whatever inspector tool your web-browser provides
(or “View Source” if you’re a sadist), determine the [longest] class on the
cells in the new column. Mine is: <code>views-field-field-event-location-value-1</code>.</p>
<p>This class is generated based on two facts: it is a view field, and the field
being displayed is <code>field-event-location-value-1</code>. You can use this to work
out what the theme function you are about to write should be called. It will
be something like this:</p>
<pre><code>`&lt;prefix&gt;_views_view_field__&lt;view_name&gt;__attachment__&lt;field_name&gt;`</code></pre>
<p>where <code>&lt;prefix&gt;</code> is your theme name (or theme engine name), <code>&lt;view_name&gt;</code> is
the name you assigned your view when you created it, and <code>&lt;field_name&gt;</code> is the
name used in the class above (but with <code>_</code>s instead of <code>-</code>s).</p>
<p>Fill in the three blanks (you can almost always use <code>phptemplate</code> for the
prefix and expect it to work!) and add a function like this to the
<code>template.php</code> in your theme:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode php"><code class="sourceCode php"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> phptemplate_views_view_field__events_calendar__attachment__field_event_location_value_1(<span class="va">$view</span><span class="ot">,</span> <span class="va">$field</span><span class="ot">,</span> <span class="va">$row</span>)</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>{</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>  <span class="co">// Get the &quot;key&quot;. This is just the value that would be displayed in the </span></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a>  <span class="co">// table cell if we weren't messing with things.</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a>  <span class="va">$text</span> <span class="op">=</span> <span class="va">$view</span>-&gt;field[<span class="va">$field</span>-&gt;options[<span class="st">'id'</span>]]-&gt;advanced_render(<span class="va">$row</span>)<span class="ot">;</span></span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a>  </span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a>  <span class="va">$class</span> <span class="op">=</span> <span class="st">&quot;crayon crayon-&quot;</span>theme(<span class="st">'seed_crayon'</span><span class="ot">,</span> <span class="va">$text</span>)<span class="ot">;</span></span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a>  </span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a>  <span class="co">// Create a span with the class </span></span>
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a>  <span class="va">$r</span> <span class="op">=</span> <span class="st">&quot;&lt;span class='</span><span class="va">$class</span><span class="st">' style='display:block;height:5px;width:15px;'&gt;&lt;/span&gt;&quot;</span><span class="ot">;</span></span>
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a>  </span>
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a>  <span class="cf">return</span> <span class="va">$r</span><span class="ot">;</span></span>
<span id="cb2-13"><a href="#cb2-13" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
<p>Once this new function is in place you will need to refresh your theme caches
(just hitting “Save” on <code>/admin/build/themes</code> will do the trick). And now your
calendar page should look something like this:</p>
<figure>
<img src="/files/2010/07/litecal-themed-calendar.jpg" alt="The finished calendar" />
<figcaption aria-hidden="true">The finished calendar</figcaption>
</figure>]]></summary>
</entry>
<entry>
    <title>Using Litecal</title>
    <link href="https://passingcuriosity.com/2010/using-litecal/" />
    <id>https://passingcuriosity.com/2010/using-litecal/</id>
    <published>2010-07-05T00:00:00Z</published>
    <updated>2010-07-05T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p><a href="http://code.developmentseed.org/litecal/node/285">litecal</a> is a small Drupal module written by <a href="http://www.developmentseed.org/">Development Seed</a>
to provide a lightweight calendar display for Drupal sites. Like the larger
and more powerful <a href="http://drupal.org/project/calendar">calendar</a> module, <code>litecal</code> is depends on both
the Views and Date API modules. Unlike <code>calendar</code>, <code>litecal</code> is relatively
small and quite easy to understand. The rest of this post will guide you
through installing and using <code>litecal</code> to create a calendar of events on
your Drupal site.</p>
<hr />
<h3 id="installing-litecal">Installing <code>litecal</code></h3>
<p>As mentioned above, <code>litecal</code> has a number of dependancies. You’ll need to
download and install the following modules:</p>
<ol type="1">
<li><a href="http://code.developmentseed.org/litecal/node/285">Litecal</a> itself.</li>
<li><a href="http://drupal.org/project/cck">Content</a> and Text - to store the event data.</li>
<li><a href="http://drupal.org/project/date">Date</a> and Date API - to process and store date data.</li>
<li><a href="http://drupal.org/project/views">Views</a> - to display the calendar.</li>
</ol>
<p>Once you’ve installed the modules you may need to configure them (you’ll need
to have set the site timezone, if nothing else), and then move on to the next
step.</p>
<h3 id="creating-the-node-type">Creating the node type</h3>
<p>Before you can display events on a calendar, you’ll need somewhere to store
them so the first step is creating a new content type.</p>
<figure>
<img src="/files/2010/07/litecal-event-type.jpg" alt="Create an event content type" />
<figcaption aria-hidden="true">Create an event content type</figcaption>
</figure>
<p>Next you’ll need to add a few fields to it. You can store the event name and
description in the <code>title</code> and <code>body</code> fields, but you’ll need to add a new
text field for the location and a new date field for the date. Make sure that:</p>
<ul>
<li>The date field is required (you can’t have an event with no date).</li>
<li>The “to date” is optional.</li>
<li>At least year, month and day are selected under granularity.</li>
</ul>
<p>When you’re done you should have something like the following:</p>
<figure>
<img src="/files/2010/07/litecal-type-fields.jpg" alt="Manage fields on calendar event" />
<figcaption aria-hidden="true">Manage fields on calendar event</figcaption>
</figure>
<p>Now that you’ve got a <em>Calendar Event</em> node type, create a few dummy nodes so
that there’s something to display on the calendar. Four or five should do it,
but make sure that a few are <em>this month</em> and one or two in past or future
months, or your calendar will be a bit empty!</p>
<figure>
<img src="/files/2010/07/litecal-create-nodes.jpg" alt="Create some Calendar Event nodes" />
<figcaption aria-hidden="true">Create some Calendar Event nodes</figcaption>
</figure>
<h3 id="creating-the-view">Creating the view</h3>
<p>Now you need to create a view to display the calendar events in a calendar.
Create a new view for nodes and add the following:</p>
<ol type="1">
<li><p>A filter <code>Node: Type = Calendar Event</code>.</p></li>
<li><p>A <code>Date: Date (node)</code> argument (select the <em>Current date</em> as default
argument, <em>Month</em> granularity, both <em>From</em> and <em>To</em> dates, and <em>OR</em>
method).</p></li>
<li><p>A <code>Content: Date - From date</code> field from your content type, a <code>Content:     Location</code> field, and a <code>Node: Title</code> field.</p></li>
</ol>
<p>Your view should look something like this:</p>
<figure>
<img src="/files/2010/07/litecal-create-view.jpg" alt="Create a view" />
<figcaption aria-hidden="true">Create a view</figcaption>
</figure>
<p>Now add a new <code>Page</code> display to the view, set its <em>Name</em> to <code>Calendar</code>, the
<em>Path</em> to <code>calendar</code>, and override the <em>Style</em> and set it to <code>Litecal</code> (leave
the style options as the default values).</p>
<figure>
<img src="/files/2010/07/litecal-calendar-display.jpg" alt="Add a calendar display" />
<figcaption aria-hidden="true">Add a calendar display</figcaption>
</figure>
<p>Save the view, then select the correct display under <em>Live Preview</em> and you
should see the dummy nodes you created earlier on a calendar of the current
month. If not, then you’ve missed a step above (perhaps the default value or
granularity for the argument?).</p>
<figure>
<img src="/files/2010/07/litecal-preview.jpg" alt="The calendar in live preview" />
<figcaption aria-hidden="true">The calendar in live preview</figcaption>
</figure>
<p>You should also be able to visit <code>/calendar</code> and see the same calendar as a
page:</p>
<figure>
<img src="/files/2010/07/litecal-calendar-page.jpg" alt="The calendar in a page" />
<figcaption aria-hidden="true">The calendar in a page</figcaption>
</figure>
<h3 id="conclusion">Conclusion</h3>
<p>You’ve seen how easy it is to get started with the <code>litecal</code> module. In the
next post, I’m going to introduce a few more sophisticated ways you can use
<code>litecal</code> to build more complete user interface.</p>]]></summary>
</entry>

</feed>
