crm_core_relationship.install in CRM Core 8.2
Install, update and uninstall functions for the relationship module.
File
modules/crm_core_relationship/crm_core_relationship.installView source
<?php
/**
* @file
* Install, update and uninstall functions for the relationship module.
*/
use Drupal\relation\Entity\RelationType;
use Drupal\relation\Entity\Relation;
/**
* Implements hook_install().
*/
function crm_core_relationship_install() {
$t = get_t();
// Add default relationships
$relation_types_info = array(
array(
'relation_type' => 'crm_friend',
'label' => $t('Friend of'),
'source_bundles' => array(
'crm_core_contact:individual',
),
'r_unique' => TRUE,
),
array(
'relation_type' => 'crm_employee',
'label' => $t('Employee of'),
'reverse_label' => $t('Employer of'),
'directional' => TRUE,
'r_unique' => TRUE,
'source_bundles' => array(
'crm_core_contact:individual',
),
'target_bundles' => array(
'crm_core_contact:organization',
),
),
array(
'relation_type' => 'crm_member',
'label' => $t('Member of'),
'reverse_label' => $t('Household for'),
'directional' => TRUE,
'r_unique' => TRUE,
'source_bundles' => array(
'crm_core_contact:individual',
),
'target_bundles' => array(
'crm_core_contact:household',
),
),
);
foreach ($relation_types_info as $relation_type_info) {
RelationType::create($relation_type_info)
->save();
}
}
/**
* Implements hook_uninstall()
*/
function crm_core_relationship_uninstall() {
// get all the relationship_types (bundles), find all fields
// delete them
$query = db_select('relation_bundles', 'rb')
->fields('rb', array(
'relation_type',
))
->condition('rb.entity_type', 'crm_core_contact')
->distinct()
->execute();
while ($record = $query
->fetchAssoc()) {
$relationship_type[] = $record['relation_type'];
}
foreach ($relationship_type as $type) {
// look into the database for each type
$relationship_query = db_select('relation', 'r')
->fields('r', array(
'relation_id',
))
->condition('r.relation_type', $type)
->execute();
while ($result = $relationship_query
->fetchAssoc()) {
// delete all the relationships of that type
Relation::load($result['relation_id'])
->delete();
}
// finally delete the relationshp_type
RelationType::load($type)
->delete();
}
}
/**
* Adding "status" field to crm_core relationships.
*/
function crm_core_relationship_update_7000() {
watchdog('update', 'Starting ' . __FUNCTION__ . ' update.');
// Attach "status" field to existing crm_core relationships.
foreach (crm_core_relationship_get_relationships() as $crm_relation) {
crm_core_relationship_field_attach_create_bundle('relation', $crm_relation->relation_type);
}
$t = get_t();
// Rename "Friend" relationship to "Friend of", if not renamed yet.
$crm_friend = RelationType::load('crm_friend');
if ($crm_friend->label == 'Friend') {
$crm_friend->label = $t('Friend of');
$crm_friend->reverse_label = $t('Friend of');
relation_type_save($crm_friend, array(
'relation_type',
));
}
// Fill in "status" field for existing relationships.
$rids = \Drupal::entityQuery('relation')
->execute();
$relations = Relation::loadMultiple($rids);
foreach ($relations as $relationship) {
if (crm_core_relationship_is_relationship_type($relationship->relation_type)) {
$relationship->crm_core_relationship_status[LANGUAGE_NONE][0]['value'] = 1;
field_attach_update('relation', $relationship);
}
}
watchdog('update', 'Finished ' . __FUNCTION__ . ' update.');
}
Functions
Name | Description |
---|---|
crm_core_relationship_install | Implements hook_install(). |
crm_core_relationship_uninstall | Implements hook_uninstall() |
crm_core_relationship_update_7000 | Adding "status" field to crm_core relationships. |