diff --git a/sharethis.module b/sharethis.module
index b7e3ff7..6f5b45b 100644
--- a/sharethis.module
+++ b/sharethis.module
@@ -83,8 +83,7 @@ function sharethis_configuration_form($form, &$form_state) {
// Create the variables for teasers.
// Get a list of content types and view modes
- $view_modes = sharethis_get_view_mode_info();
- $view_modes_selected = $current_options_array['viewModes'];
+ $view_modes_selected = $current_options_array['view_modes'];
$form = array();
$form['options'] = array(
@@ -148,22 +147,7 @@ function sharethis_configuration_form($form, &$form_state) {
'#group' => 'additional_settings',
'#description' => t('Configure where the ShareThis widget should appear.'),
);
- foreach($view_modes as $k => $v){
- $form['context']['sharethis_' .$k .'_options'] = array(
- '#title' => t($v['label']. ' View Modes'),
- '#description' => t('Select which view modes the ShareThis widget should appear on for ' . $v['label'] . ' nodes.'),
- '#type' => 'checkboxes',
- '#options' => $v['modes'],
- '#default_value' => $view_modes_selected[$k],
- );
- }
- $form['context']['sharethis_comments'] = array(
- '#title' => t('Comments'),
- '#type' => 'checkbox',
- '#default_value' => variable_get('sharethis_comments', FALSE),
- '#description' => t('Display ShareThis on comments.'),
- '#access' => module_exists('comment'),
- );
+
$form['context']['sharethis_location'] = array(
'#title' => t('Location'),
'#type' => 'radios',
@@ -173,7 +157,42 @@ function sharethis_configuration_form($form, &$form_state) {
'links' => t('Links area'),
),
'#default_value' => variable_get('sharethis_location', 'content'),
- '#description' => t('Choose where the ShareThis widget should appear. When selected to display as a block, you must choose which region to display the ShareThis block in from the Blocks administration.', array('@blocksadmin' => url('admin/structure/block'))),
+ );
+
+ // Add an information section for each location type, each dependent on the
+ // currently selected location.
+ foreach (array('links', 'content', 'block') as $location_type) {
+ $form['context'][$location_type]['#type'] = 'container';
+ $form['context'][$location_type]['#states']['visible'][':input[name="sharethis_location"]'] = array('value' => $location_type);
+ }
+
+ // Add help text for the 'content' location.
+ $form['context']['content']['#children'] = t('You must place the ShareThis links in the "Manage Display" section of each content type.');
+ // Add help text for the 'block' location.
+ $form['context']['block']['#children'] = t('You must choose which region to display the ShareThis block in from the Blocks administration.', array('@blocksadmin' => url('admin/structure/block')));
+
+ // Add checkboxes for each view mode of each bundle.
+ $entity_info = entity_get_info('node');
+ $modes = array();
+ foreach ($entity_info['view modes'] as $mode => $mode_info) {
+ $modes[$mode] = $mode_info['label'];
+ }
+ foreach ($entity_info['bundles'] as $bundle => $bundle_info) {
+ $form['context']['links']['sharethis_' . $bundle . '_options'] = array(
+ '#title' => t('%label View Modes', array('%label' => $bundle_info['label'])),
+ '#description' => t('Select which view modes the ShareThis widget should appear on for %label nodes.', array('%label' => $bundle_info['label'])),
+ '#type' => 'checkboxes',
+ '#options' => $modes,
+ '#default_value' => $view_modes_selected[$bundle],
+ );
+ }
+
+ $form['context']['sharethis_comments'] = array(
+ '#title' => t('Comments'),
+ '#type' => 'checkbox',
+ '#default_value' => variable_get('sharethis_comments', FALSE),
+ '#description' => t('Display ShareThis on comments.'),
+ '#access' => module_exists('comment'),
);
$form['context']['sharethis_weight'] = array(
'#title' => t('Weight'),
@@ -225,29 +244,12 @@ function sharethis_configuration_form($form, &$form_state) {
'#description' => t('If disabled, the ShareThis widget will be displayed on click instead of hover.'),
'#default_value' => variable_get('sharethis_option_onhover', TRUE),
);
+ $form['#submit'][] = 'sharethis_configuration_form_submit';
return system_settings_form($form);
}
/**
- * Retrieves an array containing each content type mapped to display modes.
- */
-function sharethis_get_view_mode_info(){
- $options = array();
- // Get full entity information, allowing all hooks including display suite to fire
- $info = entity_get_info('node');
- // Clean up the array for use in the settings form
- foreach($info['bundles'] as $bundle => $data){
- $options[$bundle] = array();
- foreach($info['view modes'] as $mode => $values){
- $options[$bundle]['label'] = $data['label'];
- $options[$bundle]['modes'][$mode] = $values['label'];
- }
- }
- return $options;
-}
-
-/**
- * Form submission handler for sharethis_configuration_form().
+ * Form validation handler for sharethis_configuration_form().
*/
function sharethis_configuration_form_validate($form, &$form_state) {
//Additional filters for the service option input
@@ -271,6 +273,18 @@ function sharethis_configuration_form_validate($form, &$form_state) {
$form_state['values']['sharethis_twitter_recommends'] = (isset($form_state['values']['sharethis_twitter_recommends'])) ? $form_state['values']['sharethis_twitter_recommends'] : '';
}
+/**
+ * Form submission handler for sharethis_configuration_form().
+ */
+function sharethis_configuration_form_submit($form, &$form_state) {
+ // If the location is changing to/from 'content', clear the Field Info cache.
+ $current_location = variable_get('sharethis_location', 'content');
+ $new_location = $form_state['values']['sharethis_location'];
+ if (($current_location == 'content' || $new_location == 'content') && $current_location != $new_location) {
+ field_info_cache_clear();
+ }
+}
+
/**
* Implements hook_menu().
*
@@ -369,6 +383,32 @@ function sharethis_node_view($node, $view_mode, $langcode) {
}
/**
+ * Implements hook_field_extra_fields().
+ */
+function sharethis_field_extra_fields() {
+ $extra = array();
+ // Only add extra fields if the location is the node content.
+ if (variable_get('sharethis_location', 'content') == 'content') {
+ $options = sharethis_get_options_array();
+ foreach ($options['view_modes'] as $bundle => $view_modes) {
+ // Only add for a bundle if it has at least one view mode specified.
+ // hook_field_extra_fields() does not support restriction per view mode.
+ if ((bool) array_filter($view_modes)) {
+ $extra['node'][$bundle]['display'] = array(
+ 'sharethis' => array(
+ 'label' => t('ShareThis'),
+ 'description' => t('ShareThis links'),
+ 'weight' => 0,
+ 'weight' => intval(variable_get('sharethis_weight', 10)),
+ ),
+ );
+ }
+ }
+ }
+ return $extra;
+}
+
+/**
* sharethisGetOptionArray is a helper function for DB access.
*
* Returns options that have been stored in the database.
@@ -391,7 +431,7 @@ function sharethis_get_options_array() {
'twitter_handle' => variable_get('sharethis_twitter_handle', ''),
'twitter_recommends' => variable_get('sharethis_twitter_recommends', ''),
'late_load' => variable_get('sharethis_late_load', FALSE),
- 'viewModes' => $view_modes,
+ 'view_modes' => $view_modes,
);
}