FINAL suepr merge step : added all modules to this super repos
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Define interface required for extracting synonyms from field types.
|
||||
*/
|
||||
|
||||
abstract class AbstractSynonymsExtractor {
|
||||
|
||||
/**
|
||||
* Return array of supported field types for synonyms extraction.
|
||||
*
|
||||
* @return array
|
||||
* Array of Field API field types from which this class is able to extract
|
||||
* synonyms
|
||||
*/
|
||||
static public abstract function fieldTypesSupported();
|
||||
|
||||
/**
|
||||
* Extract synonyms from a field attached to an entity.
|
||||
*
|
||||
* We try to pass as many info about context as possible, however, normally
|
||||
* you will only need $items to extract the synonyms.
|
||||
*
|
||||
* @param array $items
|
||||
* Array of items
|
||||
* @param array $field
|
||||
* Array of field definition according to Field API
|
||||
* @param array $instance
|
||||
* Array of instance definition according to Field API
|
||||
* @param object $entity
|
||||
* Fully loaded entity object to which the $field and $instance with $item
|
||||
* values is attached to
|
||||
* @param string $entity_type
|
||||
* Type of the entity $entity according to Field API definition of entity
|
||||
* types
|
||||
*
|
||||
* @return array
|
||||
* Array of synonyms extracted from $items
|
||||
*/
|
||||
static public abstract function synonymsExtract($items, $field, $instance, $entity, $entity_type);
|
||||
|
||||
/**
|
||||
* Allow you to hook in during autocomplete suggestions generation.
|
||||
*
|
||||
* Allow you to include entities for autocomplete suggestion that are possible
|
||||
* candidates based on your field as a source of synonyms. This method is
|
||||
* void, however, you have to alter and add your condition to $query
|
||||
* parameter.
|
||||
*
|
||||
* @param string $tag
|
||||
* What user has typed in into autocomplete widget. Normally you would
|
||||
* run LIKE '%$tag%' on your column
|
||||
* @param EntityFieldQuery $query
|
||||
* EntityFieldQuery object where you should put your conditions to
|
||||
* @param array $field
|
||||
* Array of field definition according to Field API
|
||||
* @param array $instance
|
||||
* Array of instance definition according to Field API
|
||||
*/
|
||||
static public abstract function processEntityFieldQuery($tag, EntityFieldQuery $query, $field, $instance);
|
||||
|
||||
/**
|
||||
* Add an entity as a synonym into a field of another entity.
|
||||
*
|
||||
* Basically this method should be called when you want to add some entity
|
||||
* as a synonym to another entity (for example when you merge one entity
|
||||
* into another and besides merging want to add synonym of the merging
|
||||
* entity into the trunk entity). You should extract synonym value (according
|
||||
* to what value is expected in this field) and return it. We try to provide
|
||||
* you with as much of context as possible, but normally you would only need
|
||||
* $synonym_entity and $synonym_entity_type parameters. Return an empty array
|
||||
* if entity of type $synonym_entity_type cannot be converted into a format
|
||||
* expected by $field.
|
||||
*
|
||||
* @param array $items
|
||||
* Array items that already exist in the field into which new synonyms is to
|
||||
* be added
|
||||
* @param array $field
|
||||
* Field array definition according to Field API of the field into which new
|
||||
* synonym is to be added
|
||||
* @param array $instance
|
||||
* Instance array definition according to Field API of the instance into
|
||||
* which new synonym is to be added
|
||||
* @param object $synonym_entity
|
||||
* Fully loaded entity object which has to be added as synonym
|
||||
* @param string $synonym_entity_type
|
||||
* Entity type of $synonym_entity
|
||||
*
|
||||
* @return array
|
||||
* Array of extra items to be merged into the items that already exist in
|
||||
* field values
|
||||
*/
|
||||
static public abstract function mergeEntityAsSynonym($items, $field, $instance, $synonym_entity, $synonym_entity_type);
|
||||
|
||||
/**
|
||||
* Supportive method.
|
||||
*
|
||||
* Set such a condition on $query that it will always yield no results. Should
|
||||
* be called from $this->processEntityFieldQuery() when for whatever reason
|
||||
* the object can't alter $query to include matched synonyms. As a fallback
|
||||
* it should call this method to make sure it filtered everything out.
|
||||
*
|
||||
* @param EntityFieldQuery $query
|
||||
* Query object passed to $this->processEntityFieldQuery() method
|
||||
* @param array $field
|
||||
* Field array passed to $this->processEntityFieldQuery() method
|
||||
*/
|
||||
static protected function emptyResultsCondition(EntityFieldQuery $query) {
|
||||
$query->entityCondition('entity_id', -1);
|
||||
}
|
||||
}
|
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Enables Entity Reference field type to be source of synonyms.
|
||||
*/
|
||||
|
||||
class EntityReferenceSynonymsExtractor extends AbstractSynonymsExtractor {
|
||||
|
||||
static public function fieldTypesSupported() {
|
||||
return array('entityreference');
|
||||
}
|
||||
|
||||
static public function synonymsExtract($items, $field, $instance, $entity, $entity_type) {
|
||||
$synonyms = array();
|
||||
|
||||
// For speading up loading all the entities at once.
|
||||
$target_tids = array();
|
||||
foreach ($items as $item) {
|
||||
$target_tids[] = $item['target_id'];
|
||||
}
|
||||
$entities = entity_load($field['settings']['target_type'], $target_tids);
|
||||
foreach ($entities as $entity) {
|
||||
$synonyms[] = entity_label($field['settings']['target_type'], $entity);
|
||||
}
|
||||
|
||||
return $synonyms;
|
||||
}
|
||||
|
||||
static public function processEntityFieldQuery($tag, EntityFieldQuery $query, $field, $instance) {
|
||||
// Unfortunately EntityFieldQuery does not currently support INNER JOINing
|
||||
// referenced entities via any field type.
|
||||
// Thus, we use an ugly solution -- going through all entities that exist
|
||||
// in such entity type trying to see if there is a match by entity's label.
|
||||
$efq = new EntityFieldQuery();
|
||||
$efq->entityCondition('entity_type', $field['settings']['target_type']);
|
||||
// Additionally we have to figure out which column in the entity table
|
||||
// represents entity label.
|
||||
$entity_info = entity_get_info($field['settings']['target_type']);
|
||||
if (!isset($entity_info['entity keys']['label'])) {
|
||||
// We can't get any matches if we do not know what column to query
|
||||
// against. So we add a condition to $query which will 100% yield empty
|
||||
// results.
|
||||
self::emptyResultsCondition($query);
|
||||
return;
|
||||
}
|
||||
$efq->propertyCondition($entity_info['entity keys']['label'], '%' . $tag . '%', 'LIKE');
|
||||
$result = $efq->execute();
|
||||
|
||||
if (!isset($result[$field['settings']['target_type']]) || !is_array($result[$field['settings']['target_type']])) {
|
||||
self::emptyResultsCondition($query);
|
||||
return;
|
||||
}
|
||||
$result = $result[$field['settings']['target_type']];
|
||||
$query->fieldCondition($field, 'target_id', array_keys($result));
|
||||
}
|
||||
|
||||
static public function mergeEntityAsSynonym($items, $field, $instance, $synonym_entity, $synonym_entity_type) {
|
||||
// Firstly validating that this entity refernce is able to reference to that
|
||||
// type of entity.
|
||||
$expected_synonym_entity_type = $field['settings']['target_type'];
|
||||
if ($expected_synonym_entity_type != $synonym_entity_type) {
|
||||
return array();
|
||||
}
|
||||
$synonym_entity_id = entity_id($synonym_entity_type, $synonym_entity);
|
||||
return array(array(
|
||||
'target_id' => $synonym_entity_id,
|
||||
));
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Default Synonyms Extractor class that ships together with the Synonym module.
|
||||
*/
|
||||
|
||||
class SynonymsSynonymsExtractor extends AbstractSynonymsExtractor {
|
||||
|
||||
static public function fieldTypesSupported() {
|
||||
return array('text', 'number_integer', 'number_float', 'number_decimal');
|
||||
}
|
||||
|
||||
static public function synonymsExtract($items, $field, $instance, $entity, $entity_type) {
|
||||
$synonyms = array();
|
||||
|
||||
foreach ($items as $item) {
|
||||
$synonyms[] = $item['value'];
|
||||
}
|
||||
|
||||
return $synonyms;
|
||||
}
|
||||
|
||||
static public function processEntityFieldQuery($tag, EntityFieldQuery $query, $field, $instance) {
|
||||
$query->fieldCondition($field, 'value', '%' . $tag . '%', 'LIKE');
|
||||
}
|
||||
|
||||
static public function mergeEntityAsSynonym($items, $field, $instance, $synonym_entity, $synonym_entity_type) {
|
||||
$synonym = entity_label($synonym_entity_type, $synonym_entity);
|
||||
switch ($field['type']) {
|
||||
case 'text':
|
||||
break;
|
||||
|
||||
// We add synonyms for numbers only if $synonym is a number.
|
||||
case 'number_integer':
|
||||
case 'number_float':
|
||||
case 'number_decimal':
|
||||
if (!is_numeric($synonym)) {
|
||||
return array();
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
return array(array(
|
||||
'value' => $synonym,
|
||||
));
|
||||
}
|
||||
}
|
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Enables Taxonomy Term Reference field types to be source of synonyms.
|
||||
*/
|
||||
|
||||
class TaxonomySynonymsExtractor extends AbstractSynonymsExtractor {
|
||||
|
||||
static public function fieldTypesSupported() {
|
||||
return array('taxonomy_term_reference');
|
||||
}
|
||||
|
||||
static public function synonymsExtract($items, $field, $instance, $entity, $entity_type) {
|
||||
$synonyms = array();
|
||||
|
||||
$terms = array();
|
||||
foreach ($items as $item) {
|
||||
$terms[] = $item['tid'];
|
||||
}
|
||||
$terms = taxonomy_term_load_multiple($terms);
|
||||
foreach ($terms as $term) {
|
||||
$synonyms[] = entity_label('taxonomy_term', $term);
|
||||
}
|
||||
return $synonyms;
|
||||
}
|
||||
|
||||
static public function processEntityFieldQuery($tag, EntityFieldQuery $query, $field, $instance) {
|
||||
// Unfortunately EntityFieldQuery does not currently support INNER JOINing
|
||||
// term entity that is referenced via taxonomy_term_reference field type.
|
||||
// Thus, we use an ugly solution -- going through all terms that exist in
|
||||
// vocabulary and trying to see if there is a match by term's name.
|
||||
$tids = array();
|
||||
|
||||
foreach ($field['settings']['allowed_values'] as $settings) {
|
||||
$efd = new EntityFieldQuery();
|
||||
$efd->entityCondition('bundle', $settings['vocabulary'])
|
||||
->entityCondition('entity_type', 'taxonomy_term')
|
||||
->propertyCondition('name', '%' . $tag . '%', 'LIKE');
|
||||
$result = $efd->execute();
|
||||
if (isset($result['taxonomy_term'])) {
|
||||
foreach ($result['taxonomy_term'] as $tid) {
|
||||
$tids[] = $tid->tid;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Now we have tids of terms from the referenced vocabulary which names
|
||||
// LIKE %$tag%, suggested are the terms that refer to any of these $tids.
|
||||
if (empty($tids)) {
|
||||
// No possible suggestions were found. We have to make sure $query yields
|
||||
// no results.
|
||||
self::emptyResultsCondition($query);
|
||||
return;
|
||||
}
|
||||
$query->fieldCondition($field, 'tid', $tids);
|
||||
}
|
||||
|
||||
static public function mergeEntityAsSynonym($items, $field, $instance, $synonym_entity, $synonym_entity_type) {
|
||||
// Taxonomy term reference supports only referencing of entity types
|
||||
// 'taxonomy_term'.. duh.
|
||||
if ($synonym_entity_type != 'taxonomy_term') {
|
||||
return array();
|
||||
}
|
||||
// Checking that $field is configured to reference the vocabulary of
|
||||
// $synonym_entity term.
|
||||
$is_allowed = FALSE;
|
||||
foreach ($field['settings']['allowed_values'] as $setting) {
|
||||
if ($synonym_entity->vocabulary_machine_name == $setting['vocabulary']) {
|
||||
if ($setting['parent'] == 0) {
|
||||
// No need to check parent property as there is no limitation on it.
|
||||
$is_allowed = TRUE;
|
||||
break;
|
||||
}
|
||||
else {
|
||||
foreach (taxonomy_get_parents_all($synonym_entity->tid) as $parent) {
|
||||
if ($parent->tid == $setting['parent']) {
|
||||
$is_allowed = TRUE;
|
||||
break(2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!$is_allowed) {
|
||||
// Synonym term is from a vocabulary that is not expected by this field,
|
||||
// or under unexpected parent.
|
||||
return array();
|
||||
}
|
||||
return array(array(
|
||||
'tid' => $synonym_entity->tid,
|
||||
));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user