SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Drupal 7.x and Appending to node title before DB insert?

    I have 2 content types and when nodes get created from both of these, they can sometimes have the same name (title). This causes problems of distinction when referencing nodes from fields that can reference both types.

    What I would like to do is append to the title the name of the content type in parenthesis whenever a node gets created (NOT EDITED) from one of these content types.

    So for example, say we have content types "Foobar A" and "Foobar B". Now let's assume that we create a node with the title of "Blah" from both content types. Now whenever a node is referenced from a reference field that allows referencing from both content types, you'll see "Blah" come down twice in the drop-down. Instead, I would like to see something like "Blah (Foobar A)" and "Blah (Foobar B)" as options.

    So, long story short, how do I manipulate the node title value before it's submitted to the database IF and ONLY IF the originating node is being CREATED and NOT EDITED? What API functions will I need to achieve this?

  2. #2
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Never mind. I figured it out...

    Here's how I did it for Drupal 7.x: Inside template.php, I added the following code:

    Code:
        function <template name>_form_alter(&$form, &$form_state, $form_id){
            if($form_id == '<content type>_node_form'){
                $form['#submit'][]="page_submit";//Name of the submit callback function, which is needed to manipulate the data before being inserted into the database ex post facto form submit.
            }
        }
    
        //Submit callback function--this is where the data is changed!
        function page_submit(&$form, &$form_state){
            //If the title of the submitted page doesn't have ' (FOOBAR!) at the end, add it...
            if(substr($form_state['values']['title'],-10,10) !== ' (FOOBAR!)'){
                $form_state['values']['title'] = $form_state['values']['title'].' (FOOBAR!)';
            }
        }
    I added all this into template.php much to the chagrin of all the IIRC advice I received. My rationale for doing this is that it keeps my code in one spot more so than it would had I followed their advice and placed it all into a separate module. To each, their own, right? It just makes it easier to mess with IMO and since the change here isn't very big, I don't believe there's much to worry about with state separation.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,028
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Yeah, to each their own... I tend to work within template.php often so that my modules directory isn't cluttered with tiny helper modules but I can see the argument to use hook_form_alter in a module too.

    I suspect the dev's who were unsupportive of your decision were thinking that you'd incur less overhead by stuffing it in a module so that it gets processed with the business logic rather than during presentation rendering. For me it depends on the site and how hard the site has to work.
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •