crumbs.taxonomy.inc in Crumbs, the Breadcrumbs suite 7
File
plugins/crumbs.taxonomy.inc
View source
<?php
function taxonomy_crumbs_plugins($api) {
$api
->multiPlugin('termParent');
foreach (field_info_fields() as $key => $info) {
if ($info['type'] == 'taxonomy_term_reference') {
foreach ($info['bundles'] as $entity_type => $bundles) {
$class = '_taxonomy_CrumbsMultiPlugin_termReference_' . $entity_type;
if (class_exists($class)) {
$plugin = new $class($key, $bundles);
$api
->multiPlugin("termReference.{$key}.{$entity_type}", $plugin);
}
}
}
}
$api
->disabledByDefault('termReference.*');
}
class _taxonomy_CrumbsMultiPlugin_termReference implements crumbs_MultiPlugin {
protected $fieldKey;
protected $bundles;
protected $entityType;
function __construct($field_key, $bundles) {
$this->fieldKey = $field_key;
$this->bundles = $bundles;
}
function describe($api) {
foreach ($this->bundles as $bundle) {
$api
->addRule($bundle, $bundle);
}
}
protected function _findParentPath($entity) {
$terms = array();
$items = field_get_items($this->entityType, $entity, $this->fieldKey);
if ($items) {
foreach ($items as $item) {
$terms[$item['tid']] = TRUE;
}
}
if (count($terms) > 1) {
$walk = $terms;
$visited = array();
while (!empty($walk)) {
$visited += $walk;
foreach ($walk as $tid => $true) {
$parents = taxonomy_get_parents($tid);
unset($walk[$tid]);
foreach ($parents as $tid => $parent) {
unset($terms[$tid]);
if (!isset($visited[$tid])) {
$walk[$tid] = $parent;
}
}
}
}
}
foreach ($terms as $tid => $term_info) {
$term = taxonomy_term_load($tid);
if (!empty($term)) {
$uri = entity_uri('taxonomy_term', $term);
if (!empty($uri)) {
return $uri['path'];
}
}
}
}
protected function _getPath_node($nid) {
return 'node/' . $nid;
}
protected function _getPath_user($uid) {
return 'user/' . $uid;
}
}
class _taxonomy_CrumbsMultiPlugin_termReference_node extends _taxonomy_CrumbsMultiPlugin_termReference {
protected $entityType = 'node';
function findParent__node_x($path, $item) {
$node = $item['map'][1];
$node = is_numeric($node) ? node_load($node) : $node;
$parent_path = $this
->_findParentPath($node);
if ($parent_path) {
return array(
$node->type => $parent_path,
);
}
}
}
class _taxonomy_CrumbsMultiPlugin_termReference_user extends _taxonomy_CrumbsMultiPlugin_termReference {
protected $entityType = 'user';
function findParent__user_x($path, $item) {
$user = $item['map'][1];
$user = is_numeric($user) ? user_load($user) : $user;
$parent_path = $this
->_findParentPath($user);
if ($parent_path) {
return array(
'user' => $parent_path,
);
}
}
}
class _taxonomy_CrumbsMultiPlugin_termReference_commerce_product extends _taxonomy_CrumbsMultiPlugin_termReference {
protected $entityType = 'commerce_product';
function findParent__product_x($path, $item) {
$product = $item['map'][1];
$product = is_numeric($product) ? commerce_product_load($product) : $product;
$parent_path = $this
->_findParentPath($product);
if ($parent_path) {
return array(
$product->type => $parent_path,
);
}
}
}
class taxonomy_CrumbsMultiPlugin_termParent implements crumbs_MultiPlugin {
function describe($api) {
foreach (taxonomy_get_vocabularies() as $voc_id => $voc) {
$api
->addRule($voc->machine_name, 'Vocabulary: ' . $voc->name);
}
$api
->addRule('*', t('Set taxonomy/term/123 as the parent for taxonomy/term/456, if 123 is the parent term of 456.'));
}
function findParent__taxonomy_term_x($path, $item) {
$term = $item['map'][2];
$term = is_numeric($term) ? taxonomy_term_load($term) : $term;
$parents = taxonomy_get_parents($term->tid);
$result = array();
foreach ($parents as $parent_tid => $parent_term) {
if ($parent_term->vocabulary_machine_name == $term->vocabulary_machine_name) {
$uri = entity_uri('taxonomy_term', $parent_term);
if (!empty($uri)) {
return array(
$term->vocabulary_machine_name => $uri['path'],
);
}
}
}
}
}