Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hook into dropdown field and add uswds combobox #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
=== Plugin Name ===
Contributors: equalizedigital, alh0319, stevejonesdev
Contributors: equalizedigital, alh0319, stevejonesdev, michelmany
Tags: Gravity Forms, accessibility, accessible, forms
Requires at least: 3.0.1
Tested up to: 3.4
Expand Down
7 changes: 5 additions & 2 deletions accessible-combobox-gravity-forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
*/
define( 'ACCESSIBLE_COMBOBOX_GRAVITY_FORMS_VERSION', '1.0.0' );

add_action( 'gform_loaded', 'run_accessible_combobox_gravity_forms', 5 );

/**
* The code that runs during plugin activation.
* This action is documented in includes/class-accessible-combobox-gravity-forms-activator.php
Expand Down Expand Up @@ -74,9 +76,10 @@ function deactivate_accessible_combobox_gravity_forms() {
* @since 1.0.0
*/
function run_accessible_combobox_gravity_forms() {
if ( ! method_exists( 'GFForms', 'include_addon_framework' ) ) {
return;
}

$plugin = new Accessible_Combobox_Gravity_Forms();
$plugin->run();

}
run_accessible_combobox_gravity_forms();
49 changes: 49 additions & 0 deletions admin/class-accessible-combobox-gravity-forms-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,53 @@ public function enqueue_scripts() {

}

public function hook_gravity_form_dropdown_field_css( $classes, $field, $form ) {
if ( $field->type === 'select' && (int)$field->enableEnhancedUI ) {
$classes .= ' gfield--accessible-combobox';
}

return $classes;
}

public function hook_gravity_form_dropdown_field( $input, $field, $value, $lead_id, $form_id )
{

if ( $field->type !== 'select' ) {
return $input;
}

if ( ! (int)$field->enableEnhancedUI ) {
return $input;
}

$field->enableEnhancedUI = false;

$choices = $this->acgf_get_choices_from_field( $field->choices );

$input = '
<div class="usa-combo-box">
<select name="input_'.$field->id.'" id="input_'.$form_id.'_'.$field->id.'" class="usa-select">
<option value class="gf_placeholder">'.$field->placeholder.'</option>
'. $choices .'
</select>
</div>
';

return $input;
}

/**
* @param $choices
* @return string
*/
private function acgf_get_choices_from_field( $choices ): string
{
$options = "";

foreach( $choices as $choice ) {
$options .= "<option value=\"{$choice['value']}\">{$choice['text']}</option>";
}

return $options;
}
}
15 changes: 15 additions & 0 deletions includes/class-accessible-combobox-gravity-forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,21 @@ private function define_admin_hooks() {
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );

$this->loader->add_filter(
'gform_field_css_class',
$plugin_admin,
'hook_gravity_form_dropdown_field_css',
10,
3
);

$this->loader->add_filter(
'gform_field_input',
$plugin_admin,
'hook_gravity_form_dropdown_field',
10,
5
);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions public/class-accessible-combobox-gravity-forms-public.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public function enqueue_scripts() {
*/

wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/accessible-combobox-gravity-forms-public.js', array( 'jquery' ), $this->version, false );
wp_enqueue_script( 'uswds-init', plugin_dir_url( __FILE__ ) . 'js/uswds.init.js', array( 'jquery' ), $this->version, false );
wp_enqueue_script( 'uswds-min', plugin_dir_url( __FILE__ ) . 'js/uswds.min.js', array( 'jquery' ), $this->version, true );

}

Expand Down
121 changes: 117 additions & 4 deletions public/css/accessible-combobox-gravity-forms-public.css

Large diffs are not rendered by default.

Loading