You are here

log.module in Log entity 8

Same filename and directory in other branches
  1. 7 log.module
  2. 2.x log.module

Contains log.module..

File

log.module
View source
<?php

/**
 * @file
 * Contains log.module..
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\log\LogInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Render\Element;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_help().
 */
function log_help($route_name, RouteMatchInterface $route_match) {
  $output = '';

  // Main module help for the log module.
  if ($route_name == 'help.page.log') {
    $output = '';
    $output .= '<h3>' . t('About') . '</h3>';
    $output .= '<p>' . t('Provides Log entity') . '</p>';
  }
  return $output;
}

/**
 * Implements hook_theme().
 */
function log_theme() {
  $theme = [
    'log' => array(
      'render element' => 'elements',
    ),
    'log_add_list' => array(
      'variables' => array(
        'content' => NULL,
      ),
    ),
  ];
  return $theme;
}

/**
 * Prepares variables for Log templates.
 *
 * Default template: log.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - elements: An associative array containing the user information and any
 *   - attributes: HTML attributes for the containing element.
 */
function template_preprocess_log(array &$variables) {

  // Helpful $content variable for templates.
  foreach (Element::children($variables['elements']) as $key) {
    $variables['content'][$key] = $variables['elements'][$key];
  }
}

/**
 * Prepares variables for list of available log type templates.
 *
 * Default template: log-add-list.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - content: An array of content types.
 *
 * @see log_add_page()
 */
function template_preprocess_log_add_list(array &$variables) {
  $variables['types'] = array();
  if (!empty($variables['content'])) {
    foreach ($variables['content'] as $type) {
      $variables['types'][$type
        ->id()] = array(
        'type' => $type
          ->id(),
        'add_link' => \Drupal::l($type
          ->label(), new Url('log.add', array(
          'log_type' => $type
            ->id(),
        ))),
      );
    }
  }
}

/**
 * Implements hook_log_access().
 */
function log_log_access(LogInterface $log, $op, $account) {
  $type = $log
    ->bundle();
  switch ($op) {
    case 'view':
      if ($account
        ->hasPermission('view any ' . $type . ' log entities', $account)) {
        return AccessResult::allowed()
          ->cachePerPermissions();
      }
      else {
        return AccessResult::allowedIf($account
          ->hasPermission('view own ' . $type . ' log entities', $account) && $account
          ->id() == $log
          ->getOwnerId())
          ->cachePerPermissions()
          ->cachePerUser()
          ->cacheUntilEntityChanges($log);
      }
    case 'update':
      if ($account
        ->hasPermission('edit any ' . $type . ' log entities', $account)) {
        return AccessResult::allowed()
          ->cachePerPermissions();
      }
      else {
        return AccessResult::allowedIf($account
          ->hasPermission('edit own ' . $type . ' log entities', $account) && $account
          ->id() == $log
          ->getOwnerId())
          ->cachePerPermissions()
          ->cachePerUser()
          ->cacheUntilEntityChanges($log);
      }
    case 'delete':
      if ($account
        ->hasPermission('delete any ' . $type . ' log entities', $account)) {
        return AccessResult::allowed()
          ->cachePerPermissions();
      }
      else {
        return AccessResult::allowedIf($account
          ->hasPermission('delete own ' . $type . ' log entities', $account) && $account
          ->id() == $log
          ->getOwnerId())
          ->cachePerPermissions()
          ->cachePerUser()
          ->cacheUntilEntityChanges($log);
      }
    default:

      // No opinion.
      return AccessResult::neutral();
  }
}

/**
 * Implements hook_log_create_access().
 */
function log_log_create_access(AccountInterface $account, array $context, $entity_bundle = NULL) {
  return AccessResult::allowedIfHasPermission($account, 'create ' . $entity_bundle . ' log entities')
    ->cachePerPermissions();
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Alters the theme form to use the admin theme on log editing.
 *
 * @see log_form_system_themes_admin_form_submit()
 */
function log_form_system_themes_admin_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['admin_theme']['log_use_admin_theme'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use the administration theme when editing or creating log entity'),
    '#default_value' => \Drupal::configFactory()
      ->getEditable('log.settings')
      ->get('log_use_admin_theme'),
  );
  $form['#submit'][] = 'log_form_system_themes_admin_form_submit';
}

/**
 * Form submission handler for system_themes_admin_form().
 *
 * @see log_form_system_themes_admin_form_alter()
 */
function log_form_system_themes_admin_form_submit($form, FormStateInterface $form_state) {
  \Drupal::configFactory()
    ->getEditable('log.settings')
    ->set('log_use_admin_theme', $form_state
    ->getValue('log_use_admin_theme'))
    ->save();
  \Drupal::service('router.builder')
    ->setRebuildNeeded();
}

Functions

Namesort descending Description
log_form_system_themes_admin_form_alter Implements hook_form_FORM_ID_alter().
log_form_system_themes_admin_form_submit Form submission handler for system_themes_admin_form().
log_help Implements hook_help().
log_log_access Implements hook_log_access().
log_log_create_access Implements hook_log_create_access().
log_theme Implements hook_theme().
template_preprocess_log Prepares variables for Log templates.
template_preprocess_log_add_list Prepares variables for list of available log type templates.