Created
April 18, 2013 17:06
-
-
Save davereid/5414416 to your computer and use it in GitHub Desktop.
Custom field default value functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Custom field default value function for entity reference fields. | |
*/ | |
function entityreference_get_default_handler_value($entity_type, $entity, $field, $instance, $langcode) { | |
if (!isset($instance['settings']['default_handler']) || !isset($instance['settings']['default_handler_settings'])) { | |
return array(); | |
} | |
// Merge the default handler settings back into the normal field settings for | |
// the purpose of using the handler. | |
$field['settings']['handler'] = $instance['settings']['default_handler']; | |
$field['settings']['handler_settings'] = $instance['settings']['default_handler_settings']; | |
$return = array(); | |
if ($handler = entityreference_get_selection_handler($field, $instance, $entity_type, $entity)) { | |
$ids = array(); | |
$results = $handler->getReferencableEntities(); | |
foreach ($results as $bundle => $result_ids) { | |
$ids = array_merge($ids, array_keys($result_ids)); | |
} | |
$ids = array_unique($ids); | |
foreach ($ids as $id) { | |
$return[] = array('target_id' => $id); | |
} | |
} | |
return $return; | |
} | |
/** | |
* For a number field, use default value of maximum + 1. | |
*/ | |
function number_integer_default_value_max_plus_one($entity_type, $entity, $field, $instance, $langcode) { | |
if ($field['storage']['type'] == 'field_sql_storage') { | |
$table = _field_sql_storage_tablename($field); | |
$column = _field_sql_storage_columnname($field['field_name'], 'value'); | |
$query = db_select($table); | |
$query->addExpression('MAX(' . $column . ')'); | |
$max = (int) $query->execute()->fetchField(); | |
return $max + 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment