Created
          May 7, 2018 17:48 
        
      - 
      
- 
        Save jasonbahl/c5cb5339ace96cc2e0e1bea5b2e747b0 to your computer and use it in GitHub Desktop. 
    Gutenberg WPGraphQL - Page Template GraphQL Fields
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | /** | |
| * Adds a field to the Page type in the GraphQL Schema to allow for the page's "blockTemplate" to be fetched and | |
| * returned. | |
| */ | |
| add_filter( 'graphql_page_fields', function( $fields ) { | |
| $fields['blockTemplate'] = [ | |
| 'type' => \WPGraphQL\Types::string(), | |
| 'description' => __( 'The Block template defined for the page', 'oshpd-guten-blocks' ), | |
| 'args' => [ | |
| 'pageTemplate' => [ | |
| 'type' => \WPGraphQL\Types::string(), | |
| 'description' => __( 'The page template associated with the block template', 'oshpd-guten-blocks' ), | |
| ], | |
| ], | |
| 'resolve' => function( \WP_Post $post, array $args, $info, $context ) { | |
| $page_template = ! empty( $args['pageTemplate'] ) ? $args['pageTemplate'] : get_page_template_slug( $post->ID ); | |
| $saved_block_template = ! empty( $page_template ) ? get_post_meta( $post->ID, '_saved_block_template_' . $page_template, true ) : null; | |
| if ( ! empty ( $saved_block_template ) ) { | |
| $template = $saved_block_template; | |
| } else { | |
| $template = oshpd_get_default_page_block_template( $page_template ); | |
| } | |
| return $template; | |
| } | |
| ]; | |
| return $fields; | |
| } ); | |
| add_action( 'do_graphql_request', function() { | |
| /** | |
| * Define the PageTemplateBlockTemplate input field to allow for block template content to be persisted when | |
| * switching templates. | |
| */ | |
| $block_template_input = new \WPGraphQL\Type\WPInputObjectType([ | |
| 'name' => 'PageTemplateBlockTemplate', | |
| 'description' => __( 'Allows for content to be persisted when page templates are switched', 'oshpd-guten-blocks' ), | |
| 'fields' => [ | |
| 'pageTemplateSlug' => [ | |
| 'type' => \WPGraphQL\Types::non_null( \WPGraphQL\Types::string() ), | |
| 'description' => __( 'The Page Template the content is connected to', 'oshpd-guten-blocks' ), | |
| ], | |
| 'content' => [ | |
| 'type' => \WPGraphQL\Types::non_null( \WPGraphQL\Types::string() ), | |
| 'description' => __( 'The content that should be stored in connection with the page template', 'oshpd-guten-blocks' ), | |
| ], | |
| ], | |
| ]); | |
| /** | |
| * Filter in the blockTemplate field into the upda | |
| */ | |
| add_filter( 'graphql_post_object_mutation_input_fields', function( $fields, $post_type_object ) use ( $block_template_input ) { | |
| /** | |
| * Only add this to post types that support page attributes. | |
| */ | |
| if ( post_type_supports( $post_type_object->name, 'page-attributes' ) ) { | |
| $fields['blockTemplate'] = [ | |
| 'type' => $block_template_input, | |
| 'description' => __( 'Persist content entered for a particular page template', 'oshpd-guten-blocks' ) | |
| ]; | |
| } | |
| return $fields; | |
| }, 10, 2 ); | |
| }); | |
| /** | |
| * Hook into the PostObject update mutation and save the page_template content to meta. | |
| */ | |
| add_action( 'graphql_post_object_mutation_update_additional_data', function( $post_id, $input, $post_type_object, $mutation_name, $context, $info ) { | |
| if ( ! empty( $input['blockTemplate']['pageTemplateSlug'] ) && ! empty( $input['blockTemplate']['content'] ) ) { | |
| $content = preg_replace( "/\r|\n|\t/", "", $input['blockTemplate']['content'] ); | |
| update_post_meta( $post_id, '_saved_block_template_' . $input['blockTemplate']['pageTemplateSlug'], $content ); | |
| } | |
| }, 10, 6 ); | |
| /** | |
| * Returns the block template associated with a particular page template | |
| * | |
| * @param string $page_template The slug of the page template (ex: templates/page-template.php) | |
| * @return string The block template | |
| */ | |
| function oshpd_get_default_page_block_template( $page_template ) { | |
| $block_template = ''; | |
| switch ( $page_template ) { | |
| case 'templates/home.php': | |
| $block_template = ' | |
| <!-- wp:oshpd/homepage-hero /--> | |
| <!-- wp:oshpd/latest-news /--> | |
| <!-- wp:oshpd/leadership-cards /--> | |
| '; | |
| break; | |
| case 'templates/landing-page.php': | |
| $block_template = ' | |
| <!-- wp:oshpd/leadership-cards /--> | |
| <!-- wp:oshpd/homepage-hero /--> | |
| '; | |
| break; | |
| } | |
| $filtered_template = apply_filters( 'oshpd_get_default_page_block_template', $block_template, $page_template ); | |
| return preg_replace( "/\r|\n|\t/", "", $filtered_template ); | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment