@@ -602,19 +602,21 @@ protected function register_graphql_field( string $type_name, string $field_name
602602 *
603603 * @see: https://github.com/wp-graphql/wp-graphql-acf/issues/25
604604 */
605+ $ enum_type = $ this ->register_choices_of_acf_fields_as_enum_type ( $ acf_field );
606+ $ enum_type = ! empty ( $ enum_type ) ? $ enum_type : 'String ' ;
605607 if ( empty ( $ acf_field ['multiple ' ] ) ) {
606608 if ('array ' === $ acf_field ['return_format ' ] ){
607- $ field_config ['type ' ] = [ 'list_of ' => ' String ' ];
609+ $ field_config ['type ' ] = [ 'list_of ' => $ enum_type ];
608610 $ field_config ['resolve ' ] = function ( $ root ) use ( $ acf_field ) {
609611 $ value = $ this ->get_acf_field_value ( $ root , $ acf_field , true );
610612
611613 return ! empty ( $ value ) && is_array ( $ value ) ? $ value : [];
612614 };
613615 }else {
614- $ field_config ['type ' ] = ' String ' ;
616+ $ field_config ['type ' ] = $ enum_type ;
615617 }
616618 } else {
617- $ field_config ['type ' ] = [ 'list_of ' => ' String ' ];
619+ $ field_config ['type ' ] = [ 'list_of ' => $ enum_type ];
618620 $ field_config ['resolve ' ] = function ( $ root ) use ( $ acf_field ) {
619621 $ value = $ this ->get_acf_field_value ( $ root , $ acf_field );
620622
@@ -623,7 +625,9 @@ protected function register_graphql_field( string $type_name, string $field_name
623625 }
624626 break ;
625627 case 'radio ' :
626- $ field_config ['type ' ] = 'String ' ;
628+ $ enum_type = $ this ->register_choices_of_acf_fields_as_enum_type ( $ acf_field );
629+ $ enum_type = ! empty ( $ enum_type ) ? $ enum_type : 'String ' ;
630+ $ field_config ['type ' ] = $ enum_type ;
627631 break ;
628632 case 'number ' :
629633 case 'range ' :
@@ -1501,4 +1505,42 @@ protected function add_acf_fields_to_graphql_types() {
15011505
15021506 }
15031507
1508+ public function register_choices_of_acf_fields_as_enum_type ( array $ acf_field ): string {
1509+ // If the field isn't a select or radio field, return an empty string.
1510+ if ( 'select ' !== $ acf_field ['type ' ] && 'radio ' !== $ acf_field ['type ' ] ) {
1511+ return '' ;
1512+ }
1513+
1514+ // Generate a unique name for the enum type using the field key.
1515+ $ enum_type_name = ucfirst ( self ::camel_case ( $ acf_field ['name ' ] ) ) . 'Enum ' ;
1516+ if ( ! $ this ->type_registry ->has_type ( $ enum_type_name ) ) {
1517+ // Initialize an empty array to hold your enum values.
1518+ $ enum_values = [];
1519+
1520+ // Loop over the choices in the field and add them to the enum values array.
1521+ foreach ( $ acf_field ['choices ' ] as $ key => $ choice ) {
1522+ // Use the sanitize_key function to create a valid enum name from the choice key.
1523+ $ enum_key = strtoupper ( sanitize_key ( $ key ) );
1524+
1525+ // Add the choice to the enum values array.
1526+ $ enum_values [ $ enum_key ] = [
1527+ 'value ' => $ key ,
1528+ 'description ' => $ choice ,
1529+ ];
1530+ }
1531+
1532+ // Register enum type.
1533+ $ this ->type_registry ->register_enum_type (
1534+ $ enum_type_name ,
1535+ [
1536+ 'description ' => $ acf_field ['label ' ],
1537+ 'values ' => $ enum_values ,
1538+ ]
1539+ );
1540+ }
1541+
1542+ // Return the enum type name.
1543+ return $ enum_type_name ;
1544+ }
1545+
15041546}
0 commit comments