Skip to content

Instantly share code, notes, and snippets.

@andypost
Last active December 13, 2015 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andypost/4945004 to your computer and use it in GitHub Desktop.
Save andypost/4945004 to your computer and use it in GitHub Desktop.
comment_update_8006
<?php
/**
* Adds comment fields for all node types.
*
* Field instance settings "comment_default_mode", "comment_default_per_page"
* and "comment_form_location" are preserved to allow migrate contrib modules.
*/
function comment_update_8006(&$sandbox) {
// Loop over defined node_types.
$node_types = array_keys(_update_7000_node_get_types());
foreach ($node_types as $node_type) {
// COMMENT_OPEN
$default_value = variable_get('comment_' . $node_type, 2);
// Add a default comment field for existing node comments.
$field = array(
'cardinality' => '1',
// We need one per content type to match the existing bundles.
'field_name' => 'comment_node_' . $node_type,
'module' => 'comment',
'settings' => array(),
'translatable' => '0',
'type' => 'comment',
);
// Make sure field doesn't already exist.
if (!_update_7000_field_read_fields(array('field_name' => $field['field_name']))) {
// Create the field.
_update_7000_field_create_field($field);
}
// Add the comment field, setting the instance settings to match those for
// the give node_type.
$instance_settings = array(
// COMMENT_MODE_THREADED
'comment_default_mode' => variable_get('comment_default_mode_' . $node_type, 1),
'comment_default_per_page' => variable_get('comment_default_per_page_' . $node_type, 50),
// COMMENT_FORM_BELOW
'comment_form_location' => variable_get('comment_form_location' . $node_type, 1),
// COMMENT_ANONYMOUS_MAYNOT_CONTACT
'comment_anonymous' => variable_get('comment_anonymous_' . $node_type, 0),
'comment_subject_field' => variable_get('comment_subject_field_' . $node_type, 1),
// DRUPAL_OPTIONAL
'comment_preview' => variable_get('comment_preview_' . $node_type, 1),
);
$instance = array(
'bundle' => $node_type,
'default_value' => array(array('comment' => $default_value)),
'deleted' => '0',
'description' => '',
'entity_type' => 'node',
'field_name' => $field['field_name'],
'label' => 'Comment settings',
'required' => 1,
'settings' => array(
'comment' => $instance_settings,
),
'widget' => array(
'active' => 0,
'module' => 'comment',
'settings' => array(),
'type' => 'comment_default',
'weight' => '50',
),
);
_update_7000_field_create_instance($field, $instance);
// Prepare defaults for the default and full view modes.
$formatter_settings = array(
'default_mode' => $instance_settings['comment_default_mode'],
'per_page' => $instance_settings['comment_default_per_page'],
'form_location' => $instance_settings['comment_form_location'],
);
$display_options_default = array(
'label' => 'hidden',
'type' => 'comment_default',
'weight' => 1,
'settings' => $formatter_settings,
);
entity_get_display('node', $node_type, 'default')
->setComponent($field['field_name'], $display_options_default)
->save();
entity_get_display('node', $node_type, 'full')
->setComponent($field['field_name'], $display_options_default)
->save();
// Update all other entity displays with custom settings.
$display_options = array(
'type' => 'hidden',
'label' => 'hidden',
);
foreach (entity_get_view_modes('node') as $view_mode => $view_mode_settings) {
if ($view_mode != 'full') {
entity_get_display('node', $node_type, $view_mode)
->setComponent($field['field_name'], $display_options)
->save();
}
}
// Clean up old variables.
variable_del('comment_' . $node_type);
variable_del('comment_default_mode_' . $node_type);
variable_del('comment_default_per_page_' . $node_type);
variable_del('comment_anonymous_' . $node_type);
variable_del('comment_subject_field_' . $node_type);
variable_del('comment_form_location_' . $node_type);
variable_del('comment_preview_' . $node_type);
}
$t = get_t();
return $t('Created required fields for each comment bundle');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment