Skip to content

Instantly share code, notes, and snippets.

@SKuipers
Last active March 24, 2021 20:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SKuipers/b1dd461f5246f257582072596efe8744 to your computer and use it in GitHub Desktop.
Save SKuipers/b1dd461f5246f257582072596efe8744 to your computer and use it in GitHub Desktop.
Breadcumb OOification for Gibbon v17.0.00

The breadcrumbs->add() method:

/**
     * Add a named route to the trail.
     *
     * @param string $title   Name to display on this route's link
     * @param string $route   URL relative to the trail's BaseURL
     * @param array  $params  Additional URL params to append to the route
     * @return self
     */
    public function add(string $title, string $route = '', array $params = [])

In use:

$page->breadcrumbs
    ->add(__('Manage Users'), 'user_manage.php')
    ->add(__('Add User'));

A few notes:

  • The title for each breadcrumb should be wrapped in __() for translation.
  • The home breadcrumb is automatically added.
  • The module entry point is automatically added, no need for the second getModuleEntry() breadcrumb.
  • The module portion of the URL (eg: index.php?q=/modules/User Admin/) isn't needed: this is automatiaclly added.
  • The final breadcrumb (trailEnd) doesnt need a url.
  • The __() function no longer requires a $guid so these can be removed eg: __($guid, 'Foo') to __('Foo')

New note:

  • Delete actions are now handled with a modal window, so the breadcrumbs for these can be removed rather than refactored.

Example 1: Single end-point, no sub-pages.

echo "<div class='trail'>";
echo "<div class='trailHead'><a href='".$_SESSION[$guid]['absoluteURL']."'>".__($guid, 'Home')."</a> > <a href='".$_SESSION[$guid]['absoluteURL'].'/index.php?q=/modules/'.getModuleName($_GET['q']).'/'.getModuleEntry($_GET['q'], $connection2, $guid)."'>".__($guid, getModuleName($_GET['q']))."</a> > </div><div class='trailEnd'>".__($guid, 'Manage Staff').'</div>';
echo '</div>';

Refactored:

$page->breadcrumbs->add(__('Manage Staff'));

Example 2: Several sub-pages with URL parameters.

echo "<div class='trail'>";
echo "<div class='trailHead'><a href='".$_SESSION[$guid]['absoluteURL']."'>".__($guid, 'Home')."</a> > <a href='".$_SESSION[$guid]['absoluteURL'].'/index.php?q=/modules/'.getModuleName($_GET['q']).'/'.getModuleEntry($_GET['q'], $connection2, $guid)."'>".__($guid, getModuleName($_GET['q']))."</a> > <a href='".$_SESSION[$guid]['absoluteURL'].'/index.php?q=/modules/'.getModuleName($_GET['q']).'/tt.php&gibbonSchoolYearID='.$_GET['gibbonSchoolYearID']."'>".__($guid, 'Manage Timetables')."</a> > <a href='".$_SESSION[$guid]['absoluteURL'].'/index.php?q=/modules/'.getModuleName($_GET['q'])."/tt_edit.php&gibbonTTID=$gibbonTTID&gibbonSchoolYearID=".$_GET['gibbonSchoolYearID']."'>".__($guid, 'Edit Timetable')."</a> > <a href='".$_SESSION[$guid]['absoluteURL'].'/index.php?q=/modules/'.getModuleName($_GET['q'])."/tt_edit_day_edit.php&gibbonTTDayID=$gibbonTTDayID&gibbonTTID=$gibbonTTID&gibbonSchoolYearID=$gibbonSchoolYearID'>".__($guid, 'Edit Timetable Day')."</a> > </div><div class='trailEnd'>".__($guid, 'Classes in Period').'</div>';
echo '</div>';

Refactored:

$urlParams = ['gibbonSchoolYearID' => $gibbonSchoolYearID, 'gibbonTTID' => $gibbonTTID, 'gibbonTTDayID' => $gibbonTTDayID];

$page->breadcrumbs
    ->add(__('Manage Timetables'), 'tt.php', $urlParams)
    ->add(__('Edit Timetable'), 'tt_edit.php', $urlParams)
    ->add(__('Edit Timetable Day'), 'tt_edit_day_edit.php', $urlParams)
    ->add(__('Classes in Period'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment