Skip to content

Instantly share code, notes, and snippets.

@rachelslurs
Last active March 30, 2017 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rachelslurs/84332e7eef4390f99acf352ebb3967ba to your computer and use it in GitHub Desktop.
Save rachelslurs/84332e7eef4390f99acf352ebb3967ba to your computer and use it in GitHub Desktop.
This is all work I did when I was thinking we were going to use the gravity forms awful markup
/**
* Filters the next, previous and submit buttons.
* Replaces the forms <input> buttons with <button> while maintaining attributes from original <input>.
*
* @param string $button Contains the <input> tag to be filtered.
* @param object $form Contains all the properties of the current form.
*
* @return string The filtered button.
*/
add_filter( 'gform_next_button', 'input_to_button', 10, 2 );
add_filter( 'gform_previous_button', 'input_to_button', 10, 2 );
add_filter( 'gform_submit_button', 'input_to_button', 10, 2 );
function input_to_button( $button, $form ) {
$dom = new DOMDocument();
$dom->loadHTML( $button );
$input = $dom->getElementsByTagName( 'input' )->item(0);
$new_button = $dom->createElement( 'button' );
$new_button->appendChild( $dom->createTextNode( $input->getAttribute( 'value' ) ) );
$input->removeAttribute( 'value' );
foreach( $input->attributes as $attribute ) {
if ($attribute->name == 'class') {
$attribute->value .= ' secondary all-caps';
}
$new_button->setAttribute( $attribute->name, $attribute->value );
}
$input->parentNode->replaceChild( $new_button, $input );
return $dom->saveHtml( $new_button );
}
// Adds foundation data validation to gravity form with id 5
add_filter( 'gform_form_tag', 'form_tag', 10, 2 );
function form_tag($form_tag, $form) {
if ( $form['id'] != 5 ) {
//not the form whose tag you want to change, return the unchanged tag
return $form_tag;
}
$form_tag = preg_replace( "|method=|", "data-abide novalidate method=", $form_tag );
return $form_tag;
}
// Adds the ability to hide individual labels
add_filter( 'gform_enable_field_label_visibility_settings', '__return_true' );
add_filter( 'gform_tooltips', 'add_pattern_tooltip' );
function add_pattern_tooltip( $tooltips ) {
$tooltips['form_patterns_picker'] = sprintf(
'<h6>%s</h6>%s <a href="%s">Link</a>',
__( 'Patterns', 'gravityforms' ),
__( 'Do not use with input masks. I did not test for the use case. Read more about usage here: ', 'gravityforms' ),
__( 'http://foundation.zurb.com/sites/docs/abide.html#builtin-patterns-and-validators', 'gravityforms' )
);
return $tooltips;
}
add_action( 'gform_field_standard_settings', 'additional_field_settings', 10, 2 );
function additional_field_settings($position, $form_id) {
//create settings on position 25 (right after Field Label)
if ($position == 25) {
$patterns = array(
'none' => '-',
'alpha' => 'alpha',
'alpha_numeric' => 'alpha_numeric',
'date' => 'date',
'domain' => 'domain',
'email' => 'email',
'integer' => 'integer',
'number' => 'number',
'url' => 'url'
);
?>
<li class="pattern_setting field_setting">
<label for="field_admin_label" class="section_label">
<?php esc_html_e('Pattern', 'gravityforms'); ?>
<?php gform_tooltip( 'form_patterns_picker' ); ?>
</label>
<select id="patterns_picker" onchange="SetFieldProperty('patternField', this.value);">
<?php foreach ($patterns as $key => $value) { ?>
<option value="<?php echo $key;?>"> <?php echo $value; ?> </option>
<?php } ?>
</select>
</li>
<?php
}
}
add_action( 'gform_editor_js', 'editor_script' );
function editor_script(){
?>
<script type='text/javascript'>
//adding setting to fields of type "text" and "email"
fieldSettings.text += ', .pattern_setting';
fieldSettings.email += ', .pattern_setting';
//binding to the load field settings event to initialize the checkbox
jQuery(document).bind('gform_load_field_settings', function(event, field, form){
jQuery('#patterns_picker').val(field.patternField || '');
});
</script>
<?php
}
/**
* Inject pattern attributes into individual fields of type email or text
* Also adds span class="form-error" for abide form validation
* adds aria-label to input if label is marked as hidden
*
* @param string $markup The markup for an individual field.
* @param GF_Field $field The Gravity Forms Field object.
*/
add_filter( 'gform_field_content', 'add_pattern_attribute', 10, 2 );
function add_pattern_attribute($markup, $field) {
if (!isset($field->patternField) || !$field->patternField || $field->patternField == 'none') {
return $markup;
}
if ($field->type == 'email' || $field->type == 'text' && isset($field->patternField)) {
// Find the first input element.
$regex = '/\<(?:input)\s+[^\>]+?(\s*\/?\>){1}/im';
if (!preg_match( $regex, $markup, $input ) ) {
return $markup;
}
$pattern = sprintf(' pattern="%s"', esc_attr( $field->patternField ) );
$element = str_replace( $input[1], $pattern . $input[1], $input[0] );
return str_replace( $input[0], $element, $markup );
}
}
/**
* Adds span class="form-error" for abide form validation
* if input has a defined error message
* and adds aria-label to input if label is marked as hidden
*
* @param string $markup The markup for an individual field.
* @param GF_Field $field The Gravity Forms Field object.
*/
add_filter( 'gform_field_content', 'add_form_error', 10, 2 );
function add_form_error($markup, $field) {
if (!isset($field->errorMessage)) {
return $markup;
}
else {
// Find the first input element.
$regex = '/\<(?:input)\s+[^\>]+?(\s*\/?\>){1}/im';
if (!preg_match( $regex, $markup, $input ) ) {
return $markup;
}
$element = $input[0];
$element .= '<span class="form-error">' . $field->errorMessage . '</span>';
// Adds aria-label attribute to input if hidden label is selected
// Assumes there's a placeholder value
if ($field->labelPlacement == 'hidden_label') {
$element = preg_replace("|placeholder|", "aria-label='" . esc_attr($field->label) . "' placeholder", $element);
}
return str_replace( $input[0], $element, $markup );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment