crm_core-8.x-3.x-dev/modules/crm_core_contact/crm_core_contact.module
modules/crm_core_contact/crm_core_contact.module
<?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) {
$variables['view_mode'] = $variables['elements']['#view_mode'];
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $variables['elements']['#crm_core_individual'];
$variables['crm_core_individual'] = $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'][] = 'crm_core_individual';
$variables['attributes']['class'][] = 'crm_core_individual-' . $entity->bundle();
}
/**
* 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) {
$variables['view_mode'] = $variables['elements']['#view_mode'];
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $variables['elements']['#crm_core_organization'];
$variables['crm_core_organization'] = $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'][] = 'crm_core_organization';
$variables['attributes']['class'][] = 'crm_core_organization-' . $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'];
}
