contacts-8.x-1.x-dev/modules/user_dashboard/contacts_user_dashboard.module
modules/user_dashboard/contacts_user_dashboard.module
<?php
/**
* @file
* Module related hook implementations for the contacts user dashboard module.
*/
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultNeutral;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Template\Attribute;
use Drupal\contacts\Form\ContactsProfileForm;
use Drupal\contacts_user_dashboard\Plugin\Derivative\UserDashboardLocalTask;
use Drupal\user\UserInterface;
/**
* Implements hook_help().
*/
function contacts_user_dashboard_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the crm_tools module.
case 'help.page.contacts_user_dashboard':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('This module manages user tabs to allow for user dashboard configuration.') . '</p>';
return $output;
}
}
/**
* Implements hook_theme().
*/
function contacts_user_dashboard_theme($existing, $type, $theme, $path) {
return [
'user_dashboard_summary' => [
'render element' => 'user_dashboard_summary',
],
];
}
/**
* Implements hook_entity_type_build().
*
* Add user dashboard form operation to profile entity.
*/
function contacts_user_dashboard_entity_type_build(array &$entity_types) {
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
$entity_types['profile']->setFormClass('user_dashboard', ContactsProfileForm::class);
}
/**
* Implements hook_ENTITY_TYPE_insert() for profile_type.
*
* Set up entity displays for user dashboard.
*/
function contacts_user_dashboard_profile_type_insert($entity) {
$form_mode = \Drupal::entityTypeManager()->getStorage('entity_form_mode')->load('user_dashboard');
$display_mode = \Drupal::entityTypeManager()->getStorage('entity_form_mode')->load('user_dashboard');
// Do not attempt to create the view/form displays if the
// user_dashboard form mode doesn't exist yet. This can happen during
// module installation if another module is being enabled at
// the same time as contacts_user_dashboard which inserts profile types.
// For example, commerce installs the 'customer' profile type.
if (!$form_mode || !$display_mode) {
return;
}
/** @var \Drupal\profile\Entity\ProfileTypeInterface $entity */
$view_storage = \Drupal::entityTypeManager()->getStorage('entity_view_display');
$form_storage = \Drupal::entityTypeManager()->getStorage('entity_form_display');
$values = [
'id' => implode('.', ['profile', $entity->id(), 'user_dashboard']),
'targetEntityType' => 'profile',
'bundle' => $entity->id(),
'mode' => 'user_dashboard',
'status' => TRUE,
];
$view_display = $view_storage->create($values);
$view_display->save();
$form_display = $form_storage->create($values);
$form_display->save();
}
/**
* Prepares variables for user_dashboard_summary templates.
*
* Default template: user-dashboard-summary.html.twig.
*
* @param array $variables
* An associative array containing render information.
*/
function template_preprocess_user_dashboard_summary(array &$variables) {
$variables['title'] = $variables['user_dashboard_summary']['#title'];
$variables['content'] = $variables['user_dashboard_summary']['#content'];
$variables['buttons'] = [];
foreach ($variables['user_dashboard_summary']['#buttons'] as $button) {
$button['attributes'] = new Attribute($button['attributes'] ?? []);
$variables['buttons'][] = $button;
}
}
/**
* Implements hook_module_implements_alter().
*/
function contacts_user_dashboard_module_implements_alter(&$implementations, $hook) {
if ($hook == 'local_tasks_alter') {
$group = $implementations['contacts_user_dashboard'];
unset($implementations['contacts_user_dashboard']);
$implementations['contacts_user_dashboard'] = $group;
}
}
/**
* Implements hook_local_tasks_alter().
*/
function contacts_user_dashboard_local_tasks_alter(&$local_tasks) {
$container = \Drupal::getContainer();
$local_task = UserDashboardLocalTask::create($container, 'contacts_user_dashboard_tab');
$local_task->alterLocalTasks($local_tasks);
}
/**
* Implements hook_entity_access().
*/
function contacts_user_dashboard_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
if ($operation == 'dashboard' && $entity instanceof UserInterface) {
if ($account->hasPermission('access user dashboards')) {
return AccessResult::allowed()->cachePerPermissions()->addCacheableDependency($entity);
}
elseif ($account->id() == $entity->id()) {
return AccessResult::allowed()->cachePerUser();
}
return AccessResultNeutral::neutral("The 'access user dashboards' permission is required.")->cachePerPermissions()->addCacheableDependency($entity);
}
}
