Skip to content

Commit

Permalink
Issue/1965 add ability to only show first letter of name fields (#2140)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcasual authored Sep 26, 2024
2 parents 6e3aff5 + 9de78af commit f9a62f6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
25 changes: 25 additions & 0 deletions includes/fields/class-gravityview-field-name.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,33 @@ class GravityView_Field_Name extends GravityView_Field {

public function __construct() {
$this->label = esc_html__( 'Name', 'gk-gravityview' );

parent::__construct();
}

/**
* {@inheritDoc}
*
* @since TBD
*/
public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) {
if ( 'edit' === $context ) {
return $field_options;
}

$field_options['show_as_initials'] = array(
'type' => 'checkbox',
'label' => __( 'Show as initials', 'gk-gravityview' ),
'desc' => __( 'This displays the first letter of the first and last names.', 'gk-gravityview' ),
'value' => '',
'group' => 'display',
);


return $field_options;
}


}

new GravityView_Field_Name();
4 changes: 4 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Beautifully display your Gravity Forms entries. Learn more on [gravitykit.com](h

* Added: Ability to view and edit entries inside a lightbox.
* Added: `:human` merge tag modifier for date fields to display in human-readable format (e.g., `10 minutes ago`, `5 days from now`).
* Added: Option to display the Name field value as initials.
* Fixed: Clearing search removed all URL query parameters and under some circumstances redirected to the homepage.
* Fixed: Searching the View added duplicate search parameters to the URL.
* Fixed: PHP 8.2 deprecation notice related to dynamic property creation.
Expand All @@ -33,6 +34,9 @@ Beautifully display your Gravity Forms entries. Learn more on [gravitykit.com](h
* Fixed: Sorting entries in random order not working.
* Fixed: Multiselect field values that started with a square bracket were not displayed as selected on the Edit Entry screen.

#### 💻 Developer Updates
* Added `gk/gravityview/field/name/display` filter to modify the Name field display value.

= 2.28.0 on August 29, 2024 =

This update adds support for plain-text URLs in entry moderation merge tags, and fixes several bugs, including critical errors in the View editor. Starting with this version, PHP 7.4 or newer is required.
Expand Down
36 changes: 33 additions & 3 deletions templates/fields/field-name-html.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
/**
* The default name field output template.
*
* @global \GV\Template_Context $gravityview
* @global Template_Context $gravityview
* @since 2.0
*/

use GV\Template_Context;

if ( ! isset( $gravityview ) || empty( $gravityview->template ) ) {
gravityview()->log->error( '{file} template loaded without context', array( 'file' => __FILE__ ) );
return;
Expand All @@ -16,8 +18,36 @@
$display_value = $gravityview->display_value;
$entry = $gravityview->entry->as_entry();


$field_settings = $gravityview->field->as_configuration();

if ( floatval( $field_id ) != intval( $field_id ) ) {
echo esc_html( gravityview_get_field_value( $entry, $field_id, $display_value ) );
$display_value = esc_html( gravityview_get_field_value( $entry, $field_id, $display_value ) );
} else {
echo gravityview_get_field_value( $entry, $field_id, $display_value );
$display_value = gravityview_get_field_value( $entry, $field_id, $display_value );
}

if ( ! empty( $field_settings['show_as_initials'] ) ) {
$names = explode( ' ', $display_value );

$display_value = '';

foreach ( $names as $name ) {
$first_char = function_exists( 'mb_substr' ) ? mb_substr( $name, 0, 1 ) : substr( $name, 0, 1 );
$upper_char = function_exists( 'mb_strtoupper' ) ? mb_strtoupper( $first_char ) : strtoupper( $first_char );

$display_value .= trim( $upper_char ) . '.';
}
}

/**
* Overrides the Name field display value.
*
* @filter `gk/gravityview/field/name/display`
*
* @since TBD
*
* @param string $display_value Name or initials to display.
* @param Template_Context $gravityview The GravityView template context.
*/
echo apply_filters( 'gk/gravityview/field/name/display', $display_value, $gravityview );

0 comments on commit f9a62f6

Please sign in to comment.