EntityFindSomething.php in Crumbs, the Breadcrumbs suite 7.2
File
lib/MultiPlugin/EntityFindSomething.php
View source
<?php
class crumbs_MultiPlugin_EntityFindSomething extends crumbs_MultiPlugin_EntityFindAbstract {
protected $entityType;
protected $bundleKey;
protected $bundleName;
protected $weights = array();
function __construct($plugin, $entity_type, $bundle_key, $bundle_name) {
$this->entityType = $entity_type;
$this->bundleKey = $bundle_key;
$this->bundleName = $bundle_name;
parent::__construct($plugin);
}
function initWeights($localWeightMap) {
if ('user' !== $this->entityType) {
return;
}
foreach (user_roles(TRUE) as $rid => $role) {
$weight = $localWeightMap
->valueAtKey($role);
if (FALSE !== $weight) {
$this->weights[$rid] = $weight;
}
}
asort($this->weights);
}
function describe($api) {
return $this
->describeGeneric($api, $this->entityType, t($this->bundleName));
}
protected function find($path, $item) {
if (FALSE === ($entity = crumbs_Util::itemExtractEntity($item, $this->entityType))) {
return NULL;
}
if ('user' === $this->entityType) {
return $this
->userFind($entity);
}
else {
return $this
->entityFind($entity);
}
}
protected function entityFind(stdClass $entity) {
if (!empty($this->bundleKey) && !empty($entity->{$this->bundleKey})) {
$distinction_key = $entity->{$this->bundleKey};
}
else {
$distinction_key = $this->entityType;
}
$parent = $this->plugin
->entityFindCandidate($entity, $this->entityType, $distinction_key);
if (!empty($parent)) {
return array(
$distinction_key => $parent,
);
}
return NULL;
}
protected function userFind(stdClass $user) {
$candidates = array();
foreach ($this->weights as $rid => $weight) {
if (!empty($user->roles[$rid])) {
$role = $user->roles[$rid];
$parent = $this->plugin
->entityFindCandidate($user, 'user', $role);
if (!empty($parent)) {
$candidates[$role] = $parent;
}
}
}
return $candidates;
}
}