theoleschool - schema http://theoleschool.com/tags/schema en Adopting a Table with Schema using Data Module http://theoleschool.com/blog/adopting-table-schema-using-data-module <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p>When working with medium to larger sized web applications, inevitably the requirement comes up for some advanced data reporting. The spectrum of requests here can obviously be pretty broad and is often beyond what you can accomplish with the default tables provided by Drupal and the installed modules. This tends to lead us to creating custom tables to track and manage the requested reports. Along with writing all of the necessary <a href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD</a> functions to make sure our new table is managing data appropriately, we also have to provide some sort of display or an export. Views can handle this latter requirement quite nicely. So it is here that we can turn to modules like <a href="http://drupal.org/project/data">Data</a> or the now depricated <a href="http://drupal.org/project/tw">Table Wizard</a> to bring our new table under views "control." This gives us the added bonus of taking the display configuration out of the hands of the developer and placing it into the site builder/administrator's domain.</p> <p>In a recent project just such a reporting request came up. The report was fairly complex and was to be visible to a larger subset of users. I wound up managing this report with a custom table and I turned to Data module to allow views to handle the output. After installing my custom module which created the custom table, I navigated to <span class="geshifilter"><code class="php geshifilter-php">admin<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>data<span style="color: #339933;">/</span>adopt</code></span>. Expecting to see my table in the list of "adoptable" tables, I was a bit confused when it was nowhere to be found. After <a href="http://drupal.org/node/888946#comment-3360326">a bit of digging</a> I realized that tables declared using <a href="http://drupal.org/node/146843">Schema API</a> were considered "owned" by the module and not included in the list of adoptable tables. This makes some sense as you don't want just any table adopted by Data. Just imagine when an admin who's not quite sure what he or she is doing, decides to adopt and then drop the node table. I'd rather not...</p> <p>So this leaves us with the question, <em>"How do I get Data module to adopt my table?"</em> From what I can tell, the creators of the data module expect us to use the Data API to handle table creation and CRUD. Personally I don't like the idea of this. I already understand and like working with Drupal's core Schema API, and don't want to adopt another layer of abstraction. I only want to utilize Data module to throw my table up to views. So after looking through how adoption is handled in the module, I found that really there is nothing preventing Data from adopting these schema declared tables. They are simply being made unavailable to the form at <span class="geshifilter"><code class="php geshifilter-php">admin<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>data<span style="color: #339933;">/</span>adopt</code></span>. This lead me to attempt to adopt my table right after it is generated in my module's .install file. This wound up working quite nicely and is implemented as follows:</p> <div class="geshifilter"><div class="drupal6 geshifilter-drupal6" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> my_module_install<span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span><br />   <span style="color: #808080; font-style: italic;">//install schema as normal</span><br />   <a href="http://api.drupal.org/api/function/drupal_install_schema/6"><span style="color: #000066;">drupal_install_schema</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'my_table'</span><span style="color: #66cc66;">)</span>;<br />   <br />   <span style="color: #808080; font-style: italic;">//make sure we have the data module</span><br />   <span style="color: #b1b100;">include_once</span><span style="color: #66cc66;">(</span><a href="http://api.drupal.org/api/function/drupal_get_path/6"><span style="color: #000066;">drupal_get_path</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'module'</span>, <span style="color: #ff0000;">'data'</span><span style="color: #66cc66;">)</span> .<span style="color: #ff0000;">'/data.module'</span><span style="color: #66cc66;">)</span>;<br />   data_include<span style="color: #66cc66;">(</span><span style="color: #ff0000;">'DataTable'</span><span style="color: #66cc66;">)</span>;<br />   <span style="color: #808080; font-style: italic;">//instanciate the table we just created with the DataTable class</span><br />   <span style="color: #0000ff;">$my_table_object</span> = DataTable::<span style="color: #006600;">instance</span><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'my_table'</span><span style="color: #66cc66;">)</span>;<br />   <span style="color: #808080; font-style: italic;">//tell Data module to adopt the table</span><br />   <span style="color: #0000ff;">$my_table_object</span>-<span style="color: #66cc66;">&gt;</span><span style="color: #006600;">adopt</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;<br />   DataTable::<span style="color: #006600;">clearCaches</span><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;<br /><span style="color: #66cc66;">}</span></div></div> <p>After installing the module you should be able to see your custom table at <span class="geshifilter"><code class="php geshifilter-php">admin<span style="color: #339933;">/</span>build<span style="color: #339933;">/</span>data</code></span>. Data module is now obviously a dependency so be sure that it is installed and enabled before installing your custom module.</p> <p>I realize that I could write my own views handlers for the custom table data I am providing, but I have found that to be a fairly time consuming (and often esoteric) task. When all we are after is a single, somewhat unique report, I feel that integration with the Data module and it's corresponding integration with views is simple, quick, and more than adequate.</p></div></div></div> Sat, 03 Mar 2012 20:19:36 +0000 rho 156 at http://theoleschool.com