function safeword_entity_by_safeword_context in Safeword 7
Same name and namespace in other branches
- 8 plugins/arguments/entity_by_safeword.inc \safeword_entity_by_safeword_context()
Discover if this argument gives us the term we crave.
1 string reference to 'safeword_entity_by_safeword_context'
- entity_by_safeword.inc in plugins/
arguments/ entity_by_safeword.inc - An argument handler to load an entity using a Safeword field.
File
- plugins/
arguments/ entity_by_safeword.inc, line 31 - An argument handler to load an entity using a Safeword field.
Code
function safeword_entity_by_safeword_context($arg = NULL, $conf = NULL, $empty = FALSE) {
// If unset it wants a generic, unfilled context.
if ($empty) {
return ctools_context_create_empty('entity:' . $conf['entity_type']);
}
// There are three required values to perform this argument check.
if (empty($arg) || empty($conf['entity_type']) || empty($conf['field'])) {
return NULL;
}
// Search for entities with this machine name.
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', $conf['entity_type']);
$query
->fieldCondition($conf['field'], 'machine', $arg);
$result = $query
->execute();
// If no results were found, bail out.
if (empty($result)) {
return NULL;
}
// EFQ nests the entities two layers deep.
$entity = NULL;
foreach ($result as $entity_type => $record) {
$record = reset($record);
$record = reset($record);
$entity = entity_load($entity_type, array(
$record,
));
// If the entity couldn't be loaded, bail out.
if (empty($entity)) {
return NULL;
}
// The entity is nested one layer deep by entity_load().
$entity = reset($entity);
}
// Compile the finished results.
$context = ctools_context_create('entity:' . $entity_type, $entity);
$context->original_argument = $arg;
return $context;
}