Skip to content

Instantly share code, notes, and snippets.

@Mizpah
Created November 27, 2014 00:29
Show Gist options
  • Save Mizpah/e3afcf9b437241416d88 to your computer and use it in GitHub Desktop.
Save Mizpah/e3afcf9b437241416d88 to your computer and use it in GitHub Desktop.
How to account for different bundles in the installer? Currently my second field (bug_status) has no active instance. I guess I need logic in and around lines 38-42?
<?php
/**
* @file
* Contains content installed by the module.
*
* Created by PhpStorm.
* User: martindean
* Date: 26/11/14
* Time: 05:14
*/
/**
* Implements hook_install().
*/
function genima_log_install() {
// Save all new node types (from all modules).
node_types_rebuild();
// Get all node types.
$types = node_type_get_types();
// Add existing body field to OUR node type.
node_add_body_field($types['logged_item']);
// Call our function to add custom fields during install.
add_custom_fields();
}
/**
* Custom function to add fields.
*
* Uses data read from our custom arrays in _genima_log_installed_fields(),
* and genima_log_installed_instances().
* @todo this will need some sort of logic to distinguish between nodes?
*/
function add_custom_fields() {
// A custom function to add fields, not a drupal hook.
foreach (_genima_log_installed_fields() as $field) {
field_create_field($field);
}
foreach (_genima_log_installed_instances() as $fieldinstance) {
$fieldinstance['entity_type'] = 'node';
$fieldinstance['bundle'] = 'logged_item';
// Actually loop through and do the work.
field_create_instance($fieldinstance);
}
}
/**
* Implements hook_uninstall().
* @todo Insert a confirm during on uninstall, showing node count to remove.
* @todo account for new fields.
*/
function genima_log_uninstall() {
$genimanodetype = 'logged_item';
// Get all node ID's of our content type.
$sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
$result = db_query($sql, array(':type' => $genimanodetype));
$nodeids = array();
foreach ($result as $row) {
$nodeids[] = $row->nid;
}
// Use the collected ID's to delete multiple nodes.
node_delete_multiple($nodeids);
// Add custom field deletion.
delete_custom_fields();
// Now delete the content type.
node_type_delete($genimanodetype);
// Batch fields in 500's.
field_purge_batch(500);
}
/**
* Custom function to delete fields.
*
* Called in genima_log_uninstall().
*/
function delete_custom_fields() {
foreach (array_keys(_genima_log_installed_fields()) as $field) {
field_delete_field($field);
}
$instances = field_info_instances('node', 'logged_item');
foreach ($instances as $instance_name => $fieldinstance) {
field_delete_instance($fieldinstance);
}
}
/**
* Implements hook_installed_fields().
*
* A private instance, just for this module, this is then invoked from the
* add_custom_fields() function.
*/
function _genima_log_installed_fields() {
// Private function, only for this module.
// use get_t(); as t() is unavailable during install.
$t = get_t();
return array(
'logged_item_type_field' => array(
'field_name' => 'logged_item_type',
'label' => $t('Type of log entry'),
'type' => 'text',
),
'bug_status_field' => array(
'field_name' => 'bug_status',
'label' => $t('Bug status'),
'type' => 'text',
),
);
}
/**
* Implements hook_installed_instances().
*
* A private instance, just for this module, this is then invoked from the
* add_custom_fields() function.
*/
function _genima_log_installed_instances() {
// Private function, only for this module.
$t = get_t();
return array(
// Add log type field instance and settings.
'logged_item_type_instance' => array(
'field_name' => 'genima_log_type',
'type' => 'text',
'label' => $t('Type of log entry'),
'widget' => array(
'type' => 'text_textfield',
),
'display' => array(
'logged_item_type_display' => array(
'label' => $t('Type of log entry'),
'type' => 'text',
),
),
),
// Add bug field instance and settings.,
'bug_status_instance' => array(
'field_name' => 'genima_bug_status',
'type' => 'text',
'label' => $t('Status of bug'),
'widget' => array(
'type' => 'text_textfield',
),
'display' => array(
'bug_status_display' => array(
'label' => $t('Status of bug'),
'type' => 'text',
),
),
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment