Advertisement
benjamin_mcf

cludder.install

Dec 23rd, 2011
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.41 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. /**
  5.  * Implements hook_install()
  6.  */
  7. function cludder_install() {
  8.   // During installation, the t() function is unavailable, so we use get_t()
  9.   // to store the name of the translation function.
  10.   $t = get_t();
  11.  
  12.   // We define the node type as an associative array.
  13.   $cludder = array(
  14.     'type' => 'cludder',
  15.     'name' => $t('cludder'),
  16.     // 'base' tells Drupal the base string for hook functions.
  17.     // This is often the module name; if base is set to 'mymodule', Drupal
  18.     // would call mymodule_insert() or similar for node hooks.
  19.     // In this case, we set base equal to 'node_content' so Drupal will handle
  20.     // our node as if we had designed it in the UI.
  21.     'base' => 'node_content',
  22.     'description' => $t('This is an cludder type with a few fields.'),
  23.     'custom' => TRUE,
  24.   );
  25.  
  26.   // Complete the node type definition by setting any defaults not explicitly
  27.   // declared above.
  28.   // http://api.drupal.org/api/function/node_type_set_defaults/7
  29.   $content_type = node_type_set_defaults($cludder);
  30.  
  31.   // Save the content type
  32.   node_type_save($content_type);
  33.  
  34.  
  35.  
  36.  
  37.   // Create all the fields we are adding to our content type.
  38.   // http://api.drupal.org/api/function/field_create_field/7
  39.   foreach (_cludder_installed_fields() as $field) {
  40.     field_create_field($field);
  41.   }
  42.  
  43.   // Create all the instances for our fields.
  44.   // http://api.drupal.org/api/function/field_create_instance/7
  45.   foreach (_cludder_installed_instances() as $instance) {
  46.     $instance['entity_type'] = 'node';
  47.     $instance['bundle'] = $cludder['type'];
  48.     field_create_instance($instance);
  49.   }
  50. }
  51.  
  52. /**
  53.  * Implements hook_uninstall().
  54.  *
  55.  * This hook is called when the user not only has disabled the module,
  56.  * but also uninstalls it from the 'uninstall' tab in the module page.
  57.  *
  58.  * So it's a perfect time to remove our fields and instances and new
  59.  * node type from the database.
  60.  *
  61.  * @ingroup cludder
  62.  */
  63. function cludder_uninstall() {
  64.   // Gather all the example content that might have been created while this
  65.   // module was enabled.  Simple selects still use db_query().
  66.   // http://api.drupal.org/api/function/db_query/7
  67.   $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
  68.   $result = db_query($sql, array(':type' => 'cludder'));
  69.   $nids = array();
  70.   foreach ($result as $row) {
  71.     $nids[] = $row->nid;
  72.   }
  73.  
  74.   // Delete all the nodes at once
  75.   // http://api.drupal.org/api/function/node_delete_multiple/7
  76.   node_delete_multiple($nids);
  77.  
  78.   // Loop over each of the fields defined by this module and delete
  79.   // all instances of the field, their data, and the field itself.
  80.   // http://api.drupal.org/api/function/field_delete_field/7
  81.   foreach (array_keys(_cludder_installed_fields()) as $field) {
  82.     field_delete_field($field);
  83.   }
  84.  
  85.   // Loop over any remaining field instances attached to the cludder
  86.   // content type (such as the body field) and delete them individually.
  87.   // http://api.drupal.org/api/function/field_delete_field/7
  88.   $instances = field_info_instances('node', 'cludder');
  89.   foreach ($instances as $instance_name => $instance) {
  90.     field_delete_instance($instance);
  91.   }
  92.  
  93.   // Delete our content type
  94.   // http://api.drupal.org/api/function/node_type_delete/7
  95.   node_type_delete('cludder');
  96.  
  97.   // Purge all field infromation
  98.   // http://api.drupal.org/api/function/field_purge_batch/7
  99.   field_purge_batch(1000);
  100. }
  101.  
  102. /**
  103.  * Returns a structured array defining the fields created by this content type.
  104.  *
  105.  * This is factored into this function so it can be used in both
  106.  * cludder_install() and cludder_uninstall().
  107.  *
  108.  * @return
  109.  *  An associative array specifying the fields we wish to add to our
  110.  *  new node type.
  111.  *
  112.  * @ingroup cludder
  113.  */
  114. function _cludder_installed_fields() {
  115.   $t = get_t();
  116.   return array(
  117.     'subject_stuff' => array(
  118.       'field_name' => 'subject_stuff',
  119.       'type'       => 'subject_field_type',
  120.     ),
  121.     'grade_stuff' => array(
  122.       'field_name' => 'grade_stuff',
  123.       'type'       => 'grade_field_type',
  124.     ),
  125.     'strand_stuff' => array(
  126.       'field_name' => 'strand_stuff',
  127.       'type'       => 'strand_field_type',
  128.     ),
  129.     'topic_stuff' => array(
  130.       'field_name' => 'topic_stuff',
  131.       'type'       => 'topic_field_type',
  132.     ),
  133.   );
  134. }
  135.  
  136. /**
  137.  * Returns a structured array defining the instances for this content type.
  138.  *
  139.  * The instance lets Drupal know which widget to use to allow the user to enter
  140.  * data and how to react in different view modes.  We are going to display a
  141.  * page that uses a custom "cludder_list" view mode.  We will set a
  142.  * cardinality of three allowing our content type to give the user three color
  143.  * fields.
  144.  *
  145.  * This is factored into this function so it can be used in both
  146.  * cludder_install() and cludder_uninstall().
  147.  *
  148.  * @return
  149.  *  An associative array specifying the instances we wish to add to our new
  150.  *  node type.
  151.  *
  152.  * @ingroup cludder
  153.  */
  154. function _cludder_installed_instances() {
  155.   $t = get_t();
  156.   return array(
  157.     'subject_stuff' => array(
  158.       'field_name'  => 'subject_stuff',
  159.       'required'    => FALSE,
  160.     ),
  161.     'grade_stuff' => array(
  162.       'field_name'  => 'grade_stuff',
  163.       'required'    => FALSE,
  164.     ),
  165.     'strand_stuff' => array(
  166.       'field_name'  => 'strand_stuff',
  167.       'required'    => FALSE,
  168.     ),
  169.     'topic_stuff' => array(
  170.       'field_name'  => 'topic_stuff',
  171.       'required'    => FALSE,
  172.     ),
  173.   );
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement