You are here

crm_core_contact.module in CRM Core 8.2

Provides default CRM Core Contact entities and the ability to create more.

File

modules/crm_core_contact/crm_core_contact.module
View source
<?php

/**
 * @file
 * Provides default CRM Core Contact entities and the ability to create more.
 */
use Drupal\Core\Render\Element;

/**
 * Implements hook_theme().
 */
function crm_core_contact_theme() {
  return array(
    'crm_core_contact' => array(
      'render element' => 'elements',
      'template' => 'crm-core-contact',
    ),
  );
}

/**
 * Process variables for CRM Core Contact.
 *
 * Default template: crm_core_contact.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - crm_core_contact: The CRM Core Contact entity.
 */
function template_preprocess_crm_core_contact(&$variables) {
  $variables['view_mode'] = $variables['elements']['#view_mode'];

  /** @var \Drupal\crm_core_contact\Entity\Contact $contact */
  $contact = $variables['elements']['#crm_core_contact'];
  $variables['crm_core_contact'] = $contact;
  foreach (Element::children($variables['elements']) as $key) {
    $variables['content'][$key] = $variables['elements'][$key];
  }

  // Add classes based on the type of contact.
  $variables['attributes']['class'][] = 'crm_core_contact';
  $variables['attributes']['class'][] = 'crm_core_contact-' . $contact
    ->bundle();
}

/**
 * Implements hook_theme_suggestions_HOOK().
 *
 * This function simply adds template suggestions for various
 * contact types.
 */
function crm_core_contact_theme_suggestions_crm_core_contact(array $variables) {
  $suggestions = array();

  /** @var \Drupal\crm_core_contact\Entity\Contact $crm_core_contact */
  $crm_core_contact = $variables['elements']['#crm_core_contact'];
  $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');

  // Add template suggestions.
  $suggestions[] = 'crm_core_contact__' . $sanitized_view_mode;
  $suggestions[] = 'crm_core_contact__' . $crm_core_contact
    ->bundle();
  $suggestions[] = 'crm_core_contact__' . $crm_core_contact
    ->bundle() . '__' . $sanitized_view_mode;
  $suggestions[] = 'crm_core_contact__' . $crm_core_contact
    ->id();
  $suggestions[] = 'crm_core_contact__' . $crm_core_contact
    ->id() . '__' . $sanitized_view_mode;
  return $suggestions;
}

/**
 * Implements hook_mail().
 */
function crm_core_contact_mail($key, &$message, $params) {
  $message['subject'] = $params['subject'];
  $message['body'][] = $params['message'];
}

/**
 * Implements hook_entity_dependencies().
 *
 * Adding contact activities and relationships as dependencies.
 */
function crm_core_contact_entity_dependencies($entity, $entity_type) {
  $dependencies = array();
  if ($entity_type == 'crm_core_contact') {

    // Lets check activities.
    if (module_exists('crm_core_activity')) {
      $query = new EntityFieldQuery();
      $query
        ->entityCondition('entity_type', 'crm_core_activity');
      $query
        ->fieldCondition('field_activity_participants', 'target_id', $entity->contact_id);
      $query
        ->fieldCondition('field_activity_participants', 'target_type', 'crm_core_contact');
      $result = $query
        ->execute();
      if (!empty($result['crm_core_activity'])) {
        foreach (array_keys($result['crm_core_activity']) as $activity_id) {
          $dependencies[] = array(
            'type' => 'crm_core_activity',
            'id' => $activity_id,
          );
        }
      }
    }

    // Lets check relations.
    if (module_exists('relation')) {
      $query = new EntityFieldQuery();
      $query
        ->entityCondition('entity_type', 'relation');
      $query
        ->fieldCondition('endpoints', 'entity_id', $entity->contact_id);
      $result = $query
        ->execute();
      if (!empty($result['relation'])) {
        foreach (array_keys($result['relation']) as $rid) {
          $dependencies[] = array(
            'type' => 'relation',
            'id' => $rid,
          );
        }
      }
    }
  }
  return $dependencies;
}

Functions