[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Drupal question - using drupal_execute for multivalue fields?

Posted on 2011-02-22
2
Medium Priority
?
489 Views
Last Modified: 2012-05-11
I'm using drupal_execute to save a node programmatically, and for the most part, it works fine, except when it comes to a multi-value field.  

What gets posted is this (I'm just including the portion that isn't working):

 
    [alt] => Array
            (
                [0] => Array
                    (
                        [name] => Sam I. Am
                        [phone] => (650) 5553131
                    )
    
                [1] => Array
                    (
                        [name] => The Lorax
                        [phone] => 6505553344
                    )
    
                [2] => Array
                    (
                        [name] => 
                        [phone] => 
                    )
    
            )

Open in new window

When I'm setting the $form_state['values'], I'm using:
    for($a = 0; $a < count($_REQUEST['alt']); $a++) {
    		$form_state['values']['field_alternativename'][$a]['value'] = check_plain($_REQUEST['alt'][$a]['name']);
    		$form_state['values']['field_alternativephone'][$a]['value'] = format_phone($_REQUEST['alt'][$a]['phone']);
    	}

Open in new window

And to save the node:

    drupal_execute('info_node_form', $form_state, $node);

Open in new window


As a test, to make sure that I'm referencing the appropriate fields, I edited an existing node using node/X/edit and printed out the $form_state['values'] upon submission.  This is what it printed out:
    //output of print '<pre>'; print_r($form_state['values']); print '</pre>';
        [field_alternativename] => Array
            (
                [0] => Array
                    (
                        [value] => Sam I. Am
                        [_error_element] => group_alternative_contacts][0][field_alternativename][value
                        [_weight] => 0
                        [_remove] => 0
                    )
    
                [1] => Array
                    (
                        [value] => The Lorax
                        [_error_element] => group_alternative_contacts][1][field_alternativename][value
                        [_weight] => 1
                        [_remove] => 0
                    )
    
            )
    
        [field_alternativephone] => Array
            (
                [0] => Array
                    (
                        [value] => (650) 5553131
                        [_error_element] => group_alternative_contacts][0][field_alternativephone][value
                        [_weight] => 0
                        [_remove] => 0
                    )
    
                [1] => Array
                    (
                        [value] => (650) 5553344
                        [_error_element] => group_alternative_contacts][1][field_alternativephone][value
                        [_weight] => 1
                        [_remove] => 0
                    )
    
            )

Open in new window

So, I'm not understanding why it isn't being saved... I'm not setting the delta, but I didn't think I'd have to?  In mysql, the data is stored as:

    mysql> select * from content_field_alternativename ;
    +-------+-------+-------+-----------------------------+
    | vid   | nid   | delta | field_alternativename_value |
    +-------+-------+-------+-----------------------------+
    | 22433 | 22433 |     0 | Sam I. Am                   |
    +-------+-------+-------+-----------------------------+
    
    mysql> select * from content_field_alternativephone;
    +-------+-------+-------+------------------------------+
    | vid   | nid   | delta | field_alternativephone_value |
    +-------+-------+-------+------------------------------+
    | 22433 | 22433 |     0 | (650) 5553131                |
    +-------+-------+-------+------------------------------+

Open in new window

0
Comment
Question by:n00b0101
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 17

Accepted Solution

by:
Dushan De Silva earned 2000 total points
ID: 34958030
Hi n00b0101,
I'm actually not a Drupal guy. Sometimes you may need to change the array index value.
   
       for($a = 0; $a < count($_REQUEST['alt']); $a++) {
                $form_state['values']['field_alternativename'][$a]['name'] = check_plain($_REQUEST['alt'][$a]['name']);
                $form_state['values']['field_alternativephone'][$a]['phone'] = format_phone($_REQUEST['alt'][$a]['phone']);
          }
0
 

Author Closing Comment

by:n00b0101
ID: 35380606
Not the solution, but thanks for trying.
0

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question