You are here

opigno_breadcrumbs.module in Opigno 7

Breadcrumbs module definition.

File

modules/breadcrumbs/opigno_breadcrumbs.module
View source
<?php

/**
 * @file
 * Breadcrumbs module definition.
 */

/**
 * Implements hook_preprocess_page().
 */
function opigno_breadcrumbs_preprocess_page(&$vars) {
  $vars['breadcrumb'] = opigno_breadcrumbs();
}

/**
 * Get the breadcrumb trail for the current page.
 */
function opigno_breadcrumbs() {

  // Get the default breadcrumb.
  $breadcrumb = drupal_get_breadcrumb();

  // Fetch the group from URL context.
  // If we are in group context, handle our own breadcrumbs.
  $group = og_context('node');
  if (!empty($group['gid']) && $group['group_type'] == 'node') {

    // If the group is for a course or a class, we add custom logic
    $node = node_load($group['gid']);
    if (in_array($node->type, array(
      'course',
      'class',
    ))) {

      // First, add the homepage.
      $new_breadcrumb = array(
        l(t('Home'), NULL),
      );

      // If the node is a course, check if it's part of a class and if the user is a member of this class.
      if ($node->type == 'course' && module_exists('opigno_class_app')) {
        $classes = opigno_breadcrumbs_get_classes_from_course($node->nid);
        if (!empty($classes)) {
          $class = array_shift($classes);
          $class = node_load($class);

          // If the user is member of this class, add it to the breadcrumb
          if (og_is_member('node', $class->nid)) {
            $new_breadcrumb[] = l($class->title, 'node/' . $class->nid);
          }
        }
      }

      // If the user is in a course or class context and not on the homepage, add the breadcrumbs.
      if (current_path() !== 'node/' . $node->nid) {

        // Add the course itself.
        $group_title = opigno_breadcrumbs_get_group_title($group['gid']);
        $new_breadcrumb[] = l($group_title, "node/{$group['gid']}");

        // Ask other modules for breadcrumbs.
        $module_breadcrumbs = module_invoke_all('opigno_breadcrumbs', $group['gid']);
        if (!empty($module_breadcrumbs)) {
          $new_breadcrumb = array_merge($new_breadcrumb, $module_breadcrumbs);
        }
      }

      // Set the new trail.
      $breadcrumb = $new_breadcrumb;
    }
  }
  return theme_breadcrumb(array(
    'breadcrumb' => $breadcrumb,
  ));
}

/**
 * Helper function to get the group title.
 *
 * We use this instead of loading the entire node object from the DB.
 *
 * @param  int $nid
 *
 * @return string
 */
function opigno_breadcrumbs_get_group_title($nid) {
  $query = db_select('node', 'n');
  $query
    ->leftJoin('node_revision', 'v', 'n.vid = v.vid');
  return $query
    ->fields('v', array(
    'title',
  ))
    ->condition('n.nid', $nid)
    ->execute()
    ->fetchField();
}

/**
 * Helper function that return the classes linked to this course.
 */
function opigno_breadcrumbs_get_classes_from_course($course_nid) {
  $query = db_select('field_data_opigno_class_courses', 't')
    ->condition(db_and()
    ->condition('opigno_class_courses_target_id', $course_nid, '=')
    ->condition('bundle', 'class', '='))
    ->fields('t', array(
    'entity_id',
  ))
    ->execute();
  $classes_ids = array();
  while ($row = $query
    ->fetchAssoc()) {
    $classes_ids[$row['entity_id']] = $row['entity_id'];
  }
  return $classes_ids;
}

Functions

Namesort descending Description
opigno_breadcrumbs Get the breadcrumb trail for the current page.
opigno_breadcrumbs_get_classes_from_course Helper function that return the classes linked to this course.
opigno_breadcrumbs_get_group_title Helper function to get the group title.
opigno_breadcrumbs_preprocess_page Implements hook_preprocess_page().