You are here

function _message_get_referenced_fields in Message 7

Helper function for getting the fields which holds the reference to the message.

Parameters

$entity_type: The type of entity being deleted (i.e. node, user, comment).

$entity: The entity object.

1 call to _message_get_referenced_fields()
message_entity_delete in ./message.module
Implements hook_entity_delete().

File

./message.module, line 437
API functions to manipulate messages.

Code

function _message_get_referenced_fields($entity_type, $entity) {
  $entity_types = variable_get('message_delete_on_entity_delete', array(
    'node',
    'user',
    'taxonomy_term',
    'comment',
  ));
  if (!$entity_types || !in_array($entity_type, $entity_types)) {
    return;
  }
  list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  $field_names = array();

  // Search for fields in which messages referenced the deleted entity.
  foreach (field_info_fields() as $field) {
    if (empty($field['bundles']['message'])) {

      // This field isn't used in any message.
      continue;
    }

    // Only delete messages due to referenced entity or referenced
    // taxonomy term deletion.
    if ($field['type'] == 'entityreference') {

      // Check if the field references entities of the given type.
      if ($entity_type != $field['settings']['target_type']) {
        continue;
      }

      // Check if the field references specific bundles. If so, check if the
      // field references the deleted entity's bundle.
      if ($field['settings']['handler_settings']['target_bundles'] && !in_array($bundle, $field['settings']['handler_settings']['target_bundles'])) {
        continue;
      }
    }
    elseif ($field['type'] == 'taxonomy_term_reference') {
      if ($entity_type != 'taxonomy_term') {
        continue;
      }
    }
    else {
      continue;
    }
    $field_names[] = $field['field_name'];
  }
  return $field_names;
}