View source
<?php
class EntityHelper {
public static function loadSingle($entity_type, $entity_id) {
$entities = entity_load($entity_type, array(
$entity_id,
));
return reset($entities);
}
public static function loadRevision($entity_type, $entity_id, $revision_id) {
$conditions = array();
if ($revision_key = static::entityTypeHasProperty($entity_type, array(
'entity keys',
'revision',
))) {
$conditions[$revision_key] = $revision_id;
}
$entities = entity_load($entity_type, array(
$entity_id,
), $conditions);
return reset($entities);
}
public static function loadByCondition($entity_type, array $conditions) {
$entities = entity_load($entity_type, FALSE, $conditions);
return reset($entities);
}
public static function queryIdsByLabel($entity_type, $label, $bundle = NULL, $query = NULL) {
$info = entity_get_info($entity_type);
if (empty($info['entity keys']['label'])) {
throw new Exception("Unable to load entities of type {$entity_type} by label.");
}
if (!isset($query)) {
$query = new EntityFieldQuery();
$query
->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT');
}
$query
->entityCondition('entity_type', $entity_type);
if (isset($bundle)) {
$query
->entityCondition('bundle', $bundle);
}
$query
->propertyCondition($info['entity keys']['label'], $label);
$results = $query
->execute();
if (!empty($results[$entity_type])) {
return array_keys($results[$entity_type]);
}
else {
return array();
}
}
public static function queryIdByLabel($entity_type, $label, $bundle = NULL, $query = NULL) {
$ids = static::queryIdsByLabel($entity_type, $label, $bundle, $query);
return reset($ids);
}
public static function entityTypeHasProperty($entity_type, array $parents) {
if ($info = entity_get_info($entity_type)) {
return drupal_array_get_nested_value($info, $parents);
}
}
public static function removeEmptyFieldValues($entity_type, $entity) {
$form = $form_state = array();
_field_invoke_default('submit', $entity_type, $entity, $form, $form_state);
}
public static function removeInvalidFieldDeltas($entity_type, $entity) {
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
$instances = field_info_instances($entity_type, $bundle);
foreach (array_keys($instances) as $field_name) {
if (!empty($entity->{$field_name})) {
$field = field_info_field($field_name);
if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED) {
foreach ($entity->{$field_name} as $langcode => $items) {
if (count($items) > $field['cardinality']) {
$entity->{$field_name}[$langcode] = array_slice($items, 0, $field['cardinality']);
}
}
}
}
}
}
public static function updateFieldValues($entity_type, $entity) {
list($id) = entity_extract_ids($entity_type, $entity);
if (empty($id)) {
throw new InvalidArgumentException(t('Cannot call EntityHelper::updateFieldValues() on an unsaved entity.'));
}
if (!isset($entity->original)) {
$entity->original = $entity;
}
unset($entity->revision);
field_attach_presave($entity_type, $entity);
field_attach_update($entity_type, $entity);
entity_get_controller($entity_type)
->resetCache(array(
$id,
));
}
public static function updateFieldValuesStorage($entity_type, $entity, array $fields = array()) {
list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
if (empty($id)) {
throw new InvalidArgumentException(t('Cannot call EntityHelper::updateFieldValues() on an unsaved entity.'));
}
$skip_fields = array();
foreach (module_implements('field_storage_pre_update') as $module) {
$function = $module . '_field_storage_pre_update';
$function($entity_type, $entity, $skip_fields);
}
$storages = array();
foreach (field_info_instances($entity_type, $bundle) as $instance) {
$field = field_info_field_by_id($instance['field_id']);
$field_id = $field['id'];
$field_name = $field['field_name'];
if (!empty($fields) && !in_array($field_name, $fields)) {
continue;
}
if (isset($entity->{$field_name}) || property_exists($entity, $field_name)) {
if (!isset($skip_fields[$field_id])) {
$storages[$field['storage']['type']][$field_id] = $field_id;
}
}
}
foreach ($storages as $storage => $storage_fields) {
$storage_info = field_info_storage_types($storage);
module_invoke($storage_info['module'], 'field_storage_write', $entity_type, $entity, FIELD_STORAGE_UPDATE, $storage_fields);
}
entity_get_controller($entity_type)
->resetCache(array(
$id,
));
}
public static function updateBaseTableValues($entity_type, $entity, array $fields = array(), $save_revision = TRUE) {
$entity_info = entity_get_info($entity_type);
if (empty($entity_info['base table'])) {
throw new Exception("Unable to update base tables for entity type {$entity_type}.");
}
list($id, $revision_id) = entity_extract_ids($entity_type, $entity);
if (empty($id)) {
throw new Exception("Unable to update base tables for an unsaved entity.");
}
$base_values = array();
foreach ($entity_info['schema_fields_sql']['base table'] as $field_name) {
if (!empty($fields) && !in_array($field_name, $fields)) {
continue;
}
if (property_exists($entity, $field_name)) {
$base_values[$field_name] = $entity->{$field_name};
}
}
if (!empty($base_values)) {
db_update($entity_info['base table'])
->fields($base_values)
->condition($entity_info['entity keys']['id'], $id)
->execute();
}
if ($save_revision && !empty($revision_id) && !empty($entity_info['revision table']) && !empty($entity_info['entity keys']['revision'])) {
$revision_values = array();
foreach ($entity_info['schema_fields_sql']['revision table'] as $field_name) {
if (!empty($fields) && !in_array($field_name, $fields)) {
continue;
}
if (property_exists($entity, $field_name)) {
$revision_values[$field_name] = $entity->{$field_name};
}
}
if (!empty($revision_values)) {
db_update($entity_info['revision table'])
->fields($revision_values)
->condition($entity_info['entity keys']['id'], $id)
->condition($entity_info['entity keys']['revision'], $revision_id)
->execute();
}
}
entity_get_controller($entity_type)
->resetCache(array(
$id,
));
}
public static function getAllRevisionIDs($entity_type, $entity_id) {
$info = entity_get_info($entity_type);
if (empty($info['entity keys']['id']) || empty($info['entity keys']['revision']) || empty($info['revision table'])) {
return array();
}
$id_key = $info['entity keys']['id'];
$revision_key = $info['entity keys']['revision'];
$query = db_select($info['revision table'], 'revision');
$query
->addField('revision', $revision_key);
$query
->condition('revision.' . $id_key, $entity_id);
return $query
->execute()
->fetchCol();
}
public static function getOperationLinks($entity_type, $entity, $base_path = NULL) {
$build = array(
'#theme' => 'links__operations__' . $entity_type,
'#links' => array(),
'#attributes' => array(
'class' => array(
'links inline',
),
),
);
list($entity_id) = entity_extract_ids($entity_type, $entity);
if (!isset($base_path)) {
$uri = entity_uri($entity_type, $entity);
if (empty($uri['path'])) {
return array();
}
$base_path = preg_replace('/\\/' . preg_quote($entity_id) . '\\b.*/', '', $uri['path']);
}
$items = menu_contextual_links($entity_type, $base_path, array(
$entity_id,
));
$links = array();
foreach ($items as $class => $item) {
$class = drupal_html_class($class);
$links[$class] = array(
'title' => $item['title'],
'href' => $item['href'],
);
$item['localized_options'] += array(
'query' => array(),
);
$item['localized_options']['query'] += drupal_get_destination();
$links[$class] += $item['localized_options'];
}
$build['#links'] = $links;
drupal_alter('contextual_links_view', $build, $items);
if (empty($links)) {
$build['#printed'] = TRUE;
}
return $build;
}
public static function getBundleLabel($entity_type, $bundle) {
$info = entity_get_info($entity_type);
return !empty($info['bundles'][$bundle]['label']) ? $info['bundles'][$bundle]['label'] : FALSE;
}
public static function getBundleOptions($entity_type) {
$info = entity_get_info($entity_type);
return !empty($info['bundles']) ? ArrayHelper::extractNestedValuesToArray($info['bundles'], array(
'label',
)) : array();
}
public static function getKey($entity_type, $entity, $key) {
$info = entity_get_info($entity_type);
if (isset($info['entity keys'][$key]) && isset($entity->{$info['entity keys'][$key]})) {
return $entity->{$info['entity keys'][$key]};
}
}
public static function getViewModeOptions($entity_type, $bundle = NULL, $include_disabled = TRUE) {
$view_modes = array();
$info = entity_get_info($entity_type);
if (!empty($info['fieldable'])) {
$view_modes['default'] = t('Default');
}
if (!empty($info['view modes'])) {
$view_modes += ArrayHelper::extractNestedValuesToArray($info['view modes'], array(
'label',
));
}
if (isset($bundle) && !$include_disabled) {
$view_mode_settings = field_view_mode_settings($entity_type, $bundle);
foreach ($view_modes as $view_mode => $label) {
if (empty($view_mode_settings[$view_mode]['custom_settings'])) {
unset($view_modes[$view_mode]);
}
}
}
return $view_modes;
}
public static function view($entity_type, $entity, $view_mode = 'default', $langcode = NULL, $page = NULL) {
if ($output = static::viewMultiple($entity_type, array(
$entity,
), $view_mode, $langcode, $page)) {
return reset($output);
}
else {
return array();
}
}
public static function viewMultiple($entity_type, array $entities, $view_mode = 'default', $langcode = NULL, $page = NULL) {
if (empty($entities)) {
return array();
}
$output = entity_view($entity_type, $entities, $view_mode, $langcode, $page);
if ($entity_type === 'file' && isset($output['files'])) {
$output = array(
'file' => reset($output),
);
}
return !empty($output[$entity_type]) ? $output[$entity_type] : array();
}
public static function filterByAccess($entity_type, array $entities, $op = 'view', $account = NULL) {
return array_filter($entities, function ($entity) use ($entity_type, $op, $account) {
return entity_access($op, $entity_type, $entity, $account);
});
}
public static function getAllReferencesTo($entity_type, array $entity_ids, EntityFieldQuery $query = NULL, $flatten = FALSE) {
if (!isset($query)) {
$query = new EntityFieldQuery();
$query
->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT');
}
$references = array();
$fields = FieldHelper::getEntityReferencingFieldsByType($entity_type);
foreach ($fields as $field_name => $columns) {
foreach (array_keys($columns) as $column) {
$field_query = clone $query;
$field_query
->fieldCondition($field_name, $column, $entity_ids);
if ($results = $field_query
->execute()) {
if ($flatten) {
$references = drupal_array_merge_deep($references, $results);
}
else {
$references[$field_name . ':' . $column] = $results;
}
}
}
}
return $references;
}
public static function isPubliclyVisible($entity_type, $entity, array $options = array()) {
$options += array(
'needs alias' => FALSE,
);
$uri = entity_uri($entity_type, $entity);
if (empty($uri['path'])) {
return FALSE;
}
elseif ($options['needs alias'] && !drupal_lookup_path('alias', $uri['path'], NULL)) {
return FALSE;
}
elseif (module_exists('rabbit_hole') && rabbit_hole_get_action($entity_type, $entity) !== RABBIT_HOLE_DISPLAY_CONTENT) {
return FALSE;
}
else {
return entity_access('view', $entity_type, $entity, drupal_anonymous_user());
}
}
public static function deleteMultiple($entity_type, array $ids) {
$function = $entity_type . '_delete_multiple';
if (function_exists($function)) {
return $function($ids);
}
else {
return entity_delete_multiple($entity_type, $ids);
}
}
}