You are here

crm_core_contact.module in CRM Core 8.3

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 [
    'crm_core_individual' => [
      'render element' => 'elements',
      'template' => 'crm-core-individual',
    ],
    'crm_core_organization' => [
      'render element' => 'elements',
      'template' => 'crm-core-organization',
    ],
  ];
}

/**
 * Process variables for CRM Core Individual.
 *
 * Default template: crm-core-individual.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - crm_core_individual: The CRM Core Individual entity.
 */
function template_preprocess_crm_core_individual(array &$variables) {
  crm_core_contact_common_template_preprocess('crm_core_individual', $variables);
}

/**
 * Process variables for CRM Core Organization.
 *
 * Default template: crm-core-organization.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - crm_core_organization: The CRM Core Organization entity.
 */
function template_preprocess_crm_core_organization(array &$variables) {
  crm_core_contact_common_template_preprocess('crm_core_organization', $variables);
}

/**
 * Common theme template additions.
 *
 * @param string $entity_id
 *   Entity.
 * @param array $variables
 *   Variables.
 */
function crm_core_contact_common_template_preprocess(string $entity_id, array &$variables) {
  $variables['view_mode'] = $variables['elements']['#view_mode'];

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

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

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function crm_core_contact_theme_suggestions_crm_core_individual(array $variables) {
  return crm_core_contact_theme_suggestions_common('crm_core_individual', $variables['elements']['#crm_core_individual'], $variables['elements']['#view_mode']);
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function crm_core_contact_theme_suggestions_crm_core_organization(array $variables) {
  return crm_core_contact_theme_suggestions_common('crm_core_organization', $variables['elements']['#crm_core_organization'], $variables['elements']['#view_mode']);
}

/**
 * Common suggestions for organizations and individuals.
 *
 * @param string $entity_id
 *   Entity.
 * @param \Drupal\crm_core_contact\IndividualInterface|\Drupal\crm_core_contact\OrganizationInterface $entity
 *   Entity.
 * @param string $view_mode
 *   View mode.
 *
 * @return array
 *   Suggestions.
 */
function crm_core_contact_theme_suggestions_common(string $entity_id, $entity, string $view_mode) : array {
  $suggestions = [];
  $sanitized_view_mode = str_replace('.', '_', $view_mode);

  // Add template suggestions.
  $suggestions[] = $entity_id . '__' . $sanitized_view_mode;
  $suggestions[] = $entity_id . '__' . $entity
    ->bundle();
  $suggestions[] = $entity_id . '__' . $entity
    ->bundle() . '__' . $sanitized_view_mode;
  $suggestions[] = $entity_id . '__' . $entity
    ->id();
  $suggestions[] = $entity_id . '__' . $entity
    ->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'];
}

Functions