Skip to content
← Back to Functions
Code

Breadcrumb Trail Builder

Builds visible breadcrumb trail data from route context.

Function signature

ogBuildBreadcrumbTrail(route_data = array(), options = array())

Categories

  • SEO and Routing

Parameters

route_dataRoute context used to build breadcrumbs or cache keys.optionsOptional documented policy controls for this helper.

Return value

Public-safe status string returned by the function for explicit controller branching or logging.

  • success
  • message
  • data

Compatibility

Existing function name, slug, path, and call order preserved; advertised metadata corrected to the actual source behavior.

Minimum PHP version: 7.4

Security notes

Use caller-owned allowlists and context-specific escaping; validate admin actions, export fields, privacy plans, cache keys, templates, settings, routes, and ecommerce policies before production use.

Code

<?php

/*
 * Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
 * Free for personal and internal use. Paid project use requires visible credit
 * to Jeffery L. Paris. Corporate use requires a paid license fee unless a
 * separate written license states otherwise.
 */

/**
 * Builds visible breadcrumb trail data from route context.
 *
 * Primary use case: Public and admin navigation.
 * Typical inputs: route data, label map.
 * Typical output: breadcrumb array.
 *
 * Implementation note: Use canonical URLs and escape labels at render time.
 *
 * @param array $route_data Route data with label, url, section, and parent keys.
 * @param array $options Optional home label, home URL, and label map.
 * @return array Breadcrumb trail data.
 */
function ogBuildBreadcrumbTrail($route_data = array(), $options = array()) {
	$result = array(
		'success' => false,
		'message' => '',
		'data' => array()
	);

	if (!is_array($route_data)) {
		$result['message'] = 'Route data must be an array.';
		return $result;
	}

	if (!is_array($options)) {
		$options = array();
	}

	$home_label = 'Home';
	if (!empty($options['home_label'])) {
		$home_label = trim((string)$options['home_label']);
	}

	$home_url = '//phpog.com/';
	if (!empty($options['home_url'])) {
		$home_url = trim((string)$options['home_url']);
	}

	$trail = array();
	$trail[] = array(
		'label' => $home_label,
		'url' => $home_url,
		'current' => false
	);

	if (!empty($route_data['parents']) && is_array($route_data['parents'])) {
		foreach ($route_data['parents'] as $parent) {
			if (!is_array($parent)) {
				continue;
			}
			$label = '';
			$url = '';
			if (!empty($parent['label'])) {
				$label = trim((string)$parent['label']);
			}
			if (!empty($parent['url'])) {
				$url = trim((string)$parent['url']);
			}
			if (!empty($label)) {
				$trail[] = array(
					'label' => $label,
					'url' => $url,
					'current' => false
				);
			}
		}
	}

	$current_label = '';
	if (!empty($route_data['label'])) {
		$current_label = trim((string)$route_data['label']);
	}
	if (!empty($current_label)) {
		$trail[] = array(
			'label' => $current_label,
			'url' => '',
			'current' => true
		);
	}

	$result['success'] = true;
	$result['message'] = 'Breadcrumb trail built.';
	$result['data'] = array('breadcrumbs' => $trail, 'count' => count($trail));
	return $result;
}