You are here

function synonyms_add_entity_as_synonym in Synonyms 7

Allow to merge $synonym_entity as a synonym into $trunk_entity.

Helpful function during various merging operations. It allows you to add a synonym (where possible) into one entity, which will represent another entity in the format expected by the field in which the synonym is being added. Important note: if the cardinality limit of the field into which you are adding synonym has been reached, calling to this function will take no effect.

Parameters

object $trunk_entity: Fully loaded entity object in which the synonym is being added

string $trunk_entity_type: Entity type of $trunk_entity

string $field: Field name that should exist in $trunk_entity and have enabled the "synonyms" behavior. Into this field synonym will be added

object $synonym_entity: Fully loaded entity object which will be added as a synonym

string $synonym_entity_type: Entity type of $synonym_entity

Return value

bool Whether synonym has been successfully added

TODO: This should be shifted into Term Merge module.

File

./synonyms.module, line 831
Provide synonyms feature for Drupal entities.

Code

function synonyms_add_entity_as_synonym($trunk_entity, $trunk_entity_type, $field, $synonym_entity, $synonym_entity_type) {
  $bundle = entity_extract_ids($trunk_entity_type, $trunk_entity);
  $bundle = $bundle[2];

  // TODO: this somehow must be incorporated into synonyms_provider_field
  // submodule.
  $behavior_implementations = synonyms_behavior_get_all_enabled($trunk_entity_type, $bundle, synonyms_provider_field_provider_name(field_info_field($field)));
  if (empty($behavior_implementations)) {

    // $field either doesn't exist in the $trunk_entity or it does not have any
    // enabled behavior.
    return FALSE;
  }
  $behavior_implementation = reset($behavior_implementations);
  $behavior_implementation['object']
    ->mergeEntityAsSynonym($trunk_entity, $synonym_entity, $synonym_entity_type);
  entity_save($trunk_entity_type, $trunk_entity);
  return TRUE;
}