diff --git a/wp-content/themes/ccat/app/composables/useNodeByUri.gql b/wp-content/themes/ccat/app/composables/useNodeByUri.gql index f7a763b..7c424df 100644 --- a/wp-content/themes/ccat/app/composables/useNodeByUri.gql +++ b/wp-content/themes/ccat/app/composables/useNodeByUri.gql @@ -4,7 +4,7 @@ query nodeByUri($uri: String!) { id breadcrumbs { label - uri + to } ... on Page { ...ThePage diff --git a/wp-content/themes/ccat/app/composables/useNodeByUri.ts b/wp-content/themes/ccat/app/composables/useNodeByUri.ts index bbf14bc..b44abc7 100644 --- a/wp-content/themes/ccat/app/composables/useNodeByUri.ts +++ b/wp-content/themes/ccat/app/composables/useNodeByUri.ts @@ -17,11 +17,6 @@ import { TheResource, } from "#components"; -interface BreadcrumbItem { - label: string; - to?: string; -} - export async function useNodeByUri() { const route = useRoute(); const uri = computed(() => route.path); @@ -30,18 +25,7 @@ export async function useNodeByUri() { throw createError({ statusCode: 500, message: "Erreur lors de la récupération du contenu", fatal: true }); } const node = data.value?.data.nodeByUri; - - const breadcrumbs: BreadcrumbItem[] = []; - if (node?.breadcrumbs) { - breadcrumbs.push( - ...node.breadcrumbs - .filter((item: any) => item.label) - .map((item: any) => ({ - label: item.label, - to: item.uri || undefined, - })), - ); - } + const breadcrumbs = node?.breadcrumbs?.map(({ label, to }) => ({ label, to: to || undefined })) || []; switch (node?.__typename) { case "Page": diff --git a/wp-content/themes/ccat/functions.php b/wp-content/themes/ccat/functions.php index 572c999..8d487e5 100644 --- a/wp-content/themes/ccat/functions.php +++ b/wp-content/themes/ccat/functions.php @@ -22,7 +22,7 @@ require_once __DIR__ . '/includes/taxonomies/resource-category.php'; // Forms -// GraphQL +// WPGraphQL require_once __DIR__ . '/includes/graphql/address.php'; require_once __DIR__ . '/includes/graphql/breadcrumbs.php'; require_once __DIR__ . '/includes/graphql/user-switching.php'; @@ -35,5 +35,6 @@ require_once __DIR__ . '/includes/graphql/user-switching.php'; // Vendors require_once __DIR__ . '/includes/vendors/acf.php'; +require_once __DIR__ . '/includes/vendors/wpgraphql.php'; // Widgets diff --git a/wp-content/themes/ccat/includes/graphql/breadcrumbs.php b/wp-content/themes/ccat/includes/graphql/breadcrumbs.php index 8705fee..69aa18b 100644 --- a/wp-content/themes/ccat/includes/graphql/breadcrumbs.php +++ b/wp-content/themes/ccat/includes/graphql/breadcrumbs.php @@ -12,9 +12,9 @@ function ccat_register_breadcrumb_graphql_types() { 'type' => array( 'non_null' => 'String' ), 'description' => 'The display label for the breadcrumb', ), - 'uri' => array( + 'to' => array( 'type' => 'String', - 'description' => 'The URI for the breadcrumb link (null for current page)', + 'description' => 'The to for the breadcrumb link (null for current page)', ), ), ) @@ -28,10 +28,20 @@ function ccat_register_breadcrumb_graphql_fields() { 'Node', 'breadcrumbs', array( - 'type' => array( 'list_of' => 'BreadcrumbItem' ), + 'type' => array( + 'list_of' => array( 'non_null' => 'BreadcrumbItem' ), + ), 'description' => 'Breadcrumb navigation items for this content', 'resolve' => function ( $node ) { - return ccat_get_breadcrumbs( $node ); + return array_map( + function ( $item ) { + return array( + 'label' => html_entity_decode( $item['label'], ENT_QUOTES, 'UTF-8' ), + 'to' => $item['to'] ?? null, + ); + }, + ccat_get_breadcrumbs( $node ) + ); }, ) ); @@ -43,15 +53,13 @@ function ccat_get_breadcrumbs( $node ) { if ( ! $node ) { return $breadcrumbs; } - - // Handle WPGraphQL Model objects if ( $node instanceof \WPGraphQL\Model\Post ) { $post = get_post( $node->databaseId ); if ( $post ) { $breadcrumbs = ccat_get_post_breadcrumbs( $post ); $breadcrumbs[] = array( - 'label' => html_entity_decode( get_the_title( $post->ID ), ENT_QUOTES, 'UTF-8' ), - 'uri' => null, + 'label' => get_the_title( $post->ID ), + 'to' => null, ); } } elseif ( $node instanceof \WPGraphQL\Model\Term ) { @@ -59,8 +67,8 @@ function ccat_get_breadcrumbs( $node ) { if ( $term && ! is_wp_error( $term ) ) { $breadcrumbs = ccat_get_term_breadcrumbs( $term ); $breadcrumbs[] = array( - 'label' => html_entity_decode( $term->name, ENT_QUOTES, 'UTF-8' ), - 'uri' => null, + 'label' => $term->name, + 'to' => null, ); } } @@ -68,15 +76,15 @@ function ccat_get_breadcrumbs( $node ) { } // Helper: Get breadcrumbs for a given post -function ccat_get_post_breadcrumbs( $post ) { +function ccat_get_post_breadcrumbs( WP_Post $post ) { $breadcrumbs = array(); - $is_front_page = get_option( 'show_on_front' ) === 'page' && get_option( 'page_on_front' ) == $post->ID; + $is_front_page = get_option( 'show_on_front' ) === 'page' && get_option( 'page_on_front' ) === $post->ID; if ( $is_front_page ) { return $breadcrumbs; // No breadcrumbs for the front page } else { $breadcrumbs[] = array( 'label' => 'Accueil', - 'uri' => '/', + 'to' => '/', ); } $post_type = get_post_type( $post->ID ); @@ -87,8 +95,8 @@ function ccat_get_post_breadcrumbs( $post ) { $ancestors = array_reverse( $ancestors ); foreach ( $ancestors as $ancestor_id ) { $breadcrumbs[] = array( - 'label' => html_entity_decode( get_the_title( $ancestor_id ), ENT_QUOTES, 'UTF-8' ), - 'uri' => str_replace( home_url(), '', get_permalink( $ancestor_id ) ), + 'label' => get_the_title( $ancestor_id ), + 'to' => str_replace( home_url(), '', get_permalink( $ancestor_id ) ), ); } } @@ -97,42 +105,42 @@ function ccat_get_post_breadcrumbs( $post ) { case 'post': $breadcrumbs[] = array( 'label' => 'Actualités', - 'uri' => '/actualites/', + 'to' => '/actualites/', ); break; case 'event': $breadcrumbs[] = array( 'label' => 'Événements', - 'uri' => '/evenements/', + 'to' => '/evenements/', ); break; case 'location': $breadcrumbs[] = array( 'label' => 'Lieux', - 'uri' => '/lieux/', + 'to' => '/lieux/', ); break; case 'membership': $breadcrumbs[] = array( 'label' => 'Membres', - 'uri' => '/membres/', + 'to' => '/membres/', ); break; case 'project': $breadcrumbs[] = array( 'label' => 'Projets', - 'uri' => '/projets/', + 'to' => '/projets/', ); break; case 'resource': $breadcrumbs[] = array( 'label' => 'Ressources', - 'uri' => '/ressources/', + 'to' => '/ressources/', ); break; } @@ -141,7 +149,7 @@ function ccat_get_post_breadcrumbs( $post ) { } // Helper: Get breadcrumbs for a given term -function ccat_get_term_breadcrumbs( $term ) { +function ccat_get_term_breadcrumbs( WP_Term $term ) { $breadcrumbs = array(); $taxonomy = $term->taxonomy; if ( $term->parent ) { @@ -152,8 +160,8 @@ function ccat_get_term_breadcrumbs( $term ) { $ancestor_term = get_term( $ancestor_id, $taxonomy ); if ( $ancestor_term && ! is_wp_error( $ancestor_term ) ) { $breadcrumbs[] = array( - 'label' => html_entity_decode( $ancestor_term->name, ENT_QUOTES, 'UTF-8' ), - 'uri' => str_replace( home_url(), '', get_term_link( $ancestor_term ) ), + 'label' => $ancestor_term->name, + 'to' => str_replace( home_url(), '', get_term_link( $ancestor_term ) ), ); } } @@ -165,7 +173,7 @@ function ccat_get_term_breadcrumbs( $term ) { array( array( 'label' => 'Catégories', - 'uri' => '/categories/', + 'to' => '/categories/', ), ), $breadcrumbs @@ -177,7 +185,7 @@ function ccat_get_term_breadcrumbs( $term ) { array( array( 'label' => 'Disciplines', - 'uri' => '/disciplines/', + 'to' => '/disciplines/', ), ), $breadcrumbs @@ -189,7 +197,7 @@ function ccat_get_term_breadcrumbs( $term ) { array( array( 'label' => 'Catégories de projets', - 'uri' => '/categories-projets/', + 'to' => '/categories-projets/', ), ), $breadcrumbs @@ -201,7 +209,7 @@ function ccat_get_term_breadcrumbs( $term ) { array( array( 'label' => 'Catégories de ressources', - 'uri' => '/categories-ressources/', + 'to' => '/categories-ressources/', ), ), $breadcrumbs diff --git a/wp-content/themes/ccat/includes/vendors/wpgraphql.php b/wp-content/themes/ccat/includes/vendors/wpgraphql.php new file mode 100644 index 0000000..b3d9bbc --- /dev/null +++ b/wp-content/themes/ccat/includes/vendors/wpgraphql.php @@ -0,0 +1 @@ +