/[drupal]/contributions/modules/auto_nodetitle/auto_nodetitle.module
ViewVC logotype

Diff of /contributions/modules/auto_nodetitle/auto_nodetitle.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.4.2.20, Thu Jan 8 11:28:42 2009 UTC revision 1.4.2.21, Tue Feb 10 10:31:34 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: auto_nodetitle.module,v 1.4.2.19 2008/12/30 20:28:45 fago Exp $  // $Id: auto_nodetitle.module,v 1.4.2.20 2009/01/08 11:28:42 fago Exp $
3    
4  /**  /**
5   * @file   * @file
# Line 47  function auto_nodetitle_form_alter($form Line 47  function auto_nodetitle_form_alter($form
47   * To let node previews work this is also done during node validation.   * To let node previews work this is also done during node validation.
48   */   */
49  function auto_nodetitle_nodeapi(&$node, $op, $form = NULL, $a4 = NULL) {  function auto_nodetitle_nodeapi(&$node, $op, $form = NULL, $a4 = NULL) {
50    if ($op == 'validate' && $form['#post']['op'] == t('Preview')) {    if ($op == 'validate' && $form['#post']['op'] == t('Preview') && auto_nodetitle_is_needed($node)) {
51      if (($setting = auto_nodetitle_get_setting($node->type)) && !($setting == AUTO_NODETITLE_OPTIONAL && !empty($node->title))) {      // As changes to $node are not persistent during validation, we use form_set_value()!
52        // As changes to $node are not persistent during validation, we use form_set_value()!      auto_nodetitle_set_title($node);
53        auto_nodetitle_set_title($node);      form_set_value($form['title'], $node->title);
       form_set_value($form['title'], $node->title);  
     }  
54    }    }
55    else if ($op == 'submit' && ($setting = auto_nodetitle_get_setting($node->type)) && !($setting == AUTO_NODETITLE_OPTIONAL && !empty($node->title))) {    else if ($op == 'submit' && auto_nodetitle_is_needed($node)) {
56      auto_nodetitle_set_title($node);      auto_nodetitle_set_title($node);
57    }    }
58  }  }
59    
60    /**
61     * Returns whether the auto nodetitle has to be set.
62     */
63    function auto_nodetitle_is_needed($node) {
64      return empty($node->auto_nodetitle_applied) && ($setting = auto_nodetitle_get_setting($node->type)) && !($setting == AUTO_NODETITLE_OPTIONAL && !empty($node->title));
65    }
66    
67  /*  /*
68   * Sets the automatically generated nodetitle for the node   * Sets the automatically generated nodetitle for the node
69   */   */
# Line 75  function auto_nodetitle_set_title(&$node Line 80  function auto_nodetitle_set_title(&$node
80    else {    else {
81      $node->title = t('@type', array('@type' => $types[$node->type]->name));      $node->title = t('@type', array('@type' => $types[$node->type]->name));
82    }    }
83      // With that flag we ensure we don't apply the title two times to the same node.
84      $node->auto_nodetitle_applied = TRUE;
85  }  }
86    
87  /**  /**
# Line 83  function auto_nodetitle_set_title(&$node Line 90  function auto_nodetitle_set_title(&$node
90  function auto_nodetitle_node_operations() {  function auto_nodetitle_node_operations() {
91    $operations = array(    $operations = array(
92      'nodetitle_update' => array(      'nodetitle_update' => array(
93        'label' => t('Update automatic Nodetitles'),        'label' => t('Update automatic nodetitles'),
94        'callback' => 'auto_nodetitle_operations_update',        'callback' => 'auto_nodetitle_operations_update',
95      ),      ),
96    );    );
# Line 96  function auto_nodetitle_node_operations( Line 103  function auto_nodetitle_node_operations(
103  function auto_nodetitle_operations_update($nodes) {  function auto_nodetitle_operations_update($nodes) {
104    foreach ($nodes as $nid) {    foreach ($nodes as $nid) {
105      $node = node_load($nid);      $node = node_load($nid);
106      $nodetitle_save = $node->title;      if ($node && auto_nodetitle_is_needed($node)) {
107      auto_nodetitle_set_title($node);        $previous_title = $node->title;
108      if ($node->title != $nodetitle_save) {        auto_nodetitle_set_title($node);
109        //only save if the title has actually changed        // Only save if the title has actually changed.
110        node_save($node);        if ($node->title != $previous_title) {
111            node_save($node);
112          }
113      }      }
114    }    }
115  }  }

Legend:
Removed from v.1.4.2.20  
changed lines
  Added in v.1.4.2.21

  ViewVC Help
Powered by ViewVC 1.1.2