Созонов Алексей

Веб-разработчик

Breadcrumbs из url Drupal

09.08.2015

1 Способ

<?php
/**
 * Override theme_breadcrumb().
 */
function [YOUR-THEME-NAME]_breadcrumb($breadcrumb) {
  $links = array();
  $path = '';

  // Get URL arguments
  $arguments = explode('/', request_uri());

  // Remove empty values
  foreach ($arguments as $key => $value) {
    if (empty($value)) {
      unset($arguments[$key]);
    }
  }
  $arguments = array_values($arguments);

  // Add 'Home' link
  $links[] = l(t('Home'), '<front>');

  // Add other links
  if (!empty($arguments)) {
    foreach ($arguments as $key => $value) {
      // Don't make last breadcrumb a link
      if ($key == (count($arguments) - 1)) {
        $links[] = drupal_get_title();
      } else {
        if (!empty($path)) {
          $path .= '/'. $value;
        } else {
          $path .= $value;
        }
        $links[] = l(drupal_ucfirst($value), $path);
      }
    }
  }

  // Set custom breadcrumbs
  drupal_set_breadcrumb($links);

  // Get custom breadcrumbs
  $breadcrumb = drupal_get_breadcrumb();

  // Hide breadcrumbs if only 'Home' exists
  if (count($breadcrumb) > 1) {
    return '<div class="breadcrumb">'. implode(' &raquo; ', $breadcrumb) .'</div>';
  }
}
?>

2 Способ

<?php
/**
 * Return a themed breadcrumb trail.
 *
 * @param $breadcrumb
 *   An array containing the breadcrumb links.
 * @return a string containing the breadcrumb output.
 */
function phptemplate_breadcrumb($breadcrumb) {
$links = array();
  $path = '';

  // Get URL arguments
  if( !isset($_REQUEST['q'])) {
  return '';
    }
  $arguments = explode('/', $_REQUEST['q']);

  // Remove empty values
  foreach ($arguments as $key => $value) {
    if (empty($value)) {
      unset($arguments[$key]);
    }
  }
  $arguments = array_values($arguments);

  // Add 'Home' link
  $links[] = l(t('Home'), '<front>');

  // Add other links
  if (!empty($arguments)) {
    foreach ($arguments as $key => $value) {
      //remove fr from breadcrumb

      if ($value == 'fr') {
        continue;
      }

      // Don't make last breadcrumb a link
      if ($key == (count($arguments) - 1)) {
        $links[] = drupal_get_title();
      } else {
        if (!empty($path)) {
          $path .= '/'. $value;
        } else {
          $path .= $value;
          $node=node_load(substr(drupal_get_normal_path($path), 5));
          $title=node_page_title($node);
        }
        //Check if path is valid
        if (drupal_lookup_path('source', $path)) {
          $links[] = l($title, $path);
        } else {
          $links[] = drupal_ucfirst($value);
        }
      }
    }
  }

  // Set custom breadcrumbs
  drupal_set_breadcrumb($links);

  // Get custom breadcrumbs
  $breadcrumb = drupal_get_breadcrumb();

  // Hide breadcrumbs if only 'Home' exists
  if (count($breadcrumb) > 1) {
    return '<div class="breadcrumb">'. implode(' &raquo; ', $breadcrumb) .'</div>';
  }
}
?>

3 Способ

/**
* Return a themed breadcrumb trail.
*
* @param $breadcrumb
*   An array containing the breadcrumb links.
* @return a string containing the breadcrumb output.
*/
function [PLACE_THEME_NAME_HERE]_breadcrumb($breadcrumb) {
    $links = array();
    $path = '';

    // Get URL arguments
    if( !isset($_REQUEST['q'])) {
        return '';
    }
    $arguments = explode('/', $_REQUEST['q']);

    // Remove empty values
    foreach ($arguments as $key => $value) {
        if (empty($value)) {
            unset($arguments[$key]);
        }
    }
    $arguments = array_values($arguments);

    // Add 'Home' link
    $links[] = l(t('Home'), '<front>');

    // Add other links
    if (!empty($arguments)) {
        foreach ($arguments as $key => $value) {
        //remove fr from breadcrumb

            if ($value == 'fr') {
              continue;
            }

            if ($value == 'Fr') {
              continue;
            }

            if ($value == 'nl') {
              continue;
            }

            if ($value == 'Nl') {
              continue;
            }

            // Don't make last breadcrumb a link
            if ($key == (count($arguments) - 1)) {
              $links[] = drupal_get_title();
            } else {
              if (!empty($path)) {
                $path .= '/'. $value;
              } else {
                $path .= $value;
                $node=node_load(substr(drupal_get_normal_path($path), 5));
                $title=node_page_title($node);
              }

              //Place link
              $links[] = l(drupal_ucfirst($value), $path);

            }
        }
    }

    // Set custom breadcrumbs
    drupal_set_breadcrumb($links);

    // Get custom breadcrumbs
    $breadcrumb = drupal_get_breadcrumb();

    // Hide breadcrumbs if only 'Home' exists
    if (count($breadcrumb) > 1) {
        return '<div class="breadcrumb">'. implode(' &raquo; ', $breadcrumb) .'</div>';
    }
}

#Drupal