first import
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
class menu_token_entity_context implements menu_token_handler {
|
||||
function form_options($options) {
|
||||
// Nothing to do here.
|
||||
}
|
||||
|
||||
function form_submit($form, &$form_state) {
|
||||
// Nothing to do here.
|
||||
}
|
||||
|
||||
function form_validate($form, &$form_state) {
|
||||
// Nothing to do here.
|
||||
}
|
||||
|
||||
function form_alter(&$form, &$form_state) {
|
||||
// Nothing to do here.
|
||||
}
|
||||
|
||||
function object_load($options) {
|
||||
$entity_type = $options['_type'];
|
||||
$entity_info = entity_get_info($entity_type);
|
||||
|
||||
// Taken from entity_uri() to determine the uri callback for this entity
|
||||
// type.
|
||||
// A bundle-specific callback takes precedence over the generic one for the
|
||||
// entity type.
|
||||
if (isset($entity_info['uri callback'])) {
|
||||
$uri_callback = $entity_info['uri callback'];
|
||||
}
|
||||
else {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
$path = @$uri_callback();
|
||||
$path = current(explode('/', $path['path']));
|
||||
|
||||
if (arg(0) === $path && is_numeric(arg(1))) {
|
||||
return entity_load_single($entity_type, arg(1));
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
class menu_token_entity_random implements menu_token_handler {
|
||||
function form_options($options) {
|
||||
// Nothing to do here.
|
||||
}
|
||||
|
||||
function form_submit($form, &$form_state) {
|
||||
// Nothing to do here.
|
||||
}
|
||||
|
||||
function form_validate($form, &$form_state) {
|
||||
// Nothing to do here.
|
||||
}
|
||||
|
||||
function form_alter(&$form, &$form_state) {
|
||||
// Nothing to do here.
|
||||
}
|
||||
|
||||
function object_load($options) {
|
||||
$entity_type = $options['_type'];
|
||||
$info = entity_get_info($entity_type);
|
||||
|
||||
$id =
|
||||
db_select($info['base table'], 'e')
|
||||
->fields('e', array($info['entity keys']['id']))
|
||||
->orderRandom()
|
||||
->range(0, 1)
|
||||
->execute()
|
||||
->fetchField(0);
|
||||
|
||||
if ($id) {
|
||||
return entity_load_single($entity_type, $id);
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
class menu_token_entity_user_defined implements menu_token_handler {
|
||||
function form_options($options) {
|
||||
$output['menu_token_entity_user_defined'] = array(
|
||||
'#title' => t('Entity ID'),
|
||||
'#description' => t('The id of the entity that this token handler should load.'),
|
||||
'#type' => 'textfield',
|
||||
'#default_value' => isset($options['id']) ? $options['id'] : '',
|
||||
);
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
function form_submit($form, &$form_state) {
|
||||
$options['id'] = $form_state['values']['menu_token_entity_user_defined'];
|
||||
return $options;
|
||||
}
|
||||
|
||||
function form_validate($form, &$form_state) {
|
||||
$id = $form_state['values']['menu_token_entity_user_defined'];
|
||||
$entity_type = $form_state['_menu_token_entity_type'];
|
||||
|
||||
if (!is_numeric($id)) {
|
||||
form_set_error('menu_token_entity_user_defined', t('Entity ID should be numeric.'));
|
||||
return;
|
||||
}
|
||||
else if (!entity_load_single($entity_type, $id)) {
|
||||
form_set_error('menu_token_entity_user_defined', t('Entity should exist.'));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
function form_alter(&$form, &$form_state) {
|
||||
// Nothing to do here.
|
||||
}
|
||||
|
||||
function object_load($options) {
|
||||
$entity_type = $options['_type'];
|
||||
|
||||
if (is_numeric($options['id'])) {
|
||||
return entity_load_single($entity_type, $options['id']);
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user