Skip to:
Content

BuddyPress.org

Opened 10 years ago

Closed 10 years ago

#6272 closed enhancement (fixed)

Add new hook admin_new_field_html_additional_options to allows plugin authors to add options to multi fields without the need to replicate the code

Reported by: svenl77's profile svenl77 Owned by: boonebgorges's profile boonebgorges
Milestone: 2.3 Priority: normal
Severity: normal Version: 2.2.1
Component: Extended Profile Keywords: needs-patch
Cc:

Description

I would like to add one new hook.

I need to add additional options to the multi select field. In my case minimumSelectionSize and maximumSelectionSize options.

That was not easy done because I need to replicate the admin_new_field_html and add my options under the sort_order select.

<p>
    <label for="sort_order_<?php echo esc_attr( $type ); ?>"><?php esc_html_e( 'Sort Order:', 'buddypress' ); ?></label>
    <select name="sort_order_<?php echo esc_attr( $type ); ?>" id="sort_order_<?php echo esc_attr( $type ); ?>" >
        <option value="custom" <?php selected( 'custom', $current_field->order_by ); ?>><?php esc_html_e( 'Custom',     'buddypress' ); ?></option>
        <option value="asc"    <?php selected( 'asc',    $current_field->order_by ); ?>><?php esc_html_e( 'Ascending',  'buddypress' ); ?></option>
        <option value="desc"   <?php selected( 'desc',   $current_field->order_by ); ?>><?php esc_html_e( 'Descending', 'buddypress' ); ?></option>
    </select>
</p>

I can already save new options with "xprofile_field_after_save", but I can not add additional options to the multi field.

This new hook solves exactly this issue. I hope you can add it to the next version.

Attachments (2)

buddypress.patch (575 bytes) - added by svenl77 10 years ago.
Add options to multi fields without the need to replicate the code
bp_admin_new_field_html_additional_settings.patch (658 bytes) - added by svenl77 10 years ago.
hook bp_admin_new_field_html_additional_settings

Download all attachments as: .zip

Change History (9)

@svenl77
10 years ago

Add options to multi fields without the need to replicate the code

#1 @boonebgorges
10 years ago

  • Keywords reporter-feedback added

Hi sven77 - Could you give a more concrete example of how this hook would be used? Are you planning to modify the $current_field object passed by reference to the action? What about the 'bp_xprofile_field_get_children' filter that already exists?

#2 @svenl77
10 years ago

Hi boonebgorges,

If I understand it correctly, 'bp_xprofile_field_get_children' is perfect to get/manipulate the multiselectbox children.

The multiselectbox options in this case.

<select>
<option></option> == children
</select>


But I do not want to add new options to a select box, I want to add general options to the multi field type to add some js depend on this options. Not to display this options. The children are the user choices. I want options to to add attributes.

The multiselectbox and all the children should work like normal.

What I try to do is to create a multiselectbox and limit the selection a user can make to a number x.

https://dl-web.dropbox.com/get/Screenshots/xprofile2.jpeg

https://photos-6.dropbox.com/t/2/AACNpU0VggbcnDXhYpUy9p7FXaeZojkmZmeFc0g3AJkxBw/12/16188696/jpeg/1024x768/3/1425398400/0/2/xprofile.jpeg/CJiK3AcgASACIAMoASgC/_vaRFsH7MtXRtYA4tW78M7au5ig1joJS2FlxgCNIATM

I did it with some lines of code, but needed to replicate the multiselectbox because of the missing hook.

$xprofile_select2_maximum_selection_size = bp_xprofile_get_meta( $current_field->id, 'field', 'xprofile_select2_maximum_selection_size' );

wp_nonce_field( 'xprofile-select2-edit-action', 'xprofile-select2-edit-action' );


and add the form elements for the option under the sort order.

<p>
    <label for="maximum-selection-size"><?php esc_html_e( 'Maximum Selection Size:', 'buddypress' ); ?></label>
    <input type='number' name='xprofile-select2-maximum-selection-size' id='xprofile-select2-maximum-selection-size' class='xprofile-select2-text' value ='<?php echo $xprofile_select2_maximum_selection_size ?>' />
</p>

I have added the complete class to Github so it is more easy to understand. We talk about the public function admin_new_field_html on line 189.

https://github.com/Themekraft/BuddyPress-xProfile-Select2/blob/master/includes/field-types/multiselectbox2.php

I hope this makes it more clear and understandable. Sorry for my poor english. Next live I will be a native speaker ;)

Many thanks,
Sven


#3 @svenl77
10 years ago

  • Keywords reporter-feedback removed

#4 @boonebgorges
10 years ago

  • Component changed from API to Component - XProfile
  • Keywords needs-patch added; has-patch removed
  • Milestone changed from Awaiting Review to 2.3

Ah, thanks for the additional explanation, svenl77. I think the word "options" (and the hook name _additional_options) was throwing me off - I was associating "options" with the field children.

I don't have a problem with an action here, but here are some suggestions for improvement - let me know what you think.

  • Call it 'bp_admin_new_field_html_additional_settings'
  • Put it at the very end of div#inside - right after the "Add Another Option" button

@svenl77
10 years ago

hook bp_admin_new_field_html_additional_settings

#5 @svenl77
10 years ago

ohh I see my images got lost. Thanks for the fast feedback boonebgorges.

#6 @boonebgorges
10 years ago

Thanks, svenl77! I'm going to make another small tweak to the action name, for greater consistency with other similar actions.

#7 @boonebgorges
10 years ago

  • Owner set to boonebgorges
  • Resolution set to fixed
  • Status changed from new to closed

In 9588:

Introduced 'bp_xprofile_admin_new_field_additional_settings' action.

This allows devs to inject additional settings into the field creation process,
without having to override the entire admin_new_field_html() method.

Props svenl77.
Fixes #6272.

Note: See TracTickets for help on using tickets.