Easy Integration of Rich Internet Applications with Drupal - The Installation Hooks

This is the last post in the Easy Integration of Rich Internet Applications with Drupal series. It describes the Drupal installation hooks that automate the creation of the Rich Internet Application content type as well as that of its related fields..

  1. /**
  2.  *
  3.  * Installs a new content type ria and attaches custom fields to it.
  4.  */
  5. function ria_install() {
  6.     $t = get_t();
  7.    
  8.     $node_type_ria_attributes = array(
  9.         'type'  => 'ria',
  10.         'name'  => $t('RIA'),
  11.         'description' => $t('A Rich Internet Application'),
  12.         'base'      => 'node_content',
  13.         'body_label'  => 'HTML'
  14.     );
  15.    
  16.     $node_type_ria = node_type_set_defaults($node_type_ria_attributes);
  17.     node_add_body_field($node_type_ria);
  18.     node_type_save($node_type_ria);
  19.  
  20.     _ria_install_fields();
  21. }

Creating RIA content type fields and field instances.

  1. function _ria_install_fields() {
  2.   $t = get_t();
  3.  
  4.   // Field types
  5.   $field_types = array();
  6.  
  7.   $field_types['application_directory'] = array(
  8.       'field_name'  => 'field_ria_application_directory',
  9.       'type'  => 'text'
  10.   );
  11.  
  12.   $field_types['javascript_api'] = array(
  13.       'field_name'  => 'field_ria_javascript_api',
  14.       'type'        => 'text',
  15.       'cardinality' => 3
  16.   );
  17.  
  18.   $field_types['css'] = array(
  19.       'field_name'  => 'field_ria_css',
  20.       'type'        => 'text'
  21.   );
  22.  
  23.   $field_types['application_javascript'] = array(
  24.       'field_name'  => 'field_ria_application_javascript',
  25.       'type'        => 'text'
  26.   );
  27.  
  28.   $field_types['inline_javascript'] = array(
  29.       'field_name'  =>  'field_ria_inline_javascript',
  30.       'type'        =>  'text'
  31.   );
  32.  
  33.   $field_types['description'] = array(
  34.       'field_name'  => 'field_ria_description',
  35.       'type'        => 'text_with_summary'
  36.   );
  37.  
  38.   foreach ($field_types as $name => $field_type) {
  39.       field_create_field($field_type);
  40.   }
  41.    
  42.   // Display
  43.   $display_hidden = array(
  44.     'default' => array(
  45.       'label' => 'hidden',
  46.       'type'  => 'hidden'
  47.     ),
  48.     'teaser'  => array(
  49.       'label' => 'hidden',
  50.       'type'  => 'hidden'
  51.     )
  52.   );
  53.    
  54.   // Field instances
  55.   $field_instances = array();
  56.  
  57.   $field_instances['application_directory'] = array(
  58.     'field_name'  => 'field_ria_application_directory',
  59.     'label'       => $t('Application Directory'),
  60.     'description' => $t('Directory where application resources will be stored.'),
  61.     'required'    => TRUE,
  62.     'display'     => $display_hidden
  63.   );
  64.  
  65.   $field_instances['javascript_api'] = array(
  66.     'field_name'  => 'field_ria_javascript_api',
  67.     'label'       => $t('JavaScript API'),
  68.     'description' => $t('URL of a JavaScript library used by the application.'),
  69.     'display'     => $display_hidden
  70.   );
  71.  
  72.   $field_instances['application_javascript'] = array(
  73.     'field_name'  => 'field_ria_application_javascript',
  74.     'label'       => $t('Application JavaScript'),
  75.     'description' => $t('Javascript file name.'),
  76.     'required'    => TRUE,
  77.     'display'     => $display_hidden
  78.   );
  79.  
  80.   $field_instances['inline_javascript'] = array(
  81.     'field_name'  => 'field_ria_inline_javascript',
  82.     'label'       => $t('Inline Javascript'),
  83.     'description' => $t("Any Javascript that needs to be added to document's head."),
  84.     'display'     => $display_hidden
  85.   );
  86.  
  87.   $field_instances['css'] = array(
  88.     'field_name'  => 'field_ria_css',
  89.     'label'       => $t('CSS'),
  90.     'description' => $t('Custom CSS file name.'),
  91.     'display'     => $display_hidden
  92.   );
  93.  
  94.   $field_instances['description'] = array(
  95.     'field_name'  => 'field_ria_description',
  96.     'label'       => $t('Description'),
  97.     'description' => $t('Description of what the application does.'),
  98.     'required'    => TRUE,
  99.     'display'     => array(
  100.       'default' => array('label' => 'hidden'),
  101.       'teaser'  => array('label' => 'hidden', 'type'  => 'text_summary_or_trimmed')
  102.     )
  103.   );
  104.  
  105.   foreach ($field_instances as $name => $instance) {
  106.     $instance['entity_type'] = 'node';
  107.     $instance['bundle'] = 'ria';
  108.     field_create_instance($instance);
  109.   }
  110.  
  111.   // Change settings for field body
  112.   $body = field_read_instance('node', 'body', 'ria');
  113.   $body_instance = array(
  114.     'entity_type' => 'node',
  115.     'field_name'  => 'body',
  116.     'bundle'      => 'ria',
  117.     'label'       => $t('HTML'),
  118.     'display'     => array(
  119.       'teaser'  => array('label' => 'hidden', 'type' => 'hidden'),
  120.       'default' => array('label' => 'hidden')
  121.     )
  122.    );
  123.    
  124.    field_update_instance($body_instance);
  125. }


The uninstall hook.

  1. function ria_uninstall() {
  2.     node_type_delete('ria');
  3.     _ria_delete_fields();
  4. }
  5.  
  6. function _ria_delete_fields() {
  7.   $fields = array('field_ria_application_directory', 'field_ria_javascript_api', 'field_ria_application_javascript',
  8.                   'field_ria_inline_javascript', 'field_ria_description', 'field_ria_css');
  9.                  
  10.   foreach ($fields as $field) {
  11.     field_delete_field($field);
  12.   }
  13. }
Thoughts: