本文整理汇总了PHP中entity_load_single函数的典型用法代码示例。如果您正苦于以下问题:PHP entity_load_single函数的具体用法?PHP entity_load_single怎么用?PHP entity_load_single使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了entity_load_single函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getTokenFromEntity
/**
* Get the token string from the token entity.
*
* @param int $token_id
* The restful_token_auth entity.
*
* @return string
* The token string.
*/
public static function getTokenFromEntity($token_id)
{
if ($token = entity_load_single('restful_token_auth', $token_id)) {
return $token->token;
}
return NULL;
}
示例2: getLabel
/**
* Overrides EntityReferenceHandler::getLabel().
*/
public function getLabel($entity) {
$target_type = $this->field['settings']['target_type'];
$field = $entity->{OG_AUDIENCE_FIELD};
$gid = $field[LANGUAGE_NONE][0]['target_id'];
$group = entity_load_single('node', $gid);
return entity_label($target_type, $entity).' ('.entity_label('node', $group).')';
}
示例3: authenticate
/**
* {@inheritdoc}
*/
public function authenticate(RequestInterface $request)
{
// Access token may be on the request, or in the headers.
if (!($token = $this->extractToken($request))) {
return NULL;
}
// Check if there is a token that has not expired yet.
$query = new \EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'restful_token_auth')->entityCondition('bundle', 'access_token')->propertyCondition('token', $token)->range(0, 1)->execute();
if (empty($result['restful_token_auth'])) {
// No token exists.
return NULL;
}
$id = key($result['restful_token_auth']);
$auth_token = entity_load_single('restful_token_auth', $id);
if (!empty($auth_token->expire) && $auth_token->expire < REQUEST_TIME) {
// Token is expired.
if (variable_get('restful_token_auth_delete_expired_tokens', TRUE)) {
// Token has expired, so we can delete this token.
$auth_token->delete();
}
return NULL;
}
return user_load($auth_token->uid);
}
示例4: refreshToken
/**
* Create a token for a user, and return its value.
*
* @param string $token
* The refresh token.
*
* @throws BadRequestException
*
* @return RestfulTokenAuth
* The new access token.
*/
public function refreshToken($token)
{
// Check if there is a token that did not expire yet.
/* @var \Drupal\restful\Plugin\resource\DataProvider\DataProviderEntityInterface $data_provider */
$data_provider = $this->getDataProvider();
$query = $data_provider->EFQObject();
$results = $query->entityCondition('entity_type', $this->entityType)->entityCondition('bundle', 'refresh_token')->propertyCondition('token', $token)->range(0, 1)->execute();
if (empty($results['restful_token_auth'])) {
throw new BadRequestException('Invalid refresh token.');
}
// Remove the refresh token once used.
$refresh_token = entity_load_single('restful_token_auth', key($results['restful_token_auth']));
$uid = $refresh_token->uid;
// Get the access token linked to this refresh token then do some cleanup.
$access_token_query = new EntityFieldQuery();
$access_token_reference = $access_token_query->entityCondition('entity_type', 'restful_token_auth')->entityCondition('bundle', 'access_token')->fieldCondition('refresh_token_reference', 'target_id', $refresh_token->id)->range(0, 1)->execute();
if (!empty($access_token_reference['restful_token_auth'])) {
$access_token = key($access_token_reference['restful_token_auth']);
entity_delete('restful_token_auth', $access_token);
}
$refresh_token->delete();
// Create the new access token and return it.
/* @var \Drupal\restful_token_auth\Entity\RestfulTokenAuthController $controller */
$controller = entity_get_controller($this->getEntityType());
$token = $controller->generateAccessToken($uid);
return $this->view($token->id);
}
示例5: getOrCreateToken
/**
* Create a token for a user, and return its value.
*/
public function getOrCreateToken()
{
$entity_type = $this->getEntityType();
$account = $this->getAccount();
// Check if there is a token that did not expire yet.
/* @var DataProviderEntityInterface $data_provider */
$data_provider = $this->getDataProvider();
$query = $data_provider->EFQObject();
$result = $query->entityCondition('entity_type', $entity_type)->entityCondition('bundle', 'access_token')->propertyCondition('uid', $account->uid)->range(0, 1)->execute();
$token_exists = FALSE;
if (!empty($result[$entity_type])) {
$id = key($result[$entity_type]);
$access_token = entity_load_single($entity_type, $id);
$token_exists = TRUE;
if (!empty($access_token->expire) && $access_token->expire < REQUEST_TIME) {
if (variable_get('restful_token_auth_delete_expired_tokens', TRUE)) {
// Token has expired, so we can delete this token.
$access_token->delete();
}
$token_exists = FALSE;
}
}
if (!$token_exists) {
/* @var \Drupal\restful_token_auth\Entity\RestfulTokenAuthController $controller */
$controller = entity_get_controller($this->getEntityType());
$access_token = $controller->generateAccessToken($account->uid);
$id = $access_token->id;
}
$output = $this->view($id);
return $output;
}
示例6: groupAudiencegetDiff
/**
* Get the difference in group audience for a saved field.
*
* @return
* Array with all the differences, or an empty array if none found.
*/
public function groupAudiencegetDiff($entity_type, $entity, $field, $instance, $langcode, $items)
{
$return = FALSE;
$field_name = $field['field_name'];
$wrapper = entity_metadata_wrapper($entity_type, $entity);
$og_memberships = $wrapper->{$field_name . '__og_membership'}->value();
$new_memberships = array();
foreach ($items as $item) {
$new_memberships[$item['target_id']] = TRUE;
}
foreach ($og_memberships as $og_membership) {
$gid = $og_membership->gid;
if (empty($new_memberships[$gid])) {
// Membership was deleted.
if ($og_membership->entity_type == 'user') {
// Make sure this is not the group manager, if exists.
$group = entity_load_single($og_membership->group_type, $og_membership->gid);
if (!empty($group->uid) && $group->uid == $og_membership->etid) {
continue;
}
}
$return['delete'][] = $og_membership->id;
unset($new_memberships[$gid]);
} else {
// Existing membership.
unset($new_memberships[$gid]);
}
}
if ($new_memberships) {
// New memberships.
$return['insert'] = array_keys($new_memberships);
}
return $return;
}
示例7: getBillingCycle
/**
* Returns the user's billing cycle with the provided start time.
*
* If an existing billing cycle matches the expected start and end, it will
* be returned. Otherwise, a new one will be created.
*
* @param $uid
* The uid of the user.
* @param $start
* The unix timestamp when the billing cycle needs to start.
* @param $save
* Whether to save the created billing cycle entity.
* Passing FALSE allows an unsaved billing cycle entity to be returned
* for estimation purposes.
*
* @return
* A cl_billing_cycle entity.
*/
public function getBillingCycle($uid, $start = REQUEST_TIME, $save = TRUE)
{
// Make the billing cycle exactly 30 days long, so that it can be divided
// predictably for prorating.
// The 1 is substracted to make sure that the billing cycle ends 1s before
// the next one starts
$end = $start + 2592000 - 1;
// Try to find an existing billing cycle matching our parameters.
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'cl_billing_cycle')->entityCondition('bundle', $this->name)->propertyCondition('status', 1)->propertyCondition('uid', $uid);
if ($start != REQUEST_TIME) {
// In case of a custom start, make sure to match the exact billing cycle.
// Ensures that new orders get the previous billing cycle created at the
// start of testing, while getNextBillingCycle returns the expected result.
$query->propertyCondition('start', $start);
}
$result = $query->execute();
if ($result) {
$billing_cycle_id = key($result['cl_billing_cycle']);
$billing_cycle = entity_load_single('cl_billing_cycle', $billing_cycle_id);
} else {
// No existing billing cycle found. Create a new one.
$billing_cycle = entity_create('cl_billing_cycle', array('type' => $this->name));
$billing_cycle->uid = $uid;
$billing_cycle->start = $start;
$billing_cycle->end = $end;
$billing_cycle->status = 1;
if ($save) {
$billing_cycle->save();
}
}
return $billing_cycle;
}
示例8: validate
/**
* {@inheritdoc}
*/
public function validate(&$value)
{
if (!empty($value['entity_type']) && !empty($value['entity_id'])) {
$entity = entity_load_single($value['entity_type'], $value['entity_id']);
if (!$entity) {
$entity_types = $this->getEntityTypeOptions();
return array(t('@entity_type with id @entity_id does not exist.', array('@entity_type' => $entity_types[$value['entity_type']], '@entity_id' => $value['entity_id'])));
}
$wrapper = entity_metadata_wrapper($value['entity_type'], $value['entity_id']);
return parent::validate($wrapper);
}
}
示例9: fetchSingle
public function fetchSingle()
{
$query = $this->getEntityFieldQuery();
$result = $query->execute();
if (empty($result[$this->entityType])) {
return null;
} else {
$keys = array_keys($result[$this->entityType]);
$id = array_shift($keys);
return entity_load_single($this->entityType, $id);
}
}
示例10: exportContextResults
/**
* Sends the context messages back to Drupal's messaging system using
* drupal_set_message.
*
* Please note that this is meant to be called before using drupal_goto()
* to redirect the user to a result page.
*
* @param array $results The context from the batch operation.
*/
public static function exportContextResults(array $results)
{
foreach ($results as $result) {
if ($result['entity_id'] && $result['entity_type']) {
$message = ':label [<strong>:type</strong> <code>:id</code>] !message';
$arguments = array(':label' => entity_label($result['entity_type'], entity_load_single($result['entity_type'], $result['entity_id'])), ':type' => $result['entity_type'], ':id' => $result['entity_id'], '!message' => $result['message']);
} else {
$message = '!message';
$arguments = array('!message' => $result['message']);
}
drupal_set_message(t($message, $arguments), $result['type']);
}
}
示例11: validate
/**
* Implements FeedsEntityProcessorPropertyInterface::validate().
*/
public function validate(&$value)
{
$info = $this->getPropertInfo();
$entity_type = $info['type'];
if ($value) {
$entity = entity_load_single($entity_type, $value);
if (!$entity) {
$entity_info = entity_get_info();
$entity_type_label = $entity_info[$entity_type]['label'];
return array(t('@entity_type with id @entity_id does not exist.', array('@entity_type' => $entity_type_label, '@entity_id' => $value)));
}
$wrapper = entity_metadata_wrapper($entity_type, $value);
return parent::validate($wrapper);
}
}
示例12: view
/**
* Displays the bean.
*/
public function view($bean, $content, $view_mode = 'default', $langcode = NULL)
{
// Retrieve the terms from the loaded entity.
$active_entity = bean_tax_active_entity_array();
// Check for cached content on this block.
$cache_name = 'bean_tax:listing:' . $bean->delta . ':' . $active_entity['type'] . ':' . $active_entity['ids'][0];
if ($cache = cache_get($cache_name)) {
$content = $cache->data;
} else {
// We need to make sure that the bean is configured correctly.
if ($active_entity['type'] != 'bean' && !empty($bean->filters['vocabulary']) && (isset($active_entity['terms']) && count($active_entity['terms']))) {
// Reformat vocabulary list from machine names to vocabulary vids.
$vids = array();
foreach ($bean->filters['vocabulary'] as $vm) {
$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'taxonomy_vocabulary');
$query->propertyCondition('machine_name', $vm);
global $language;
if ($language->language != NULL && db_field_exists('taxonomy_vocabulary', 'language')) {
$query->propertyCondition('language', $language->language);
}
$result = $query->execute();
foreach ($result['taxonomy_vocabulary'] as $vocabulary) {
$vids[$vocabulary->vid] = $vocabulary->vid;
}
}
$i = 0;
$content['terms'] = array();
// Parse terms from correct vocabularies, limit list to X results.
foreach ($active_entity['terms'] as $term) {
$term = entity_load_single('taxonomy_term', $term->tid);
if (in_array($term->vid, $vids) && $i < $bean->settings['records_shown']) {
$content['terms'][$term->tid] = entity_view('taxonomy_term', array($term->tid => $term), $bean->settings['term_view_mode']);
$i++;
}
}
cache_set($cache_name, $content, 'cache', time() + 60 * $bean->settings['cache_duration']);
} elseif (isset($active_entity['type']) && $active_entity['type'] == 'bean' && $bean->bid === $active_entity['object']->bid) {
$content['#markup'] = '';
} elseif ($bean->settings['hide_empty'] || !$active_entity['object']) {
return;
} else {
$content['#markup'] = t('No terms.');
}
}
return $content;
}
示例13: refreshToken
/**
* Create a token for a user, and return its value.
*
* @param string $token
* The refresh token.
*
* @throws RestfulBadRequestException
*
* @return \RestfulTokenAuth
* The new access token.
*/
public function refreshToken($token) {
// Check if there is a token that did not expire yet.
$query = new EntityFieldQuery();
$results = $query
->entityCondition('entity_type', $this->entityType)
->entityCondition('bundle', 'refresh_token')
->propertyCondition('token', $token)
->range(0, 1)
->execute();
if (empty($results['restful_token_auth'])) {
throw new \RestfulBadRequestException('Invalid refresh token.');
}
// Remove the refresh token once used.
$refresh_token = entity_load_single('restful_token_auth', key($results['restful_token_auth']));
$uid = $refresh_token->uid;
// Get the access token linked to this refresh token then do some cleanup.
$access_token_query = new EntityFieldQuery();
$access_token_reference = $access_token_query
->entityCondition('entity_type', $this->getEntityType())
->entityCondition('bundle', $this->getBundle())
->fieldCondition('refresh_token_reference', 'target_id', $refresh_token->id)
->range(0, 1)
->execute();
if (!empty($access_token_reference['restful_token_auth'])) {
$access_token_id = key($access_token_reference['restful_token_auth']);
entity_delete('restful_token_auth', $access_token_id);
}
$refresh_token->delete();
// Create the new access token and return it.
$controller = entity_get_controller($this->getEntityType());
$token = $controller->generateAccessToken($uid);
return $this->viewEntity($token->id);
}
示例14: authenticate
/**
* {@inheritdoc}
*/
public function authenticate(array $request = array(), $method = \RestfulInterface::GET) {
$options = $this->getPluginKey('options');
$key_name = !empty($options['param_name']) ? $options['param_name'] : 'access_token';
$token = !empty($request['__application'][$key_name]) ? $request['__application'][$key_name] : $request[$key_name];
// Check if there is a token that did not expire yet.
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'restful_token_auth')
->entityCondition('bundle', 'access_token')
->propertyCondition('token', $token)
->range(0, 1)
->execute();
if (empty($result['restful_token_auth'])) {
// No token exists.
return;
}
$id = key($result['restful_token_auth']);
$auth_token = entity_load_single('restful_token_auth', $id);
if (!empty($auth_token->expire) && $auth_token->expire < REQUEST_TIME) {
// Token is expired.
if (variable_get('restful_token_auth_delete_expired_tokens', TRUE)) {
// Token has expired, so we can delete this token.
$auth_token->delete();
}
return;
}
return user_load($auth_token->uid);
}
示例15: refreshToken
/**
* Create a token for a user, and return its value.
*
* @param string $token
* The refresh token.
*
* @throws RestfulBadRequestException
*
* @return \RestfulTokenAuth
* The new access token.
*/
public function refreshToken($token) {
$account = $this->getAccount();
// Check if there is a token that did not expire yet.
$query = new EntityFieldQuery();
$results = $query
->entityCondition('entity_type', $this->entityType)
->entityCondition('bundle', 'refresh_token')
->propertyCondition('token', $token)
->range(0, 1)
->execute();
if (empty($results['restful_token_auth'])) {
throw new \RestfulBadRequestException('Invalid refresh token.');
}
// Remove the refresh token once used.
$refresh_token = entity_load_single('restful_token_auth', key($results['restful_token_auth']));
$refresh_token->delete();
// Create the new access token and return it.
$controller = entity_get_controller($this->getEntityType());
$token = $controller->generateAccessToken($account->uid);
return $this->viewEntity($token->id);
}