Commit 942159bb authored by Eric Rasmussen's avatar Eric Rasmussen
Browse files

[gh-642] Update Entity API and Entity Reference to 7.x-1.x

parent 61b25013
......@@ -25,9 +25,9 @@ files[] = views/handlers/entity_views_handler_field_uri.inc
files[] = views/handlers/entity_views_handler_relationship_by_bundle.inc
files[] = views/handlers/entity_views_handler_relationship.inc
files[] = views/plugins/entity_views_plugin_row_entity_view.inc
; Information added by drupal.org packaging script on 2012-09-07
version = "7.x-1.0-rc3+10-dev"
; Information added by drupal.org packaging script on 2012-12-25
version = "7.x-1.0"
core = "7.x"
project = "entity"
datestamp = "1346977698"
datestamp = "1356471145"
......@@ -1333,15 +1333,6 @@ function entity_module_implements_alter(&$implementations, $hook) {
function entity_entity_info_alter(&$entity_info) {
_entity_info_add_metadata($entity_info);
// Modules are using entity_load() in hook_entity_info_alter() to add
// their bundle-info. This might trigger the field API cache to be built,
// which makes use of the bundle-info. In that case the field-info cache would
// have been built with in-complete bundle-info, thus we clear it so it is
// rebuilt with complete bundle-info the next time field-info is requested.
_field_info_collate_fields(TRUE);
// To be sure, also clear the property info cache as it depends on field info.
entity_property_info_cache_clear();
// Populate a default value for the 'configuration' key of all entity types.
foreach ($entity_info as $type => $info) {
if (!isset($info['configuration'])) {
......
......@@ -5,9 +5,9 @@ files[] = entity_token.tokens.inc
files[] = entity_token.module
dependencies[] = entity
; Information added by drupal.org packaging script on 2012-09-07
version = "7.x-1.0-rc3+10-dev"
; Information added by drupal.org packaging script on 2012-12-25
version = "7.x-1.0"
core = "7.x"
project = "entity"
datestamp = "1346977698"
datestamp = "1356471145"
......@@ -292,10 +292,11 @@ function entity_metadata_taxonomy_term_get_properties($term, array $options, $na
function entity_metadata_taxonomy_term_setter($term, $name, $value) {
switch ($name) {
case 'vocabulary':
// Make sure to also update the taxonomy bundle key.
$vocabulary = taxonomy_vocabulary_load($value);
// Make sure to update the taxonomy bundle key, so load the vocabulary.
// Support both, loading by name or ID.
$vocabulary = is_numeric($value) ? taxonomy_vocabulary_load($value) : taxonomy_vocabulary_machine_name_load($value);
$term->vocabulary_machine_name = $vocabulary->machine_name;
return $term->vid = $value;
return $term->vid = $vocabulary->vid;
case 'parent':
return $term->parent = $value;
}
......@@ -483,7 +484,7 @@ function entity_metadata_field_verbatim_get($entity, array $options, $name, $ent
*/
function entity_metadata_field_verbatim_set($entity, $name, $items, $langcode, $entity_type) {
$field = field_info_field($name);
$langcode = entity_metadata_field_get_language($entity_type, $entity, $entity_type, $langcode);
$langcode = entity_metadata_field_get_language($entity_type, $entity, $field, $langcode);
$value = $field['cardinality'] == 1 ? array($items) : (array) $items;
// Filter out any items set to NULL.
$entity->{$name}[$langcode] = array_filter($value);
......@@ -508,8 +509,13 @@ function entity_metadata_field_verbatim_set($entity, $name, $items, $langcode, $
*/
function entity_metadata_field_get_language($entity_type, $entity, $field, $langcode = LANGUAGE_NONE, $fallback = FALSE) {
// Try to figure out the default language used by the entity.
// @todo: Update once http://drupal.org/node/1260640 has been fixed.
$default_langcode = !empty($entity->language) ? $entity->language : LANGUAGE_NONE;
// With Drupal >= 7.15 we can use entity_language().
if (function_exists('entity_language')) {
$default_langcode = entity_language($entity_type, $entity);
}
else {
$default_langcode = !empty($entity->language) ? $entity->language : LANGUAGE_NONE;
}
// Determine the right language to use.
if ($default_langcode != LANGUAGE_NONE && field_is_translatable($entity_type, $field)) {
......
......@@ -6,9 +6,9 @@ files[] = entity_feature.module
dependencies[] = entity_test
hidden = TRUE
; Information added by drupal.org packaging script on 2012-09-07
version = "7.x-1.0-rc3+10-dev"
; Information added by drupal.org packaging script on 2012-12-25
version = "7.x-1.0"
core = "7.x"
project = "entity"
datestamp = "1346977698"
datestamp = "1356471145"
......@@ -7,9 +7,9 @@ files[] = entity_test.install
dependencies[] = entity
hidden = TRUE
; Information added by drupal.org packaging script on 2012-09-07
version = "7.x-1.0-rc3+10-dev"
; Information added by drupal.org packaging script on 2012-12-25
version = "7.x-1.0"
core = "7.x"
project = "entity"
datestamp = "1346977698"
datestamp = "1356471145"
......@@ -5,9 +5,9 @@ dependencies[] = i18n_string
package = Multilingual - Internationalization
core = 7.x
hidden = TRUE
; Information added by drupal.org packaging script on 2012-09-07
version = "7.x-1.0-rc3+10-dev"
; Information added by drupal.org packaging script on 2012-12-25
version = "7.x-1.0"
core = "7.x"
project = "entity"
datestamp = "1346977698"
datestamp = "1356471145"
......@@ -158,7 +158,7 @@ class entity_views_handler_field_entity extends views_handler_field {
return '';
}
$render = $this->render_single_value($entity, $values);
if (!$this->options['link_to_entity']) {
if (!$this->options['link_to_entity'] || $this->options['display'] == 'view') {
return $render;
}
if (is_object($entity) && ($url = entity_uri($type, $entity))) {
......
<?php
/**
* @file
* Provide Diff module field callbacks for the Entity Reference module.
*/
/**
* Diff field callback for preloading entities.
*/
function entityreference_field_diff_view_prepare(&$old_items, &$new_items, $context) {
$field = $context['field'];
// Build an array of entities ID.
$entity_ids = array();
foreach (array_merge_recursive($old_items, $new_items) as $item) {
$entity_ids[] = $item['target_id'];
}
// Load those entities and loop through them to extract their labels.
$entities = entity_load($field['settings']['target_type'], $entity_ids);
foreach ($old_items as $delta => $info) {
$old_items[$delta]['entity'] = isset($entities[$info['target_id']]) ? $entities[$info['target_id']] : NULL;
}
foreach ($new_items as $delta => $info) {
$new_items[$delta]['entity'] = isset($entities[$info['target_id']]) ? $entities[$info['target_id']] : NULL;
}
}
/**
* Diff field callback for parsing entity field comparative values.
*/
function entityreference_field_diff_view($items, $context) {
$field = $context['field'];
$instance = $context['instance'];
$settings = $context['settings'];
$entity_type = $field['settings']['target_type'];
$diff_items = array();
// We populate as much as possible to allow the best flexability in any
// string overrides.
$t_args = array();
$t_args['!entity_type'] = $entity_type;
$entity_info = entity_get_info($entity_type);
$t_args['!entity_type_label'] = $entity_info['label'];
foreach ($items as $delta => $item) {
if (isset($item['entity'])) {
$output = array();
list($id,, $bundle) = entity_extract_ids($entity_type, $item['entity']);
$t_args['!id'] = $id;
$t_args['!bundle'] = $bundle;
$t_args['!diff_entity_label'] = entity_label($entity_type, $item['entity']);
$output['entity'] = t('!diff_entity_label', $t_args);
if ($settings['show_id']) {
$output['id'] = t('ID: !id', $t_args);
}
$diff_items[$delta] = implode('; ', $output);
}
}
return $diff_items;
}
/**
* Provide default field comparison options.
*/
function entityreference_field_diff_default_options($field_type) {
return array(
'show_id' => 0,
);
}
/**
* Provide a form for setting the field comparison options.
*/
function entityreference_field_diff_options_form($field_type, $settings) {
$options_form = array();
$options_form['show_id'] = array(
'#type' => 'checkbox',
'#title' => t('Show ID'),
'#default_value' => $settings['show_id'],
);
return $options_form;
}
......@@ -19,11 +19,12 @@ files[] = views/entityreference_plugin_row_fields.inc
; Tests.
files[] = tests/entityreference.handlers.test
files[] = tests/entityreference.taxonomy.test
files[] = tests/entityreference.admin.test
; Information added by drupal.org packaging script on 2012-09-25
version = "7.x-1.0-rc5"
; Information added by drupal.org packaging script on 2012-11-18
version = "7.x-1.0"
core = "7.x"
project = "entityreference"
datestamp = "1348565045"
datestamp = "1353230808"
......@@ -9,7 +9,10 @@
* Implement hook_migrate_api().
*/
function entityreference_migrate_api() {
return array('api' => 2);
return array(
'api' => 2,
'field_handlers' => array('MigrateEntityReferenceFieldHandler'),
);
}
class MigrateEntityReferenceFieldHandler extends MigrateSimpleFieldHandler {
......
......@@ -330,6 +330,49 @@ function entityreference_field_attach_delete($entity_type, $entity) {
}
}
/**
* Implements hook_entity_insert().
*/
function entityreference_entity_insert($entity, $entity_type) {
entityreference_entity_crud($entity, $entity_type, 'entityPostInsert');
}
/**
* Implements hook_entity_update().
*/
function entityreference_entity_update($entity, $entity_type) {
entityreference_entity_crud($entity, $entity_type, 'entityPostUpdate');
}
/**
* Implements hook_entity_delete().
*/
function entityreference_entity_delete($entity, $entity_type) {
entityreference_entity_crud($entity, $entity_type, 'entityPostDelete');
}
/**
* Invoke a behavior based on entity CRUD.
*
* @param $entity
* The entity object.
* @param $entity_type
* The entity type.
* @param $method_name
* The method to invoke.
*/
function entityreference_entity_crud($entity, $entity_type, $method_name) {
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
foreach (field_info_instances($entity_type, $bundle) as $field_name => $instance) {
$field = field_info_field($field_name);
if ($field['type'] == 'entityreference') {
foreach (entityreference_get_behavior_handlers($field, $instance) as $handler) {
$handler->{$method_name}($entity_type, $entity, $field, $instance);
}
}
}
}
/**
* Implements hook_field_settings_form().
*/
......@@ -719,7 +762,21 @@ function entityreference_field_widget_settings_form($field, $instance) {
* Implements hook_options_list().
*/
function entityreference_options_list($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
return entityreference_get_selection_handler($field, $instance, $entity_type, $entity)->getReferencableEntities();
if (!$options = entityreference_get_selection_handler($field, $instance, $entity_type, $entity)->getReferencableEntities()) {
return array();
}
// Rebuild the array, by changing the bundle key into the bundle label.
$target_type = $field['settings']['target_type'];
$entity_info = entity_get_info($target_type);
$return = array();
foreach ($options as $bundle => $entity_ids) {
$bundle_label = check_plain($entity_info['bundles'][$bundle]['label']);
$return[$bundle_label] = $entity_ids;
}
return count($return) == 1 ? reset($return) : $return;
}
/**
......@@ -830,6 +887,10 @@ function _entityreference_autocomplete_validate($element, &$form_state, $form) {
// autocomplete but filled in a value manually.
$field = field_info_field($element['#field_name']);
$handler = entityreference_get_selection_handler($field);
$field_name = $element['#field_name'];
$field = field_info_field($field_name);
$instance = field_info_instance($element['#entity_type'], $field_name, $element['#bundle']);
$handler = entityreference_get_selection_handler($field, $instance);
$value = $handler->validateAutocompleteInput($element['#value'], $element, $form_state, $form);
}
}
......@@ -916,6 +977,31 @@ function entityreference_autocomplete_access_callback($type, $field_name, $entit
function entityreference_autocomplete_callback($type, $field_name, $entity_type, $bundle_name, $entity_id = '', $string = '') {
$field = field_info_field($field_name);
$instance = field_info_instance($entity_type, $field_name, $bundle_name);
return entityreference_autocomplete_callback_get_matches($type, $field, $instance, $entity_type, $entity_id, $string);
}
/**
* Return JSON based on given field, instance and string.
*
* This function can be used by other modules that wish to pass a mocked
* definition of the field on instance.
*
* @param $type
* The widget type (i.e. 'single' or 'tags').
* @param $field
* The field array defintion.
* @param $instance
* The instance array defintion.
* @param $entity_type
* The entity type.
* @param $entity_id
* Optional; The entity ID the entity-reference field is attached to.
* Defaults to ''.
* @param $string
* The label of the entity to query by.
*/
function entityreference_autocomplete_callback_get_matches($type, $field, $instance, $entity_type, $entity_id = '', $string = '') {
$matches = array();
$entity = NULL;
......@@ -925,6 +1011,7 @@ function entityreference_autocomplete_callback($type, $field_name, $entity_type,
return MENU_ACCESS_DENIED;
}
}
$handler = entityreference_get_selection_handler($field, $instance, $entity_type, $entity);
if ($type == 'tags') {
......@@ -946,15 +1033,17 @@ function entityreference_autocomplete_callback($type, $field_name, $entity_type,
$entity_labels = $handler->getReferencableEntities($tag_last, $instance['widget']['settings']['match_operator'], 10);
// Loop through the products and convert them into autocomplete output.
foreach ($entity_labels as $entity_id => $label) {
$key = "$label ($entity_id)";
// Strip things like starting/trailing white spaces, line breaks and tags.
$key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(decode_entities(strip_tags($key)))));
// Names containing commas or quotes must be wrapped in quotes.
if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
$key = '"' . str_replace('"', '""', $key) . '"';
foreach ($entity_labels as $values) {
foreach ($values as $entity_id => $label) {
$key = "$label ($entity_id)";
// Strip things like starting/trailing white spaces, line breaks and tags.
$key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(decode_entities(strip_tags($key)))));
// Names containing commas or quotes must be wrapped in quotes.
if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
$key = '"' . str_replace('"', '""', $key) . '"';
}
$matches[$prefix . $key] = '<div class="reference-autocomplete">' . $label . '</div>';
}
$matches[$prefix . $key] = '<div class="reference-autocomplete">' . $label . '</div>';
}
}
......@@ -1083,12 +1172,14 @@ function entityreference_field_formatter_prepare_view($entity_type, $entities, $
$rekey = FALSE;
foreach ($items[$id] as $delta => $item) {
// Check whether the referenced entity could be loaded and that the user has access to it.
if (isset($target_entities[$item['target_id']]) && entity_access('view', $field['settings']['target_type'], $target_entities[$item['target_id']])) {
// Check whether the referenced entity could be loaded.
if (isset($target_entities[$item['target_id']])) {
// Replace the instance value with the term data.
$items[$id][$delta]['entity'] = $target_entities[$item['target_id']];
// Check whether the user has access to the referenced entity.
$items[$id][$delta]['access'] = entity_access('view', $field['settings']['target_type'], $target_entities[$item['target_id']]);
}
// Otherwise, unset the instance value, since the entity does not exists or should not be accessible.
// Otherwise, unset the instance value, since the entity does not exist.
else {
unset($items[$id][$delta]);
$rekey = TRUE;
......@@ -1109,6 +1200,13 @@ function entityreference_field_formatter_view($entity_type, $entity, $field, $in
$result = array();
$settings = $display['settings'];
// Rebuild the items list to contain only those with access.
foreach ($items as $key => $item) {
if (empty($item['access'])) {
unset($items[$key]);
}
}
switch ($display['type']) {
case 'entityreference_label':
$handler = entityreference_get_selection_handler($field, $instance, $entity_type, $entity);
......
......@@ -4,9 +4,9 @@ core = 7.x
package = Fields
dependencies[] = entityreference
; Information added by drupal.org packaging script on 2012-09-25
version = "7.x-1.0-rc5"
; Information added by drupal.org packaging script on 2012-11-18
version = "7.x-1.0"
core = "7.x"
project = "entityreference"
datestamp = "1348565045"
datestamp = "1353230808"
<?php
/**
* @file
* CTools plugin class for the taxonomy-index behavior.
*/
/**
* Extends an entityreference field to maintain its references to taxonomy terms
* in the {taxonomy_index} table.
*
* Note, unlike entityPostInsert() and entityPostUpdate(), entityDelete()
* is not needed as cleanup is performed by taxonomy module in
* taxonomy_delete_node_index().
*/
class EntityReferenceBehavior_TaxonomyIndex extends EntityReference_BehaviorHandler_Abstract {
/**
* Overrides EntityReference_BehaviorHandler_Abstract::access().
*
* Ensure that it is only enabled for ER instances on nodes targeting
* terms, and the core variable to maintain index is enabled.
*/
public function access($field, $instance) {
if ($instance['entity_type'] != 'node' || $field['settings']['target_type'] != 'taxonomy_term') {
return;
}
if ($field['storage']['type'] !== 'field_sql_storage') {
// Field doesn't use SQL storage.
return;
}
return variable_get('taxonomy_maintain_index_table', TRUE);
}
/**
* Overrides EntityReference_BehaviorHandler_Abstract::entityPostInsert().
*
* Runs after hook_node_insert() used by taxonomy module.
*/
public function entityPostInsert($entity_type, $entity, $field, $instance) {
if ($entity_type != 'node') {
return;
}
$this->buildNodeIndex($entity);
}
/**
* Overrides EntityReference_BehaviorHandler_Abstract::entityPostUpdate().
*
* Runs after hook_node_update() used by taxonomy module.
*/
public function entityPostUpdate($entity_type, $entity, $field, $instance) {
if ($entity_type != 'node') {
return;
}
$this->buildNodeIndex($entity);
}
/**
* Builds and inserts taxonomy index entries for a given node.
*
* The index lists all terms that are related to a given node entity, and is
* therefore maintained at the entity level.
*
* @param $node
* The node object.
*
* @see taxonomy_build_node_index()
*/
protected function buildNodeIndex($node) {
// We maintain a denormalized table of term/node relationships, containing
// only data for current, published nodes.
$status = NULL;
if (variable_get('taxonomy_maintain_index_table', TRUE)) {
// If a node property is not set in the node object when node_save() is
// called, the old value from $node->original is used.
if (!empty($node->original)) {
$status = (int)(!empty($node->status) || (!isset($node->status) && !empty($node->original->status)));
$sticky = (int)(!empty($node->sticky) || (!isset($node->sticky) && !empty($node->original->sticky)));
}
else {
$status = (int)(!empty($node->status));
$sticky = (int)(!empty($node->sticky));
}
}
// We only maintain the taxonomy index for published nodes.
if ($status) {
// Collect a unique list of all the term IDs from all node fields.
$tid_all = array();
foreach (field_info_instances('node', $node->type) as $instance) {
$field_name = $instance['field_name'];
$field = field_info_field($field_name);
if (!empty($field['settings']['target_type']) && $field['settings']['target_type'] == 'taxonomy_term' && $field['storage']['type'] == 'field_sql_storage') {
// If a field value is not set in the node object when node_save() is
// called, the old value from $node->original is used.
if (isset($node->{$field_name})) {
$items = $node->{$field_name};
}
elseif (isset($node->original->{$field_name})) {
$items = $node->original->{$field_name};
}
else {
continue;
}
foreach (field_available_languages('node', $field) as $langcode) {
if (!empty($items[$langcode])) {
foreach ($items[$langcode] as $item) {
$tid_all[$item['target_id']] = $item['target_id'];
}
}
}
}
// Re-calculate the terms added in taxonomy_build_node_index() so
// we can optimize database queries.
$original_tid_all = array();
if ($field['module'] == 'taxonomy' && $field['storage']['type'] == 'field_sql_storage') {
// If a field value is not set in the node object when node_save() is
// called, the old value from $node->original is used.
if (isset($node->{$field_name})) {
$items = $node->{$field_name};
}
elseif (isset($node->original->{$field_name})) {
$items = $node->original->{$field_name};
}