<HTML>
<HEAD>
<TITLE>Re: [support] Content type modules, install and uninstall code</TITLE>
</HEAD>
<BODY>
<FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>Next question. How do you copy code out of Dreamweaver and have it come out looking right?<BR>
-- <BR>
Alex Adams<BR>
<BR>
hawkVision — see the elephant for what it is<BR>
A2 Technology Partners, Inc.<BR>
831-724-1397<BR>
<a href="alex@a2technology.com">alex@a2technology.com</a><BR>
www.a2technology.com<BR>
<BR>
<BR>
<HR ALIGN=CENTER SIZE="3" WIDTH="95%"><B>From: </B>Alex Adams <<a href="alex@a2technology.com">alex@a2technology.com</a>><BR>
<B>Reply-To: </B><<a href="support@drupal.org">support@drupal.org</a>><BR>
<B>Date: </B>Tue, 31 May 2011 16:51:09 -0700<BR>
<B>To: </B><<a href="support@drupal.org">support@drupal.org</a>><BR>
<B>Subject: </B>[support] Content type modules, install and uninstall code<BR>
<BR>
I’m new to Drupal. A very experienced programmer, but new to Drupal. I have worked my way through Beginning Drupal 7 by Todd Tomlinson and Pro Drupal 7 Development by Todd Tomlinson and John K VanDyk. I’ve watched some videos and online code examples.<BR>
<BR>
Here’s where I am in my project. The site is set up, contributed modules installed, base theme installed. I’m beginning to add 13 custom content types. I want them to be added from modules, 1 for each type since there will be a cafeteria plan. There’s much more, but I’m stuck here.<BR>
<BR>
In the books there was a example of adding one field to a new content type. All created in a module. The exercise worked great. However, when I try it with one of my modules that has multiple fields, things get screwed up. <BR>
<BR>
- The fields are added to the database, but they do not appear on the form.<BR>
- During uninstall, the field tables are not removed from the database.<BR>
- I don’t know how to set all of the properties of the data field or widget that I can when I am creating a custom content type using the administrative pages.<BR>
- I want the fields to be contained in fieldsets upon installation. I can do this if I am creating a form from scratch with code, but how do I do this in a custom content type module.install script?<BR>
<BR>
I’ve screwed around with this so much now that I’m not sure when what stopped working.<BR>
<BR>
Here are some code snippets I’ve tried.<BR>
<BR>
----<BR>
function _job_post_installed_fields() { $t = get_t(); return array( 'job_post_company' => array( 'field_name' => 'job_post_company', 'label' => $t('Company posting the job listing'), 'type' => 'text', ), 'job_post_color' => array( 'field_name' => 'job_post_color', 'label' => $t('Job post color'), 'type' => 'text', ), ); } /** * return a structured array defining the field instances associated with this content type */ function _job_post_installed_instances() { $t = get_t(); return array( 'job_post_company' => array( 'field_name' => 'job_post_company', 'type' => 'text', 'label' => $t('Company posting the job listing'), 'widget' => array( 'type' => 'text_textfield', ), 'display' => array( 'example_node_list' => array( 'label' => $t('Company posting the job listing'), 'type' => 'text', ), ), ), 'job_post_color' => array( 'field_name' => 'job_post_color', 'type' => 'text', 'label' => $t('Job post color'), 'widget' => array( 'type' => 'text_textfield', ), 'display' => array( 'example_node_list' => array( 'label' => $t('Job post color'), 'type' => 'text', ), ), ), ); }<BR>
----<BR>
And <BR>
----<BR>
function _hawk_topic_installed_fields() { $t = get_t(); return array( 'hawk_topic_type' => array( 'field_name' => 'hawk_topic_type', 'label' => $t('hawk Topic Type'), 'type' => 'text', 'settings' => array( 'max_length' => 50, ), ), 'hawk_topic_color' => array( 'field_name' => 'hawk_topic_color', 'label' => $t('hawk Topic Color'), 'type' => 'text', 'settings' => array( 'max_length' => 20, ), ), ); } /** * return a structured array defining the field instances associated with this content type */ function _hawk_topic_installed_instances() { $t = get_t(); return array( 'hawk_topic_type' => array( 'field_name' => 'hawk_topic_type', 'type' => 'text', 'label' => $t('Type of topic'), 'widget' => array( 'type' => 'text_textfield', 'settings' => array('size' => 60), ), 'display' => array( 'example_node_list' => array( 'label' => $t('Type of topic'), 'type' => 'text', ), ), ), 'hawk_topic_color' => array( 'field_name' => 'hawk_topic_color', 'type' => 'text', 'label' => $t('Color of topic'), 'widget' => array( 'type' => 'text_textfield', 'settings' => array('size' => 20), ), 'display' => array( 'example_node_list' => array( 'label' => $t('Color of topic'), 'type' => 'text', ), ), ), ); }<BR>
----<BR>
The uninstall script seems to be right out of the book (pun intended).<BR>
----<BR>
function hawk_topic_uninstall() { // gather all the example content that might have been created while this // module was enabled $sql = 'SELECT nid FROM {node} n WHERE n.type = :type'; $result = db_query($sql, array(':type' => 'hawk_topic')); $nids = array (); foreach ($result as $row) { $nids[] = $row->nid; } // delete all the nodes at once node_delete_multiple($nids); // loop over each of the fields defined by this moduel and delete // all instance of the field, their data, and the field itself. foreach (array_keys(_hawk_topic_installed_fields()) as $field) { field_delete_field($field); } // loop over any remaining file instances attached to the hawk_topic // content type (such as body field) and delete them individually $instances = field_info_instances('node', 'hawk_topic'); foreach ($instances as $instance_name => $instance) { field_delete_instance($instance); } // delete our content type node_type_delete('hawk_topic'); // purge all field information field_purge_batch(1000); }<BR>
----<BR>
<BR>
Any ideas? Probably a beginners mistake?<BR>
<BR>
Thanks in advance,<BR>
-- <BR>
Alex Adams<BR>
<BR>
hawkVision — see the elephant for what it is<BR>
A2 Technology Partners, Inc.<BR>
831-724-1397<BR>
<a href="alex@a2technology.com">alex@a2technology.com</a><BR>
www.a2technology.com<BR>
<BR>
<HR ALIGN=CENTER SIZE="3" WIDTH="95%"></SPAN></FONT><FONT SIZE="2"><FONT FACE="Consolas, Courier New, Courier"><SPAN STYLE='font-size:10pt'>-- <BR>
[ Drupal support list | <a href="http://lists.drupal.org/">http://lists.drupal.org/</a> ]<BR>
</SPAN></FONT></FONT>
</BODY>
</HTML>