work_time-1.0.x-dev/work_time.module
work_time.module
<?php
/**
* @file
* Provides a work time entity type.
*/
use Drupal\Core\Render\Element;
use Drupal\user\UserInterface;
/**
* Implements hook_theme().
*/
function work_time_theme() {
return [
'work_time' => [
'render element' => 'elements',
],
'work_time_block' => [
'variables' => [
'label' => '',
'display' => '',
'entity_id' => '',
'entity_type' => '',
'entity_bundle' => '',
'entity_field' => '',
'reference_field' => '',
'reference_id' => '',
'reference_type' => '',
'limit' => 'week',
],
],
'work_time_formatter' => [
'variables' => [
'entity_id' => '',
'entity_type' => '',
'entity_field' => '',
'reference_id' => '',
'reference_field' => '',
'reference_type' => '',
'beginning' => 0,
'time' => 0,
],
],
'views_view_work_time' => [
'file' => 'work_time.views.theme.inc',
],
'views_view_work_time_sheet' => [
'file' => 'work_time.views.theme.inc',
],
'views_view_work_timekeeper' => [
'file' => 'work_time.views.theme.inc',
],
];
}
/**
* Prepares variables for work time templates.
*
* Default template: work-time.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing work time information and any
* fields attached to the entity.
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_work_time(array &$variables) {
$variables['view_mode'] = $variables['elements']['#view_mode'];
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}
/**
* Implements hook_user_cancel().
*/
function work_time_user_cancel($edit, UserInterface $account, $method) {
switch ($method) {
case 'user_cancel_block_unpublish':
// Unpublish work times.
$storage = \Drupal::entityTypeManager()->getStorage('work_time');
$work_time_ids = $storage->getQuery()
->condition('uid', $account->id())
->condition('status', 1)
->execute();
foreach ($storage->loadMultiple($work_time_ids) as $work_time) {
$work_time->set('status', FALSE);
$work_time->save();
}
break;
case 'user_cancel_reassign':
// Anonymize work times.
$storage = \Drupal::entityTypeManager()->getStorage('work_time');
$work_time_ids = $storage->getQuery()
->condition('uid', $account->id())
->execute();
foreach ($storage->loadMultiple($work_time_ids) as $work_time) {
$work_time->setOwnerId(0);
$work_time->save();
}
break;
}
}
/**
* Implements hook_ENTITY_TYPE_predelete() for user entities.
*/
function work_time_user_predelete(UserInterface $account) {
// Delete work times.
$storage = \Drupal::entityTypeManager()->getStorage('work_time');
$work_time_ids = $storage->getQuery()
->condition('uid', $account->id())
->execute();
$work_times = $storage->loadMultiple($work_time_ids);
$storage->delete($work_times);
}
/**
* Implements hook_views_data().
*/
function work_time_views_data() {
$data['views']['work_time_area'] = [
'title' => t('Work time area'),
'help' => t('Provide a filter for work time style.'),
'area' => [
'id' => 'work_time_views_link',
],
];
return $data;
}
