diff --git a/includes/fields/class-gravityview-field-name.php b/includes/fields/class-gravityview-field-name.php index 90f9f58fb..37820b759 100644 --- a/includes/fields/class-gravityview-field-name.php +++ b/includes/fields/class-gravityview-field-name.php @@ -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(); diff --git a/readme.txt b/readme.txt index e94c296af..80e7eae7d 100644 --- a/readme.txt +++ b/readme.txt @@ -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. @@ -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. diff --git a/templates/fields/field-name-html.php b/templates/fields/field-name-html.php index 94b9e0d06..b29c75d75 100644 --- a/templates/fields/field-name-html.php +++ b/templates/fields/field-name-html.php @@ -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; @@ -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 );