You are here

function _comment_alter_submit_node_fields in Comment Alter 7

Submit callback for the altered comment form.

Determines which fields have actually changed, then calls form_attach_submit() on each of them, and saves the resulting node.

See also

form_attach_submit()

comment_alter_form_comment_form_alter()

1 string reference to '_comment_alter_submit_node_fields'
comment_alter_form_comment_form_alter in ./comment_alter.module
Implements hook_form_BASE_FORM_ID_alter().

File

./comment_alter.module, line 344
Provides UI to alter nodes' parameters from comment forms.

Code

function _comment_alter_submit_node_fields($form, &$form_state) {
  $values = $form_state['values'];

  // Do not try to save anything if there is nothing that was allowed to be
  // changed from the comment form.
  if (isset($values['comment_alter'])) {
    $changed_fields = array();
    foreach ($values['comment_alter']['fields'] as $field_name) {
      _comment_alter_cleanup_field_values($values, $field_name);
      _comment_alter_cleanup_field_values($values, $field_name, '_old');

      // If field values have changed, add it to the list.
      if ($values[$field_name . '_old'] != $values[$field_name]) {
        $changed_fields[$field_name] = $field_name;
      }
    }
    if (!empty($changed_fields)) {
      $node = $form_state['node'];

      // Run field_attach_submit for all the changed fields.
      foreach ($form_state['values']['comment_alter']['fields'] as $field_name) {
        field_attach_submit('node', $node, $form, $form_state, array(
          'field_name' => $field_name,
        ));
      }

      // Special support for the Title field via the Title module.
      if (module_exists('title') && isset($form_state['values']['comment_alter']['fields']['title_field'])) {

        // Calling this should be enough:
        //   title_entity_sync('node', $node, $node->language);
        // But it isn't. That function uses a static variable to make sure it is
        // only run once per request, however, under certain conditions observed
        // when using Panels, title_entity_sync() can be called several times
        // before ever reaching this point. So, the following code was copied
        // from title_entity_sync() to avoid the static variable.
        $fr_info = title_field_replacement_info('node');
        if ($fr_info) {
          foreach ($fr_info as $legacy_field => $info) {
            if (title_field_replacement_enabled('node', $node->type, $legacy_field)) {
              title_field_sync_get('node', $node, $legacy_field, $info, $node->language);
            }
          }
        }
      }

      // Creating a new node revision regardless the node type settings.
      $node->revision = TRUE;

      // Disable node updated notifications that caused by comment_alter. New
      // comment notifications should be enough - this happens when submitting a
      // comment, not when editing a node, after all. This disabling is
      // unconditional: there may be other modules than notifications_content
      // that wants these notifications be disabled; reusing the same key seems
      // to be a non-issue here as it won't get stored anywhere.
      $node->notifications_content_disable = TRUE;
      node_save($node);

      // Fire the comment_alter_node_postsave hook with the $node and $comment
      // objects; though $node does not have a $node->original property, all the
      // affected fields' information (both previous and current values) are
      // available in the comment object.
      $node->original = node_load($node->nid, $values['comment_alter']['old_vid']);
      module_invoke_all('comment_alter_node_postsave', $node, $form_state['comment']);
      $comment_alter = array(
        'old_vid' => $values['comment_alter']['old_vid'],
        'new_vid' => $node->vid,
        'cid' => $values['cid'],
      );
      drupal_write_record('comment_alter', $comment_alter);
    }
  }
}