View source
<?php
use Drupal\Core\Access\AccessResultNeutral;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\redhen_connection\Entity\ConnectionType;
use Drupal\redhen_contact\Entity\Contact;
const REDHEN_CONNECTION_INACTIVE = 0;
const REDHEN_CONNECTION_ACTIVE = 1;
const REDHEN_CONNECTION_ENDPOINTS = 2;
function redhen_connection_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.redhen_connection':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Defines the base connection entity and features.') . '</p>';
return $output;
default:
}
}
function redhen_connection_theme() {
$theme = [];
$theme['redhen_connection'] = [
'render element' => 'elements',
'file' => 'redhen_connection.page.inc',
'template' => 'redhen_connection',
];
$theme['redhen_connection_content_add_list'] = [
'render element' => 'content',
'variables' => [
'content' => NULL,
'entity' => NULL,
],
'file' => 'redhen_connection.page.inc',
];
return $theme;
}
function redhen_connection_theme_suggestions_redhen_connection(array $variables) {
$suggestions = [];
$connection = $variables['elements']['#redhen_connection'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'redhen_connection__' . $sanitized_view_mode;
$suggestions[] = 'redhen_connection__' . $connection
->getType();
$suggestions[] = 'redhen_connection__' . $connection
->getType() . '__' . $sanitized_view_mode;
$suggestions[] = 'redhen_connection__' . $connection
->id();
$suggestions[] = 'redhen_connection__' . $connection
->id() . '__' . $sanitized_view_mode;
return $suggestions;
}
function redhen_connection_type_options_list() {
$options = [];
foreach (ConnectionType::loadMultiple() as $type) {
$options[$type
->id()] = $type
->label();
}
return $options;
}
function redhen_connection_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
$connection_plugin_manager = \Drupal::service('plugin.manager.connection_permission');
$plugin_definitions = $connection_plugin_manager
->getDefinitions();
foreach ($plugin_definitions as $plugin_id => $plugin_definition) {
$plugin_instance = $connection_plugin_manager
->createInstance($plugin_id);
if ($entity
->getEntityTypeId() === $plugin_instance
->get('subject_entity_type')) {
if (!empty($plugin_instance
->get('subject_entity_bundle'))) {
if ($plugin_instance
->get('subject_entity_bundle') != $entity
->bundle()) {
continue;
}
}
$contact = Contact::loadByUser($account);
if ($contact) {
return $plugin_instance
->hasRolePermissions($entity, $operation, $contact);
}
}
}
return new AccessResultNeutral();
}
function redhen_connection_entity_update(EntityInterface $entity) {
if (in_array($entity
->getEntityTypeId(), [
'redhen_contact',
'redhen_org',
]) && \Drupal::config('redhen_connection.settings')
->get('auto_disable_connections') == TRUE) {
$original = $entity->original;
if (!$entity
->isActive() && $original
->isActive()) {
$connections = \Drupal::service('redhen_connection.connections')
->getConnections($entity, NULL, NULL, TRUE);
foreach ($connections as $connection) {
$connection
->setActive(FALSE);
$connection
->save();
}
}
}
}
function redhen_connection_entity_delete(EntityInterface $entity) {
if (in_array($entity
->getEntityTypeId(), [
'redhen_contact',
'redhen_org',
])) {
$connections = \Drupal::service('redhen_connection.connections')
->getConnections($entity, NULL, NULL, FALSE);
foreach ($connections as $connection) {
$connection
->delete();
}
}
}
function redhen_connection_entity_type_alter(array &$entity_types) {
foreach ($entity_types as $entity_type_id => $entity_type) {
if ($entity_type
->hasViewBuilderClass() && $entity_type
->hasLinkTemplate('canonical')) {
$canonical = $entity_type
->getLinkTemplate('canonical');
$entity_type
->setLinkTemplate('redhen_connection', "{$canonical}/connections");
}
}
}
function redhen_connection_get_connection_entity_from_route() {
foreach (\Drupal::routeMatch()
->getParameters() as $param) {
if ($param instanceof EntityInterface) {
if ($param
->getEntityTypeId() !== 'redhen_connection_type') {
return $param;
}
}
}
return NULL;
}