crumbs.entityreference_prepopulate.inc in Crumbs, the Breadcrumbs suite 7.2
File
plugins/crumbs.entityreference_prepopulate.inc
View source
<?php
function entityreference_prepopulate_crumbs_plugins($api) {
foreach (field_info_fields() as $field_name => $field_info) {
if (0 || $field_info['type'] !== 'entityreference' || empty($field_info['bundles']['node']) || empty($field_info['settings']['target_type'])) {
continue;
}
$target_type = $field_info['settings']['target_type'];
foreach ($field_info['bundles']['node'] as $node_type) {
$instance = field_info_instance('node', $field_name, $node_type);
if (0 || empty($instance['default_value_function']) || 'entityreference_prepopulate_field_default_value' !== $instance['default_value_function']) {
continue;
}
$route = 'node/add/' . str_replace('_', '-', $node_type);
$plugin = new entityreference_prepopulate_CrumbsMonoPlugin_node($node_type, $field_name, $target_type);
$api
->routeMonoPlugin($route, "node.{$field_name}.{$node_type}", $plugin);
}
}
}
class entityreference_prepopulate_CrumbsMonoPlugin_node implements crumbs_MonoPlugin_FindParentInterface {
protected $nodeType;
protected $fieldName;
protected $targetType;
function __construct($node_type, $field_name, $target_type) {
$this->nodeType = $node_type;
$this->fieldName = $field_name;
$this->targetType = $target_type;
}
function describe($api) {
$api
->titleWithLabel(t('!field_name from request', array(
'!field_name' => '<code>?' . $this->fieldName . '=*</code>',
)), t('Parent'));
}
function findParent($path, $item) {
if (empty($_GET[$this->fieldName])) {
return NULL;
}
$v = $_GET[$this->fieldName];
if (!($v > 0)) {
return NULL;
}
$target_entities = entity_load($this->targetType, array(
$v,
));
if (empty($target_entities[$v])) {
return NULL;
}
$uri = entity_uri($this->targetType, $target_entities[$v]);
if (empty($uri['path'])) {
return NULL;
}
return $uri['path'];
}
}