Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Implements hook_install()
- */
- function cludder_install() {
- // During installation, the t() function is unavailable, so we use get_t()
- // to store the name of the translation function.
- $t = get_t();
- // We define the node type as an associative array.
- $cludder = array(
- 'type' => 'cludder',
- 'name' => $t('cludder'),
- // 'base' tells Drupal the base string for hook functions.
- // This is often the module name; if base is set to 'mymodule', Drupal
- // would call mymodule_insert() or similar for node hooks.
- // In this case, we set base equal to 'node_content' so Drupal will handle
- // our node as if we had designed it in the UI.
- 'base' => 'node_content',
- 'description' => $t('This is an cludder type with a few fields.'),
- 'custom' => TRUE,
- );
- // Complete the node type definition by setting any defaults not explicitly
- // declared above.
- // http://api.drupal.org/api/function/node_type_set_defaults/7
- $content_type = node_type_set_defaults($cludder);
- // Save the content type
- node_type_save($content_type);
- // Create all the fields we are adding to our content type.
- // http://api.drupal.org/api/function/field_create_field/7
- foreach (_cludder_installed_fields() as $field) {
- field_create_field($field);
- }
- // Create all the instances for our fields.
- // http://api.drupal.org/api/function/field_create_instance/7
- foreach (_cludder_installed_instances() as $instance) {
- $instance['entity_type'] = 'node';
- $instance['bundle'] = $cludder['type'];
- field_create_instance($instance);
- }
- }
- /**
- * Implements hook_uninstall().
- *
- * This hook is called when the user not only has disabled the module,
- * but also uninstalls it from the 'uninstall' tab in the module page.
- *
- * So it's a perfect time to remove our fields and instances and new
- * node type from the database.
- *
- * @ingroup cludder
- */
- function cludder_uninstall() {
- // Gather all the example content that might have been created while this
- // module was enabled. Simple selects still use db_query().
- // http://api.drupal.org/api/function/db_query/7
- $sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
- $result = db_query($sql, array(':type' => 'cludder'));
- $nids = array();
- foreach ($result as $row) {
- $nids[] = $row->nid;
- }
- // Delete all the nodes at once
- // http://api.drupal.org/api/function/node_delete_multiple/7
- node_delete_multiple($nids);
- // Loop over each of the fields defined by this module and delete
- // all instances of the field, their data, and the field itself.
- // http://api.drupal.org/api/function/field_delete_field/7
- foreach (array_keys(_cludder_installed_fields()) as $field) {
- field_delete_field($field);
- }
- // Loop over any remaining field instances attached to the cludder
- // content type (such as the body field) and delete them individually.
- // http://api.drupal.org/api/function/field_delete_field/7
- $instances = field_info_instances('node', 'cludder');
- foreach ($instances as $instance_name => $instance) {
- field_delete_instance($instance);
- }
- // Delete our content type
- // http://api.drupal.org/api/function/node_type_delete/7
- node_type_delete('cludder');
- // Purge all field infromation
- // http://api.drupal.org/api/function/field_purge_batch/7
- field_purge_batch(1000);
- }
- /**
- * Returns a structured array defining the fields created by this content type.
- *
- * This is factored into this function so it can be used in both
- * cludder_install() and cludder_uninstall().
- *
- * @return
- * An associative array specifying the fields we wish to add to our
- * new node type.
- *
- * @ingroup cludder
- */
- function _cludder_installed_fields() {
- $t = get_t();
- return array(
- 'subject_stuff' => array(
- 'field_name' => 'subject_stuff',
- 'type' => 'subject_field_type',
- ),
- 'grade_stuff' => array(
- 'field_name' => 'grade_stuff',
- 'type' => 'grade_field_type',
- ),
- 'strand_stuff' => array(
- 'field_name' => 'strand_stuff',
- 'type' => 'strand_field_type',
- ),
- 'topic_stuff' => array(
- 'field_name' => 'topic_stuff',
- 'type' => 'topic_field_type',
- ),
- );
- }
- /**
- * Returns a structured array defining the instances for this content type.
- *
- * The instance lets Drupal know which widget to use to allow the user to enter
- * data and how to react in different view modes. We are going to display a
- * page that uses a custom "cludder_list" view mode. We will set a
- * cardinality of three allowing our content type to give the user three color
- * fields.
- *
- * This is factored into this function so it can be used in both
- * cludder_install() and cludder_uninstall().
- *
- * @return
- * An associative array specifying the instances we wish to add to our new
- * node type.
- *
- * @ingroup cludder
- */
- function _cludder_installed_instances() {
- $t = get_t();
- return array(
- 'subject_stuff' => array(
- 'field_name' => 'subject_stuff',
- 'required' => FALSE,
- ),
- 'grade_stuff' => array(
- 'field_name' => 'grade_stuff',
- 'required' => FALSE,
- ),
- 'strand_stuff' => array(
- 'field_name' => 'strand_stuff',
- 'required' => FALSE,
- ),
- 'topic_stuff' => array(
- 'field_name' => 'topic_stuff',
- 'required' => FALSE,
- ),
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement