generated from pascalmartineau/wp-skeleton
fix: GraphQL breadcrumbs and address
This commit is contained in:
@@ -16,6 +16,14 @@ function ccat_graphql_address_register() {
|
||||
'type' => 'String',
|
||||
'description' => 'Name of the location (e.g., street address)',
|
||||
),
|
||||
'streetNumber' => array(
|
||||
'type' => 'String',
|
||||
'description' => 'Street number',
|
||||
),
|
||||
'streetName' => array(
|
||||
'type' => 'String',
|
||||
'description' => 'Street name',
|
||||
),
|
||||
'city' => array(
|
||||
'type' => 'String',
|
||||
'description' => 'City name',
|
||||
@@ -67,7 +75,7 @@ function ccat_graphql_address_register_field_type() {
|
||||
return 'Address';
|
||||
},
|
||||
'resolve' => static function ( $root, $args, $context, $info, $field_type, $field_config ) {
|
||||
$value = get_field( $field_config['name'], $root, false );
|
||||
$value = get_field( $field_config->get_acf_field()['key'], $root['node']->data->ID, false );
|
||||
if ( empty( $value ) || ! is_array( $value ) ) {
|
||||
return null;
|
||||
}
|
||||
@@ -83,6 +91,12 @@ function ccat_graphql_address_register_field_type() {
|
||||
case 'name':
|
||||
$address_data['name'] = $field_value;
|
||||
break;
|
||||
case 'street_number':
|
||||
$address_data['streetNumber'] = $field_value;
|
||||
break;
|
||||
case 'street_name':
|
||||
$address_data['streetName'] = $field_value;
|
||||
break;
|
||||
case 'city':
|
||||
$address_data['city'] = $field_value;
|
||||
break;
|
||||
|
||||
204
wp-content/themes/ccat/includes/graphql/breadcrumbs.php
Normal file
204
wp-content/themes/ccat/includes/graphql/breadcrumbs.php
Normal file
@@ -0,0 +1,204 @@
|
||||
<?php
|
||||
|
||||
// Register GraphQL types and fields for breadcrumbs
|
||||
add_action( 'graphql_register_types', 'ccat_register_breadcrumb_graphql_types' );
|
||||
function ccat_register_breadcrumb_graphql_types() {
|
||||
register_graphql_object_type(
|
||||
'BreadcrumbItem',
|
||||
array(
|
||||
'description' => 'A single breadcrumb item',
|
||||
'fields' => array(
|
||||
'label' => array(
|
||||
'type' => array( 'non_null' => 'String' ),
|
||||
'description' => 'The display label for the breadcrumb',
|
||||
),
|
||||
'uri' => array(
|
||||
'type' => 'String',
|
||||
'description' => 'The URI for the breadcrumb link (null for current page)',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// Add 'breadcrumbs' field to all Node types
|
||||
add_action( 'graphql_register_types', 'ccat_register_breadcrumb_graphql_fields' );
|
||||
function ccat_register_breadcrumb_graphql_fields() {
|
||||
register_graphql_field(
|
||||
'Node',
|
||||
'breadcrumbs',
|
||||
array(
|
||||
'type' => array( 'list_of' => 'BreadcrumbItem' ),
|
||||
'description' => 'Breadcrumb navigation items for this content',
|
||||
'resolve' => function ( $node ) {
|
||||
return ccat_get_breadcrumbs( $node );
|
||||
},
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// Helper: Get breadcrumbs for a given node
|
||||
function ccat_get_breadcrumbs( $node ) {
|
||||
$breadcrumbs = array();
|
||||
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' => get_the_title( $post->ID ),
|
||||
'uri' => null,
|
||||
);
|
||||
}
|
||||
} elseif ( $node instanceof \WPGraphQL\Model\Term ) {
|
||||
$term = get_term( $node->databaseId );
|
||||
if ( $term && ! is_wp_error( $term ) ) {
|
||||
$breadcrumbs = ccat_get_term_breadcrumbs( $term );
|
||||
$breadcrumbs[] = array(
|
||||
'label' => $term->name,
|
||||
'uri' => null,
|
||||
);
|
||||
}
|
||||
}
|
||||
return $breadcrumbs;
|
||||
}
|
||||
|
||||
// Helper: Get breadcrumbs for a given post
|
||||
function ccat_get_post_breadcrumbs( $post ) {
|
||||
$breadcrumbs = array();
|
||||
$post_type = get_post_type( $post->ID );
|
||||
switch ( $post_type ) {
|
||||
case 'page':
|
||||
$ancestors = get_post_ancestors( $post->ID );
|
||||
if ( ! empty( $ancestors ) ) {
|
||||
$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 ) ),
|
||||
);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'post':
|
||||
$breadcrumbs[] = array(
|
||||
'label' => 'Actualités',
|
||||
'uri' => '/actualites/',
|
||||
);
|
||||
break;
|
||||
|
||||
case 'event':
|
||||
$breadcrumbs[] = array(
|
||||
'label' => 'Événements',
|
||||
'uri' => '/evenements/',
|
||||
);
|
||||
break;
|
||||
|
||||
case 'location':
|
||||
$breadcrumbs[] = array(
|
||||
'label' => 'Lieux',
|
||||
'uri' => '/lieux/',
|
||||
);
|
||||
break;
|
||||
|
||||
case 'membership':
|
||||
$breadcrumbs[] = array(
|
||||
'label' => 'Membres',
|
||||
'uri' => '/membres/',
|
||||
);
|
||||
break;
|
||||
|
||||
case 'project':
|
||||
$breadcrumbs[] = array(
|
||||
'label' => 'Projets',
|
||||
'uri' => '/projets/',
|
||||
);
|
||||
break;
|
||||
|
||||
case 'resource':
|
||||
$breadcrumbs[] = array(
|
||||
'label' => 'Ressources',
|
||||
'uri' => '/ressources/',
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
||||
return $breadcrumbs;
|
||||
}
|
||||
|
||||
// Helper: Get breadcrumbs for a given term
|
||||
function ccat_get_term_breadcrumbs( $term ) {
|
||||
$breadcrumbs = array();
|
||||
$taxonomy = $term->taxonomy;
|
||||
if ( $term->parent ) {
|
||||
$ancestors = get_ancestors( $term->term_id, $taxonomy );
|
||||
if ( ! empty( $ancestors ) ) {
|
||||
$ancestors = array_reverse( $ancestors );
|
||||
foreach ( $ancestors as $ancestor_id ) {
|
||||
$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 ) ),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
switch ( $taxonomy ) {
|
||||
case 'category':
|
||||
$breadcrumbs = array_merge(
|
||||
array(
|
||||
array(
|
||||
'label' => 'Catégories',
|
||||
'uri' => '/categories/',
|
||||
),
|
||||
),
|
||||
$breadcrumbs
|
||||
);
|
||||
break;
|
||||
|
||||
case 'discipline':
|
||||
$breadcrumbs = array_merge(
|
||||
array(
|
||||
array(
|
||||
'label' => 'Disciplines',
|
||||
'uri' => '/disciplines/',
|
||||
),
|
||||
),
|
||||
$breadcrumbs
|
||||
);
|
||||
break;
|
||||
|
||||
case 'project-category':
|
||||
$breadcrumbs = array_merge(
|
||||
array(
|
||||
array(
|
||||
'label' => 'Catégories de projets',
|
||||
'uri' => '/categories-projets/',
|
||||
),
|
||||
),
|
||||
$breadcrumbs
|
||||
);
|
||||
break;
|
||||
|
||||
case 'resource-category':
|
||||
$breadcrumbs = array_merge(
|
||||
array(
|
||||
array(
|
||||
'label' => 'Catégories de ressources',
|
||||
'uri' => '/categories-ressources/',
|
||||
),
|
||||
),
|
||||
$breadcrumbs
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
||||
return $breadcrumbs;
|
||||
}
|
||||
Reference in New Issue
Block a user