+110
-12
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user