Changing a form function to block_configure function

2 replies [Last post]
bill
Offline
Last seen: 7 years 7 weeks ago
Joined: 2011-06-04

I have a form function today:

function content_form ($form, &$form_submit) {
}

That I use to allow administrators to customize content and settings for an online tool. Once changes have been made to the form, they can be saved, which I do today using a form submit function using db_update and the fields method for assigning values. As you can see the values are assigned based on passing $form_state into the function.

function content_form_submit ($form, &$form_state) {

    $price_update = db_update ('fielddata')
        ->fields (array(
                'title' => $form_state['values']['price_title'],
                'desc' => $form_state['values']['price_description'],
                'value' => $form_state['values']['price_value'],
                'minvalue' => $form_state['values']['price_minvalue'],
                'maxvalue' => $form_state['values']['price_maxvalue'],
            ))
        ->condition('fieldid', 'price')
        ->execute();
}

These two functions work perfectly well as implemented and the data gets saved to the proper table correctly.

Now, I'd like to convert these two functions to block_configuration and block_save functions, respectively so that they can be accessed in the block configuration area. In reviewing the examples module, it looks like the correct functions are as follows:

function inputs_block_configure($delta = '') {
   $form = array();
   if ($delta == 'inputs') {
   }
}

function inputs_block_save($delta = '', $edit = array()) {
    if ($delta == 'inputs') {
    }
}

When I've implemented this in my code, wrapping the function around the previously working code, the block_configure function works as it is supposed to. The form is accessible when accessing the configuration settings for the block and retrieves settings data from the database as it should. What is not working is the block_save function.

As I know that I was pulling data from $form_state previously and my existing code references it, I'm pretty sure I need to find the proper way to pass that information into the block_save function. But I'm not sure how to do that.

I'd appreciate any help from anybody that has accomplished such a conversion or who has experience working with the block_configure and block_save hooks.

Thanks!

obrignoni
Offline
Last seen: 6 years 47 weeks ago
Joined: 2011-02-28
I think in the case of hook_block_save()

I think in the case of hook_block_save() that the $edit argument is equivalent to $form_state['values'].

So probably the values are being passed correctly. You just need to reference each field in the form by key using $edit (instead of $form_state['values']) like this...

I didn't test this so try to see if it works.

function inputs_block_save($delta = '', $edit = array()) {
    if ($delta == 'inputs') {
      $price_update = db_update ('fielddata')
        ->fields (array(
                'title' => $edit['price_title'],
                'desc' => $edit['price_description'],
                'value' => $edit['price_value'],
                'minvalue' => $edit['price_minvalue'],
                'maxvalue' => $edit['price_maxvalue'],
            ))
        ->condition('fieldid', 'price')
        ->execute();
    }
}

bill
Offline
Last seen: 7 years 7 weeks ago
Joined: 2011-06-04
Thanks for the suggestion

Thanks for the suggestion obrignoni. Unfortunately, that didn't work. However, it also seems like the function is not being called as I put debug($edit) as the first line after the block_save function call and it never displayed. Nor did the drupal_set_message I inserted in the function. I'm going to try rebuilding this from the code provided in the examples module to see if I can't get it working. I can see that they use $edit as you suggested in the save statement, so that seems like the correct approach here.

Thanks again.

Add to playlist

Add to , or

Add to new playlist:

Add to playlist
This is a member-only feature
But, it's easy to become a member

Add to cart:


Loading...
Contact us

Below are the supported keyboard shortcuts for our video player.

space
Start / stop video
shift
Go to next video
shift
Go to previous video
Increase or decrease player speed by 20%
Skip ahead or go back by 15 seconds
1...9
Jump to a percentage (2 is 20%, for example)
f
Toggle full screen
Videos Pricing More Search
BuildAModule Logo Icon BuildAModule Logo Icon
Become a member!
$29
1 month
$156
6 months
10% off
$276
12 months
20% off
All memberships come with the following:
24 / 7 access to the entire 1800+ video library
An innovative video player with a rolling transcript
Extensive Drupal and cross-platform training
A thorough and thoughtful training style
Progress tracking
Take notes on specific video sections
Intuitive multi-user management
How many users?
user
Go solo with a single user, or give multiple team members access through one account and get discounts by bundling multiple users into a single membership.