diff --git a/smileys.admin.inc b/smileys.admin.inc index 4ac12aa..b640997 100644 --- a/smileys.admin.inc +++ b/smileys.admin.inc @@ -50,6 +50,20 @@ function smileys_admin_settings() { '#default_value' => variable_get('smileys_node_types_content', array()), '#options' => node_get_types('names'), ); + $additional_forms = module_invoke_all('smileys_get_additional_forms'); + if (!empty($additional_forms)) { + $options = array(); + foreach ($additional_forms as $key => $value) { + $options[$key] = $value['name']; + } + $form['smileys_selectbox']['smileys_additional_forms'] = array( + '#type' => 'checkboxes', + '#title' => t('Enable smileys also on the following forms'), + '#description' => t('Select the node types you want to enable Smileys select-box for.'), + '#default_value' => variable_get('smileys_additional_forms', array()), + '#options' => $options, + ); + } $form['smileys_selectbox']['smileys_select_box_expanded'] = array( '#type' => 'checkbox', '#title' => t('Expand select-box fieldset by default'), @@ -183,7 +197,7 @@ function smileys_admin_list() { foreach ($packages as $package) { drupal_add_tabledrag('smileys_admin_list', 'match', 'sibling', 'smiley-category-select', 'smiley-category-' . str_replace(array('_', ' '), '-', drupal_strtolower($package)), NULL, FALSE); - drupal_add_tabledrag('smileys_admin_list', 'order', 'sibling', 'smiley-weight'); + drupal_add_tabledrag('smileys_admin_list', 'order', 'sibling', 'smiley-weight'); } if (count($rows) == 0) { diff --git a/smileys.module b/smileys.module index ad78ee9..5f51ef3 100644 --- a/smileys.module +++ b/smileys.module @@ -63,59 +63,108 @@ function smileys_block($op = 'list', $delta = 0) { } /** - * Implementation of hook_form_alter(). + * Implements hook_smileys_get_additional_forms. + * + * Returns form-ids (and field names) to which we want to add smileys. + * + * form_id => field_name + * + * @return void + * @author Lars + */ +function smileys_smileys_get_additional_forms() { + $additional_form = array(); + + if (module_exists('privatemsg')) { + $additional_form['privatemsg_new'] = array('field' => 'privatemsg', 'name' => t('Create New Private Message')); + } + + return $additional_form; +} + +/** + * Implements hook_form_alter(). */ function smileys_form_alter(&$form, $form_state, $form_id) { - if ($form_id != 'comment_form' && !isset($form['#node'])) { + + // Check Permissions + if (!user_access('use smiley select box')) { return; } - + + // Invoke hooks hook_smileys_get_additional_forms to get all form_id's and + // field_keys for forms with smileys support. + $additional_form_ids = module_invoke_all('smileys_get_additional_forms'); + + // Don't check anything if form is not node/comment/additional form. + $type = ''; if ($form_id == 'comment_form') { - $node_type = db_result(db_query('SELECT type FROM {node} WHERE nid=%d', $form['nid']['#value'])); + $type = 'comment'; } - else if (!empty($form['type']['#value'])) { - $node_type = $form['type']['#value']; + else if (substr($form_id, -10) == '_node_form') { + $type = 'node'; + } + else if(isset($additional_form_ids[$form_id])) { + $type = 'additional'; + } + else { + return; } - if (in_array($node_type, variable_get('smileys_node_types_content', array()), TRUE)) { - if (user_access('use smiley select box') && - ((isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) && - variable_get('smileys_enable_for_nodes', 0) && - isset($form['body_field']) || - ('comment_form' == $form_id && variable_get('smileys_enable_for_comments', 0)))) { - - $output = ''; - if (array_key_exists('body_field', $form)) { - $key = 'body_field'; + // If none field key will be found in the form, add a + // smileys_wrapper key (default; usually at the bottom of the form). + $field_key = 'smileys_wrapper'; + switch ($type) { + case 'node': + // Check if smileys are allowed for node forms and if body field is set. + if (!variable_get('smileys_enable_for_nodes', 0) || !isset($form['body_field'])) { + return; } - elseif (array_key_exists('comment_filter', $form)) { - $key = 'comment_filter'; + // Check if smileys are enabled for this node type. + if (empty($form['type']['#value']) || !in_array($form['type']['#value'], variable_get('smileys_node_types_content', array()), TRUE)) { + return; } - else { - $key = 'smileys_wrapper'; + $field_key = 'body_field'; + break; + case 'comment': + // Check if smileys are allowed for comment forms + if (!variable_get('smileys_enable_for_comments', 0)) { + return; } - - $collapsed = variable_get('smileys_select_box_expanded', TRUE) ? FALSE : TRUE; - $form[$key]['smileys'] = array( - '#type' => 'fieldset', - '#title' => t('Smileys'), - '#collapsible' => TRUE, - '#collapsed' => $collapsed, - '#weight' => 0, - '#prefix' => '